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 Наука
Variable might be null? THREATEN IT into compliance
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 ;)
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.
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.
@@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.
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)
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.
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
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)".
Finally Zig is catching steam. 1.0 is around the corner lads.
Elixir + Zig = MUAH!!!!
Zig 1.0 is probably years away knowing what they want zig to be.
@@Tobssonseeing their milestones and release cycles on GitHub makes me feel like 1.0 is as close as year of the Linux lol
Dereferencing with .*. Is actually pretty awesome, reading left to right, then going to the left, then to the right again, is horrible
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.
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.
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.
what would be the use case for a constant pointer ?
Prime in C doing x->y is the same as (*x).y
Not using deodorant in Brazil is almost a crime. Please don't do that ever again 😆
Is he not streaming to youtube while in Brazil?
Orelse? Is that not the // operator in Perl?
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
Back to gruvbox.
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.
Btw vim motions jkl works on youtube in browser as well.
j to go back,
l to go forward,
k to pause.
No music sound to us.
where are you?
53:15 this is awkward
Huffman?
huffman coding is a way to compress data. It's used by jpeg, png, zip files, etc.
@@notuxnobux I know, but why it's there
@@notuxnobux NSO stuff
@@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.
@@beeplove7 oh, I get it now
The worst part about zig is anytype. You have to “figure” out all methods a type implement if you want to have a “interface”
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.
Have a look at jetzig- it’s under heavy development, but it aims to provide a DX similar to ruby on rails
@@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.
@@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.
@@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.
Early ~
TLDR where Prime is /why?
conference in brazil, idk why
He goes to Brazil basically just for a meet up (inside a conference). He live reacted TeeJ's and BigBoxSWE's video there lol.
LUA CONFERENCE
BRAZIL MENTIONED
LUA MENTIONED
NVIM BTW
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?
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
@@UnidimensionalPropheticCatgirl So it’s static assert on assignment. Well any access to a thing that isn’t modified is thread safe.
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.
@@SlightRemorse So it’s inferring that so you can put the decorative keyword there.
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.