Zig in Depth: struct

Поделиться
HTML-код
  • Опубликовано: 25 ноя 2024
  • НаукаНаука

Комментарии • 10

  • @bjorn2625
    @bjorn2625 7 месяцев назад +3

    These videos are SO good. I don’t know how you’ve got the time and the energy to keep making them, but honestly you’re beating any written guide or documentation I’ve seen on Zig, anywhere.

  • @thilina91
    @thilina91 7 месяцев назад

    another great video, thanks!

  • @bjorn2625
    @bjorn2625 7 месяцев назад +1

    Man the . notation on pointers really breaks my C++ trained mental model. When you set Y based on address of X, that line screams “you’re setting the temporary, stack-allocated instance, not the original struct”.
    I actually really love Zig so far, but find it really confusing to have to refer back to the declaration of the constant/var to figure out if I’m dealing with a pointer to the heap or a stack allocated variable.
    NGL I think I’ll miss the -> operator

    • @dudethebuilder
      @dudethebuilder  7 месяцев назад

      Agreed, this is a very convenient syntax sugar, but can be cause for confusion and errors. I guess these are the tradeoffs that have to be weighed when designing a language.

  • @makroshlyta
    @makroshlyta 10 месяцев назад

    great vid as always!

  • @b3owu1f
    @b3owu1f 10 месяцев назад +1

    This is fantastic.. with one exception.. when you talked about the separate Point.zig file, the syntax y: f32.. it has no var, const, etc before it. I get that having the const Point = @This is similar (or is it identical) to the const Point = struct {} in the other file. But is it valid to have a .zig source file with the y and x defined above? Seems like there is nothing indicating if they are const, var, etc. Also.. maybe I missed it, but when I import Point.zig, and having the @This const in there.. does that basically now make the function new and distance part of the struct @This const? So identical to the way it's defined in the first file? Lastly.. why.. why would you ever do this? It seems harder to read and serves no purpose that I can see. Why not in Point.zig have the actual const Point = struct { ... } so its readable/clear that its a struct, and the two functions are methods (or namespace and method)? So what is the benefit of doing this? I asked Bard this question and it came back with basically saying that @This approach is good for generic declarations inside a function, but it didnt indicate using a separate file with this sort of code layout.

    • @dudethebuilder
      @dudethebuilder  10 месяцев назад +2

      You totally can put the const struct = Point definition in another file instead of using the entire file as the struct definition and having to use @This to reference itself. Then when you @import you would @import("point.zig").Point. I guess the answer to all your questions is "flexibility and consistency". Zig lets you define a struct in several ways but no matter how you do it, structs are always anonymous and are named according to the rules of the compiler. One advanteage to the entire file as the struct is that you can start with an inline `const struct = ...` style and over time if the struct grows large and complex you can just crete a new file, copy and paste everything between the {} and add @This, and you're done.

    • @b3owu1f
      @b3owu1f 9 месяцев назад

      @@dudethebuilder Ah.. ok.. that is cool then. I just wasn't sure if there was one way or another "the right way". Sounds like either are perfectly fine with the individual file offering a little more separation/organization. Thank you.

    • @bjorn2625
      @bjorn2625 7 месяцев назад

      I guess another advantage of putting the struct definition inside its own file is that you now have private variables and functions.

  • @venkatabhaskararao4061
    @venkatabhaskararao4061 11 месяцев назад

    amazing!!