ChanServ changed the topic of #zig to: zig programming language | ziglang.org | be excellent to each other | channel logs: https://irclog.whitequark.org/zig/
schme245 has quit [Remote host closed the connection]
_whitelogger has joined #zig
schme245 has joined #zig
schme245 has quit [Ping timeout: 240 seconds]
marmotini has joined #zig
marmotini has quit [Ping timeout: 240 seconds]
_whitelogger has joined #zig
steveno__ has joined #zig
_whitelogger has joined #zig
steveno__ has quit [Ping timeout: 240 seconds]
steveno_ has quit [Quit: Leaving]
kristate has joined #zig
hio has quit [Quit: Connection closed for inactivity]
steveno has joined #zig
<emekankurumeh[m]>
what's the default tag type for a union?
<andrewrk>
emekankurumeh[m], you mean on of these? union(enum)
<emekankurumeh[m]>
no, when you just have union.
marmotini has joined #zig
<andrewrk>
for those, there is no tag
<andrewrk>
however if safety is enabled, they have a secret tag, to help catch access of inactive field
<emekankurumeh[m]>
so they are like c unions in release mode?
<andrewrk>
yes
<daurnimator>
andrewrk: I assume you saw my comment about programmer-provided tag functions?
<andrewrk>
daurnimator, yes - however I have not yet taken the time to write a thought out response
<daurnimator>
k :)
<andrewrk>
I'm trying to merge a pull request before bed
jevinskie has joined #zig
marmotini has quit [Quit: Leaving]
marmotini_ has joined #zig
schme245 has joined #zig
schme245 has quit [Ping timeout: 245 seconds]
marmotini_ has quit [Ping timeout: 250 seconds]
marmotini_ has joined #zig
steveno_ has joined #zig
steveno_ has quit [Remote host closed the connection]
steveno has quit [Ping timeout: 240 seconds]
companion_cube has joined #zig
marmotini_ has quit [Ping timeout: 250 seconds]
darithorn has quit [Quit: Leaving]
kristate_ has joined #zig
kristate has quit [Ping timeout: 250 seconds]
jevinski_ has joined #zig
jevinskie has quit [Ping timeout: 250 seconds]
marmotini_ has joined #zig
schme245 has joined #zig
schme245 has quit [Remote host closed the connection]
kristate_ has quit [Remote host closed the connection]
kristate has joined #zig
kristate has quit [Remote host closed the connection]
kristate has joined #zig
schme245 has joined #zig
schme245 has quit []
gamester has joined #zig
<gamester>
Let's say that you're manually translating a header or maybe translating a c++ project one file at a time, in these cases you might want to make sure all the code in a file compiles but the only way to do so is a laburous process of 'touching' the code somehow, say in a test you do "const a = func_name" to see if func_name compiles. Will there be a better alternative?
kristate has quit [Remote host closed the connection]
gamester has quit [Read error: Connection reset by peer]
gamester has joined #zig
kristate has quit [Remote host closed the connection]
kristate has joined #zig
schme245 has joined #zig
kristate has quit [Ping timeout: 240 seconds]
steveno has joined #zig
hio has joined #zig
<hio>
wow I think from the reaction to this post we can see that people really want golang like syntax (no semicolons, no braces around round if/while/for) https://news.ycombinator.com/item?id=19086712
<hio>
even the lack of var/const makes a lot of sense in that context. Instead he just says :=, everything is just shorter. No public/private either.
gamester has quit [Read error: Connection reset by peer]
<schme245>
how are you drawing that conclusion from that post? Go is barely mentioned, no one (as far as I could see) is talking about semicolons or braces whatsoever
<hio>
that's what that language looks like
<schme245>
that does not imply that people like it because of the syntax
<hio>
come on man, the language isnt even released yet. Practically nothing other than the syntax is visible
marmotini_ has quit [Ping timeout: 245 seconds]
<andrewrk>
gamester, are you checking the IRC logs?
<companion_cube>
hio: to me it mostly seems that people want impossible things, like compiling super fast yet being as fast as C and as expressive as C++
<andrewrk>
it's my goal to accomplish the first 2 things. not sure about exactly what it means to be as expressive as C++
<companion_cube>
I think it was phrased as "can do anything C++ can" or something like that
<companion_cube>
and then also "for memory management, do like rust but simpler" 😅
noonien has joined #zig
<hio>
any turing complete language can do anything that c++ can do, that's how he means it
schme245 has quit [Remote host closed the connection]
schme245 has joined #zig
schme245 has quit [Ping timeout: 250 seconds]
steveno has quit [Ping timeout: 250 seconds]
<companion_cube>
really? that's not a very interesting property then :/
<very-mediocre>
any specific plans for differentiating zig?
<andrewrk>
nope, the design of zig is not affected by competing with other languages
gamester has joined #zig
<andrewrk>
zig continues on its straight and narrow course
schme245 has joined #zig
<gamester>
andrewrk: yeah I check the logs
<andrewrk>
gamester, if you're translating C/C++ code and you want your zig code to interact with your C/C++ code then you're probably making exported functions, right?
<andrewrk>
exported stuff gets analyzed
<very-mediocre>
andrewrk: I see. It's certainly an embarrassment of riches. Can't complain.
<andrewrk>
very-mediocre, also, that language is vaporware, just like jai
<andrewrk>
not much to do until they actually release
<companion_cube>
jai seems to have so much ad-hoc parts in its design, iirc, I'm curious if it will lead somewhere
<very-mediocre>
I appreciate the discipline in zig's design
<very-mediocre>
I feel like software is reaching a weird place of overabundance/overcrowding now, so it's a bit worrisome sometimes
gamester has quit [Remote host closed the connection]
<schme245>
I don't understand why people get hyped over something like V: some 10 LOC examples followed by claims like "Faster than C" and "Easier memory management than Rust" with nothing to back it up
<schme245>
of course that sounds fantastic if you take it at face value
<companion_cube>
agreed
<andrewrk>
yeah, I posted that to fish more information from the creator
<schme245>
but then the author mentions, and I quote: "I haven't mentioned memory management because it's not done yet. [...] I want to do something similar to Rust's approach, but much much simpler. It's not an easy task."
<schme245>
you don't say
<schme245>
and also: " V compiles 15 million lines of code per second on an 8-core desktop CPU." well, if you take out memory management from C++ and Rust maybe they would also compile faster...
<schme245>
"/end rant" :D
<companion_cube>
pretty sure tcc compiles fast as well
<andrewrk>
I intend to get to that speed with zig in the self hosted compiler
<andrewrk>
well, we're limited by LLVM, but I have a few tricks up my sleeve
<companion_cube>
that's at odds with optimizations
<andrewrk>
not in debug mode
<andrewrk>
in debug mode zig is willing to trade runtime performance in exchange for faster compilation speed
<schme245>
yeah, I'm sure it can be done, but I don't understand why you would make that claim for a language that has barely implemented any of the features that it claims it will have. of course all those features will have a huge impact on compilation speed
<schme245>
Jai actually exists and it seems to compile pretty darn fast
<companion_cube>
ah yes, ok, if it's for debug I agree
<andrewrk>
and the fact that zig intentionally does not guarantee an ABI for functions / types means the compiler has a lot of tools available to improve compile speed
<companion_cube>
if you disable any form of inlining or lto, that's probably doable
<schme245>
andrewrk, are you planning any streams the coming week(s)?
<andrewrk>
schme245, today at 17:00 EST (in 6 hours) I'm doing one on Thread Local Storage
<andrewrk>
while it's fresh in my mind
<schme245>
cool! I think I'll be able to catch most of it (23:00 for me)
<andrewrk>
looking forward to it :)
<andrewrk>
I also take general questions about zig at the end
jevinski_ has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
marmotini_ has joined #zig
marmotini_ has quit [Remote host closed the connection]
marmotini_ has joined #zig
<schme245>
random question: is it possible to combine labeled blocks with inline for/while loops to assign values?
<schme245>
I've tried different variations of this:
<schme245>
const a = inline for (array) |x| :blk {
<schme245>
if (x == 4) {
<schme245>
break :blk x;
<schme245>
}
<schme245>
} else {
<schme245>
break :blk 99;
<schme245>
}
<schme245>
but I can't seem to get the order of things right
<companion_cube>
why isn't each construct an expression, in the end? it's simpler and more uniform?
<andrewrk>
schme245, I recommend https://clbin.com/ for multi-line snippets
<andrewrk>
(I can see this one just fine though)
<schme245>
ahh, roger!
<andrewrk>
that's a good question. it should work but I don't think we have any test cases covering it, so maybe there is a compiler bug
<andrewrk>
companion_cube, in zig nearly everything is an expression. things that are not expressions are variable declarations, and .... I can't actually think of anything else
<schme245>
if statements?
<companion_cube>
andrewrk: except for { … } blocks without labels, which is surprising
<andrewrk>
that's still an expression, that returns a void value
<companion_cube>
:/
<andrewrk>
zig has no if statements, only if expressions :)
<companion_cube>
I suppose a `if` without else must return void, for example
<companion_cube>
that's what OCaml and rust do, and it's natural
<schme245>
I'm extending my question to about inline for/while to if's as well :)
<schme245>
andrewrk, do you prefer to ask about stuff like this on IRC or to open issues on GitHub? (or to not ask at all? xD)
<andrewrk>
there's no inline if - when an if expression's condition is comptime-known, the compiler analyzes only the active branch
<andrewrk>
same with switch
<schme245>
I mean to combine the if with a named block so that you can "assign" the if to a variable
<andrewrk>
IRC is fine. GitHub is fine too but try to make sure the question is not answered in http://ziglang.org/documentation/master/ or another issue already
<andrewrk>
const x = if (c) a else b;
<schme245>
doh, of course... thanks for reminding me
<companion_cube>
but no `const x = if (c) a else { foo; b }` without label?
<andrewrk>
const x = if (c) a else l: { foo(); break :l b};
<schme245>
btw, I figured out how to make it work: `const a = blk: inline for (array) |x| { ... }`
marmotini_ has quit [Ping timeout: 245 seconds]
<companion_cube>
this issue baffles me, really
<schme245>
nvm, it doesn't work that way either
<companion_cube>
it's like the only "problem" with `{x; y}` returning `y` is that you can return from a function in 2 ways
<companion_cube>
but not having to type `return` is good, it should only be used for early returns
<andrewrk>
"not having to type" is not a strong motivation in zig design decisions
<andrewrk>
zig programmers are willing to type a lot
<companion_cube>
I don't mean it as saving keystrokes, just as being more straightforward
<companion_cube>
expressions are good
<companion_cube>
(this makes me angry every time I write python and I forgot to put `return` and it returns None silently)
<andrewrk>
dtz, never mind, it was fixed in a newer version
<andrewrk>
companion_cube, I see
<companion_cube>
I'm also really used to OCaml where `;` is a separator, I must add (so everything is always an expression, and blocks are just like parenthesis)
<companion_cube>
but rust does the same, and it's quite ergonomic imho
<Flaminator>
I honestly believe that returning by excluding the ; at the end of the statement instead of using return x; is a bad thing in Rust.
<companion_cube>
why?
marmotini_ has joined #zig
<companion_cube>
it's the simplest , cleanest way of making every block an expression, I think
<Flaminator>
Because writing something without the ; means different things in different situations. In one of them it returns and in the other case it assigns.
Hejsil has joined #zig
marmotini_ has quit [Ping timeout: 245 seconds]
<companion_cube>
in rust, it's pretty simple: `[let] x = a;` is the only way to assign
<companion_cube>
if there's no `=`, it's not an assignment
<Hejsil>
I have a few problems with the 'no semi to return rule'
<companion_cube>
wow, ok, seems like it's the right choice for people here :/
<Hejsil>
Firstly, for a newcommer, how would you look up the semantics for 'no semi at the end of statement' in docs?
<companion_cube>
it'd be in the tutorial, hopefully?
<andrewrk>
I don't mean to gang up on you companion_cube but it's true that zig syntax is designed to be readable, even for people who have never seen the language before or read any docs
<companion_cube>
andrewrk: no worries, I'm the one making trouble if anything
<companion_cube>
the current trend of expression-oriented languages would make this natural, I'd hope, but apparently it's surprising to many
<companion_cube>
maybe it boils down to different notions of what a block is, really: is it just a scoping construct with a chain of `;` separated expressions, or is it something more important
<Hejsil>
Also, labeled blocks can break out of many scopes, which is really nice
<Hejsil>
But i guess expr langs would probably just 'bubble' the value up
<companion_cube>
yep
<companion_cube>
looking at my rust code, I have a ton of if/match that return expressions (often without a block)
<Flaminator>
I honestly just want a better improved C for embedded programming and Zig seems to give me that.
<companion_cube>
but sometimes I add a `assert(…);` before the expression to return
halosghost has joined #zig
Akuli has joined #zig
very-mediocre has quit [Ping timeout: 256 seconds]
Ichorio has joined #zig
steveno has joined #zig
Akuli has quit [Quit: Leaving]
Akuli has joined #zig
<companion_cube>
anyway, I suppose `result` would look ok to me (avoids the need to introduce label names everywhere)
<companion_cube>
sorry for the whole shenanigan
<companion_cube>
andrewrk: is comptime done with a zig interpreter that lives in the compiler?
Zaab1t has joined #zig
marmotini_ has joined #zig
marmotini_ has quit [Ping timeout: 244 seconds]
marmotini_ has joined #zig
<Hejsil>
companion_cube, I'm not andrewrk but yes. The compiler compiles Zig into Zig IR which is then interpreted
<companion_cube>
that's neat. it's unsual to see a properly dependently typed language.
<companion_cube>
unusual
steveno_ has joined #zig
steveno has quit [Ping timeout: 250 seconds]
<companion_cube>
is it also how the pruning of `if` with comptime conditions is done?
darithorn has quit [Quit: Leaving]
Hejsil has quit [Quit: Page closed]
Zaab1t has quit [Quit: bye bye friends]
marmotini_ has quit [Ping timeout: 244 seconds]
marmotini_ has joined #zig
wootehfoot has joined #zig
steveno_ has quit [Ping timeout: 244 seconds]
marmotini_ has quit [Remote host closed the connection]
<andrewrk>
companion_cube, the code for each zig ir instruction handles both comptime and runtime interpretation. here's an example...
<andrewrk>
you can see the same validation is done whether comptime or runtime. if both operands are comptime known it computes a comptime answer. otherwise it emits a runtime instruction
<companion_cube>
so you mean that compile-time evaluation is always done, and if runtime bits remain in a comptime context there's an error
<companion_cube>
(I mean, comptime evaluation of `if (T==bool) a else b` evaluates everything at comptime, but a and b will be mostly composed of runtime instructions?)
<andrewrk>
yes to your first statement. I'm not sure what you're asking in the parenthetical
<companion_cube>
so things like constant folding is just comptime evaluation in Zig, that's what I mean
<andrewrk>
yes. `1 + 2` turns into a bin op instruction, which notices both operands are known, and so produces a known result
<andrewrk>
it would not re-order them though: `1 + runtime_value + 2` is going to turn into two LLVM add instructions
<companion_cube>
and similarly for `if` I suppose, except you only reduce both branches if the condition is not true/fakse
<companion_cube>
false*
<companion_cube>
so that's really a form of symbolic evaluation in a dependently typed language, you should write a paper :P
<andrewrk>
all the control flow constructs actually get simplified to a Control Flow Graph with goto and conditional goto instructions
<andrewrk>
and zig ir supports comptime goto
<andrewrk>
it used to even be exposed, but turns out we don't need goto after all
<companion_cube>
thanks to defer?
steveno_ has joined #zig
<shachaf>
It seems to me that labeled-break handles almost everything that people want goto for.
<shachaf>
It's nice that Zig has labeled-break-with-value. Almost no other language seems to have it for some reason.
<companion_cube>
rust does both of these, it's in the air perhaps
<halosghost>
if rust fans are called rustaceans, are zig fans called ziggies?
<shachaf>
Hmm, when I looked at Rust it looked like a pretty limited form, I think?
<andrewrk>
goto has 4 use cases: 1. jump backwards (use a labeled loop) 2. jump forwards (use a labeled block) 3. error cleanup (use defer/errdefer) 4. computed goto (ok actually we haven't proven that this isn't needed yet)
<companion_cube>
it mostly has labels on loops + continue/break with labels
<shachaf>
"continue" and "break" are both a kind of early-exit
<companion_cube>
andrewrk: makes a lot of sense, indeed.
<companion_cube>
4. might be useful for writing things like interpreters, but that's a really niche case
<andrewrk>
it's a valid use case of zig, so I won't close the issue that we have open for it until we have a satisfying recommendation
<shachaf>
In "loop { ... }", you can translate it to "outer: { loop { inner: { ... } } }", and then "break" means "break :outer" and "continue" means "break :inner"
<andrewrk>
hmm I think there is no issue open for computed goto. anyway I want someone to have a Real Actual Use Case for computed goto to drive the discussion if it were to happen
<shachaf>
Hmm, I kind of wonder whether the error handling style of goto has a more general use. It's sort of about sharing code between different parts of a function.
<companion_cube>
shachaf: what about continue in non nested loops? :p
<shachaf>
Probably better replaced with other structures wherever possible, though.
<shachaf>
companion_cube: What about it?
<companion_cube>
it's not directly a break
<shachaf>
Yes it is. It early-exits the loop body.
<companion_cube>
(unless you introduce a second block from which you break)
<shachaf>
Yes, sure.
<shachaf>
The thing I wrote isn't a nested loop, it just adds extra labels.
<companion_cube>
ah yes, I read your snippet as nested loops but you did exactly that, my bad
<shachaf>
Python's "for x in xs: BODY else: ELSE" loop can be read as "outer: { for (xs) |x| { inner: { BODY } } { ELSE } }"
<andrewrk>
zig also has else on for and while
<andrewrk>
justification for that is to be consistent with `if`, because `if` works with bools, optionals, and error unions, and so does while
<shachaf>
Oh, I didn't notice that.
<andrewrk>
and they are different syntax, depending on your capture groups
<shachaf>
So regular while always has type void, but while-else can have arbitrary type?
<andrewrk>
so you actually need the `else` on a `while` in order to do while (error_union_value) |payload| {...} else |err| {...}
<andrewrk>
while can be any type
<andrewrk>
break takes a parameter
<andrewrk>
just like return
<shachaf>
I guess while (true) could also have arbitrary type, if you can guarantee it statically.
<shachaf>
Right, but what happens when the loop exits without a break?
<companion_cube>
--> loop{ … } :)
<andrewrk>
shachaf, the else
<shachaf>
But if there's no else can the while have any type?
<andrewrk>
oh
<andrewrk>
yeah, if the else is unreachable
<andrewrk>
I think zig is smart enough to understand that about while (true) at least
<shachaf>
Ah, I guess.
<shachaf>
Makes sense.
<shachaf>
I should read the whole documentation rather than just bits of it.
<andrewrk>
it's ok, there's a lot of it, and there are a lot of TODOs
<andrewrk>
and it changes
<andrewrk>
once it's more complete it will be my goal to answer every question with a link to documentation
<andrewrk>
not because I don't enjoy chatting or I'm too busy, but because I want the docs to be that good
<companion_cube>
:)
steveno_ has quit [Ping timeout: 268 seconds]
Ichorio has quit [Ping timeout: 244 seconds]
Akuli has quit [Quit: Leaving]
<emekankurumeh[m]>
who is the person working on the documentation generator@
<emekankurumeh[m]>
*?
halosghost has quit [Quit: WeeChat 2.3]
wootehfoot has quit [Read error: Connection reset by peer]
<MajorLag>
I think the best argument for computed goto is simply that you can't always trust the optimizer to do the right thing, and you'd rather have the option of being explicit than fumbling around trying to trick it into doing the right thing.
schme245 has quit [Remote host closed the connection]
<andrewrk>
MajorLag, I would be willing to put the computed goto optimization into the zig frontend if necessary
<shachaf>
I wonder whether any language has goto-with-value
<shachaf>
"x = foo:; ...; goto :x 5;"
<companion_cube>
try/with ? :D
<shachaf>
I guess that's what SSA phi nodes are, sort of.
<andrewrk>
shachaf, zig's labeled block is pretty much that
<shachaf>
But only for early exit, right?
<andrewrk>
hmm, yes you wouldn't be able to give a value forwards and give a value backwards to the same destination