Learning Zig Part 2

Поделиться
HTML-код
  • Опубликовано: 9 май 2024
  • LIVE ON TWITCH: / theprimeagen
    Become a backend engineer. Its my favorite site
    boot.dev/?promo=PRIMEYT
    This is also the best way to support me is to support yourself becoming a better backend engineer.
    Get in on Discord: / discord
    Get in on Twitter: / theprimeagen
    Got Something For Me to Read or Watch??:
    / theprimeagenreact
  • НаукаНаука

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

  • @tiedye001
    @tiedye001 2 месяца назад +45

    Variable might be null? THREATEN IT into compliance

  • @IanRiley915
    @IanRiley915 2 месяца назад +4

    Nothing captures the life of a dev better than watching prime perk up at the idea of using his phone for chat only to find out that its battery is nearly dead ;)

  • @notuxnobux
    @notuxnobux 2 месяца назад +16

    40:27 files in zig are anonymous structs yes, it's like the file is a struct without the const name = struct { header part, so you can write exact same code as in a struct there, so you can have fields separated by , instead of ;. When you import the file with const Name = @import("Name.zig"); you can do const name = Name{};
    since files are structs, you can use metaprogramming on files to find all fields (variables, structs, functions, etc) that is defined in a file. That makes metaprogramming work on everything without any special weird cases.

    • @ForeverZer0
      @ForeverZer0 2 месяца назад

      I thought it was strange at first, but in practice it is actually pretty useful. I am still very much a Zig amateur, but just discovered how you can use this feature to make your files the "type" being defined within them with something like "const MyType = @This();", and not have to nest all your functions/fields within the opening/closing braces of a struct definition. It also makes importing a cleaner looking, so you can do "const MyType = @import("MyType.zig");" instead of "const MyType = @import("my_type.zig").MyType;"
      It almost gives me Lua vibes where you can require the file as the thing.

    • @notuxnobux
      @notuxnobux 2 месяца назад

      @@ForeverZer0 Yeah and it also makes the compiler much simpler. I wrote my own compiler once and I made files structs as well naturally. If your code can handle referencing a struct then importing a file and referencing data in that will automatically work as well, no need for a separate "File" type.

    • @SimonBuchanNz
      @SimonBuchanNz 2 месяца назад

      Traditionally you need to distinguish "static members" (using the container as a namespace) and "instance members" (treating the container as a template) - how does that work?
      In many script-ier languages the module level (barring import/export stuff) is identical to function bodies, so it's not a totally new idea (JavaScript commonjs implemented a module system using this fact, for example)

  • @maxoumimaro
    @maxoumimaro 2 месяца назад +2

    Thanks Prime for doing this learning ziglings, seeing you start this on part1 gave me the boost to just do it and I am currently on exercise 97 since then. You are going to like the next few parts.
    What I prefered was getting into the compile time code, it is very interesting.

  • @axisaligned9799
    @axisaligned9799 2 месяца назад +8

    im not sure why he says the var const pointer stuff is weird, it literally behaves the same way in rust LOL. `let` is `const` , `let mut` is `var`, `&T` is `*const T`, `&mut T` is `*T`
    edit* he figured it out

  • @andrebrait
    @andrebrait 2 месяца назад +4

    5:36 you need to see "const u8" as its own type. So then the pointer becomes this:
    const p: *(const u8) = &a;
    (I added the parentheses for clarity)
    It's a constant "p" of type "pointer to a (constant u8)".

  • @_orangutan
    @_orangutan 2 месяца назад +4

    Finally Zig is catching steam. 1.0 is around the corner lads.
    Elixir + Zig = MUAH!!!!

    • @Tobsson
      @Tobsson 2 месяца назад +3

      Zig 1.0 is probably years away knowing what they want zig to be.

    • @Vergillo
      @Vergillo Месяц назад

      @@Tobssonseeing their milestones and release cycles on GitHub makes me feel like 1.0 is as close as year of the Linux lol

  • @ficolas2
    @ficolas2 2 месяца назад +2

    Dereferencing with .*. Is actually pretty awesome, reading left to right, then going to the left, then to the right again, is horrible

    • @TheDolphiner
      @TheDolphiner 2 месяца назад +2

      Zig's dereference syntax is a wonderful example of going with a familiar C-like syntax, but still rethinking all the choices. Similar example I like is just how much I adore zig's multiline strings.

  • @notuxnobux
    @notuxnobux 2 месяца назад +4

    47:31 the variable is const but not the pointer. The way its done is zig makes sense. On the left side is the variable definition and on the right side is the type definition. const is attached to the variable name so the variable is const, there is no const attached to the type so the type is not const.

  • @adamsribz
    @adamsribz 2 месяца назад

    glorp_access3 isn't a "pointer to a constant", though. It's a constant pointing to a mutable var (&glorp). that's how i read it.
    anyways i really like this series!
    EDIT: ok, you addressed this haha.

  • @kevinkkirimii
    @kevinkkirimii 2 месяца назад

    what would be the use case for a constant pointer ?

  • @CatDevz
    @CatDevz 2 месяца назад +3

    Prime in C doing x->y is the same as (*x).y

  • @helanomiguel3783
    @helanomiguel3783 2 месяца назад +2

    Not using deodorant in Brazil is almost a crime. Please don't do that ever again 😆

  • @ark_knight
    @ark_knight 2 месяца назад

    Is he not streaming to youtube while in Brazil?

  • @MrMeltdown
    @MrMeltdown 2 месяца назад

    Orelse? Is that not the // operator in Perl?

  • @MCroppered
    @MCroppered 2 месяца назад

    I’ve just realised who you remind me of. Ive finally worked it out. Beavis. You are Beavis from Beavis and Butthead. Yeyeyyeyeyeye = boioioioioioioing. You know it

  • @AChonkyBird
    @AChonkyBird 2 месяца назад

    Back to gruvbox.

  • @GuyFromJupiter
    @GuyFromJupiter 2 месяца назад

    I don't dislike prefix derefs, but I do hate that in C
    pointer declarations and type casts have almost the exact same syntax as dereferencing a pointer. I find Rust's way of using & for reference types to be much more natural.

  • @TheTrippyTerminal
    @TheTrippyTerminal 2 месяца назад

    Btw vim motions jkl works on youtube in browser as well.
    j to go back,
    l to go forward,
    k to pause.

  • @TheTrippyTerminal
    @TheTrippyTerminal 2 месяца назад

    No music sound to us.

  • @albumin30days90
    @albumin30days90 2 месяца назад

    where are you?

  • @JLarky
    @JLarky 2 месяца назад

    53:15 this is awkward

  • @RajarshiKhatua100
    @RajarshiKhatua100 2 месяца назад

    Huffman?

    • @notuxnobux
      @notuxnobux 2 месяца назад

      huffman coding is a way to compress data. It's used by jpeg, png, zip files, etc.

    • @RajarshiKhatua100
      @RajarshiKhatua100 2 месяца назад

      @@notuxnobux I know, but why it's there

    • @RajarshiKhatua100
      @RajarshiKhatua100 2 месяца назад

      @@notuxnobux NSO stuff

    • @beeplove7
      @beeplove7 2 месяца назад +2

      @@RajarshiKhatua100 it was a roadmap for the stream. Learning zig was the first segment and Huffman was the last segment where he implemented Huffman encoding in his ASCII doom project.

    • @RajarshiKhatua100
      @RajarshiKhatua100 2 месяца назад

      @@beeplove7 oh, I get it now

  • @metaltyphoon
    @metaltyphoon 2 месяца назад

    The worst part about zig is anytype. You have to “figure” out all methods a type implement if you want to have a “interface”

  • @porcupinetree-bb3zg
    @porcupinetree-bb3zg 2 месяца назад

    this may be unpopular opinion but i think zig is more complex than rust for things outside of systems programming. for systems programming it's simpler but for like web backends and things like that it's way more complex. the syntax is simpler but it feels more low level in those contexts. that's my personal experience from using both.

    • @steveoc64
      @steveoc64 2 месяца назад

      Have a look at jetzig- it’s under heavy development, but it aims to provide a DX similar to ruby on rails

    • @porcupinetree-bb3zg
      @porcupinetree-bb3zg 2 месяца назад

      @@steveoc64 yeah, that's actually what i tried. maybe a different api would be more appealing to me. i tried that and zap and i liked zap more but there's so much boilerplate for like a basic middleware implementation. the issue is all of the lower level code, which is why i think rust works better in that context, because it feels a lot more like a higher level language. the borrow checker makes it feel like higher level. zig is a lot closer to c which has benefits for systems programming, but other areas is too verbose in my opinion. i'm willing to give it another shot in the future though when the ecosystem matures more.

    • @porcupinetree-bb3zg
      @porcupinetree-bb3zg 2 месяца назад

      @@steveoc64 take a look at axum and loco and you can kind of see what i mean. it's still complex relative to go or typescript, but less complex relative to zig for web. at least for the moment, because it's still very early in development for zig. i'll give zig a couple of years and try it out again.

    • @steveoc64
      @steveoc64 2 месяца назад

      @@porcupinetree-bb3zg yeah, I’ve done a tonne of go in prod (8 years) .. have used Axum (don’t like)
      Am currently developing using http.zig - which sort of fits in similar to go, although the middleware (dispatcher) interface is a bit different.
      I’m working on my fork of that, building a framework specifically for HTMX. Just taking my time with it for now.
      But again, even http.zig is moving fast and getting better. Zig is ideal if you want to get involved contributing to building the next great framework.. but can be frustrating if you just want to start building apps right now.

  • @itsmenewbie03
    @itsmenewbie03 2 месяца назад

    Early ~

  • @Ash-qp2yw
    @Ash-qp2yw 2 месяца назад

    TLDR where Prime is /why?

    • @UnidimensionalPropheticCatgirl
      @UnidimensionalPropheticCatgirl 2 месяца назад

      conference in brazil, idk why

    • @opposite342
      @opposite342 2 месяца назад +1

      He goes to Brazil basically just for a meet up (inside a conference). He live reacted TeeJ's and BigBoxSWE's video there lol.

    • @ficolas2
      @ficolas2 2 месяца назад +3

      LUA CONFERENCE
      BRAZIL MENTIONED
      LUA MENTIONED
      NVIM BTW

  • @platin2148
    @platin2148 2 месяца назад +1

    Const is fundamentally useless the compiler does already know waht’s constant and what is not.
    Does this help the compiler to build the SSA form of the code in anyway?

    • @UnidimensionalPropheticCatgirl
      @UnidimensionalPropheticCatgirl 2 месяца назад

      consts are thread safe, so it’s good to have them known, beyond that it’s just safety feature, so it throws errors when you try to assign to it. It doesn’t help ssa as far as I know

    • @platin2148
      @platin2148 2 месяца назад +1

      @@UnidimensionalPropheticCatgirl So it’s static assert on assignment. Well any access to a thing that isn’t modified is thread safe.

    • @SlightRemorse
      @SlightRemorse 2 месяца назад

      With v0.12 if you have a var declaration that is never modified the compiler actually complains that you should turn the var declaration into a const one instead.

    • @platin2148
      @platin2148 2 месяца назад

      @@SlightRemorse So it’s inferring that so you can put the decorative keyword there.

    • @Tom-dd3vl
      @Tom-dd3vl 2 месяца назад +1

      Comments are useless because the compiler already knows what to compile your code to. Just like comments, const exists to help the programmer reading and writing the code. You'll never accidentally modify it. This is especially helpful in cases where modifying it would be a mistake 100% of the time, like captured values.
      That all aside, const does have a benefit. Constants can be inlined. If the compiler had to find every reference of every variable to make sure it's constant that'll bog down compile times. Finding every reference of every variable also becomes impossible as soon as pointers are involved. It gets too complex for the compiler to follow.