ChanServ changed the topic of #zig to: zig programming language | https://ziglang.org | be excellent to each other | channel logs: https://irclog.whitequark.org/zig/
klltkr has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
klltkr has joined #zig
klltkr has quit [Client Quit]
klltkr has joined #zig
klltkr has quit [Client Quit]
hiljusti has joined #zig
hiljusti has quit [Ping timeout: 250 seconds]
pretty_dumm_guy has quit [Quit: WeeChat 3.2-dev]
Guest13599 has joined #zig
tlam has joined #zig
Guest13599 has quit [Ping timeout: 252 seconds]
earnestly has quit [Ping timeout: 252 seconds]
<olabaz> can you use switch on an enum?
<g-w1> yes
<g-w1> what trouble are you running into?
<olabaz> none, just wondering if it was allowed.
<g-w1> imo thats the main thing to switch on :)
<olabaz> Is it possible to have a next function in an enum so that it updates my variable
<g-w1> yes
<olabaz> when I did next(self: Myenum) { self = blah} it said I couldn't modify self
<g-w1> self: *Myenum
<g-w1> function paramaters are immutable
<olabaz> ah I see
<olabaz> it's also asking for an else clause even though I did every branch
<g-w1> is it a non-exuastive num?
<g-w1> s/num/enum
<olabaz> oh I forgot to dereference
<g-w1> ah :)
leon-p has quit [Quit: leaving]
hiljusti has joined #zig
olabaz has left #zig ["WeeChat 2.3"]
hiljusti has quit [Ping timeout: 250 seconds]
proteusguy has quit [Remote host closed the connection]
lonjil has quit [Quit: No Ping reply in 180 seconds.]
lonjil has joined #zig
hiljusti has joined #zig
<noam> Thought occurred
<noam> Is there an analyzer valgrind-style that runs two variants of a program and tracks diverging code flow?
<noam> because there absolutely should be
<noam> Heck, just recording control flow would be enough
xackus has quit [Ping timeout: 240 seconds]
sord937 has joined #zig
<noam> I just felt the presence of Andrew, calling me to use Zig. An hour spent debugging only to realize an implicit cast from u32 to u16 was removing 90% of the dependency list. "Debug your cooooooode," the voice intoned, "nooooot your knowledge of the lannnnguaaage"
<noam> If I don't say "foo = bar & 0xFFFF", why the heck does the language think it's okay to insert a truncation? :(
waleee-cl has quit [Quit: Connection closed for inactivity]
<noam> andrewrk: idea that I'm not sure deserves a proper proposal: compiler error if anytype is used but comptime logic in the function constrains it to a single type (e.g. `if(@TypeOf(foo) != bar)`)
cole-h has quit [Ping timeout: 240 seconds]
zenkuro has quit [Ping timeout: 268 seconds]
mikdusan has quit [Quit: WeeChat 3.0.1]
mikdusan has joined #zig
antaoiseach has joined #zig
<antaoiseach> hello folks, basic question here - does switching on a tagged union need to return the same type for all prongs?
<antaoiseach> (sort of makes sense I think since the switch expression has a single type?)
<noam> Peer types must always resolve to a single type
<noam> So you can have e.g. a comptime_int and a u8, because the comptime-int implicitly upcasts to u8
<noam> but you can't do, say, a random structure and a number
<antaoiseach> noam: okay, that makes sense. thanks!
<noam> np :)
<noam> Basically, every expression has a *single type* - so if you have, say, `a + (b * c) / (if(foo)d else e)`, every subexpression has a type, and they all have to be compatible as you climb the chain
<noam> a,b,c,d, and e each have a type; (b * c) has a type; a + (b * c) has a type; i(f(foo)d else e) has a type...
<noam> There's other ways of thinking about it; I personally like this one, but my programming habits aren't the norm and you might find a different perspective more useful
<antaoiseach> noam: thanks for that! :-) ... yes, indeed, sometimes one can get confused when overthinking things! That is a useful aide memoire
[wtf] has joined #zig
Bernstein has joined #zig
klltkr has joined #zig
antaoiseach has quit [Quit: leaving]
earnestly has joined #zig
waffle_ethics has quit [Read error: Connection reset by peer]
waffle_ethics has joined #zig
detha has quit [Quit: ZNC - http://znc.in]
detha has joined #zig
hiljusti has quit [Ping timeout: 276 seconds]
klltkr has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
gazler_ is now known as gazler
tdeo has quit [Ping timeout: 245 seconds]
tdeo has joined #zig
pretty_dumm_guy has joined #zig
pretty_dumm_guy has quit [Quit: WeeChat 3.2-dev]
Guest78827 has joined #zig
[RMS] has quit [Ping timeout: 246 seconds]
[RMS] has joined #zig
zenkuro has joined #zig
Guest78827 is now known as notzmv
antaoiseach has joined #zig
proteusguy has joined #zig
<antaoiseach> hello folks, another basic question - the Zig ref mentions that slices have length known at runtime and also have a `ptr` field. So that would mean that something like my_array[1..3] is not a slice? The next example then calls this a slice as well, so I'm a bit confused.
<ifreund> my_array[1..3] will actually evaluate to a pointer to an array of 3 items as the slicing bounds are comptime known. a pointer to array coerces to a slice though
<antaoiseach> ifreund: yes, precisely! That's what I found when I printed the type out - something like *[5]i32.
<antaoiseach> Ah, okay ... so something like arr[1..3] would coerce to a slice?
<dutchie> yup
<antaoiseach> Ah, okay. I think I need to experiment a bit more to get the variations right in my head. Thanks, guys!
<ifreund> no problem!
<dutchie> there is a zig showtime about the various pointer types that's pretty informative
<antaoiseach> dutchie: Thank you! That should come in useful now... I'll watch it, and get back with any questions I might have. Cheers!
TheLemonMan has joined #zig
<antaoiseach> dutchie: okay, so watched the whole video - it was quite useful indeed, especially the coercion rules between the various kinds of pointers. Thanks again for the recco!
<dutchie> cool :)
kwilczynski has quit []
antaoiseach has quit [Quit: leaving]
<marler8997> fengb, I got pointed to your Swhash type
<marler8997> I was curious, in your "hash" function, why do you need to copy the string to a temporary on the stack in order to call readIntNative on it?
leon-p has joined #zig
<TheLemonMan> in case the string is too short
<TheLemonMan> tmp is zero-filled
<marler8997> oh right
kragacles has quit [Quit: ZNC 1.8.2 - https://znc.in]
kwilczynski has joined #zig
kwilczynski has left #zig [#zig]
kragacles has joined #zig
TheLemonMan has quit [Quit: "It's now safe to turn off your computer."]
[wtf] has quit [Quit: [wtf]]
nycex has quit [Ping timeout: 240 seconds]
nycex has joined #zig
plumm has joined #zig
cole-h has joined #zig
<kiedtl> What's the import name of array_list? std.array_list doesn't seem to work
<mq32> std.ArrayList
<kiedtl> Ah
<kiedtl> Still getting used to Zig's module system
<fengb> Modules are manually managed in the code. std exports ArrayList as a different symbol than what's on the file system
B767B has joined #zig
<andrewrk> noam, there is no compile error planned for the situation you mentioned with `anytype`. also I think it is a non-trivial, nuanced topic
B767B has quit [Remote host closed the connection]
waleee-cl has joined #zig
xackus has joined #zig
aigoo has quit [Quit: leaving]
TheLemonMan has joined #zig
rxF is now known as torque
semarie has quit [Quit: WeeChat 3.1]
PhiCore has joined #zig
semarie has joined #zig
Akuli has joined #zig
zenkuro has quit [Read error: Connection reset by peer]
zenkuro has joined #zig
wootehfoot has joined #zig
<ifreund> andrew's streaming in case anyone here isn't aware and would like to tune in: https://www.twitch.tv/andrewrok
<hspak> Sneaky stream!
Bernstein has quit [Remote host closed the connection]
sord937 has quit [Quit: sord937]
<noam> andrewrk: yeah, I figured as much :) I think this is one of those cases where "don't encourage the pattern" is really the best solution
pretty_dumm_guy has joined #zig
TheLemonMan has quit [Quit: "It's now safe to turn off your computer."]
wootehfoot has quit [Quit: Leaving]
PhiCore has quit [Quit: Connection closed]
Akuli has quit [Quit: Leaving]
aigoo_ has joined #zig
skuzzymiglet has quit [Remote host closed the connection]
skuzzymiglet has joined #zig
notzmv has quit [Ping timeout: 240 seconds]
Guest43271 has joined #zig
<kiedtl> Are dynamically-sized stack-allocated slices supported yet? e.g. `var foo: [not_comptime_length]Foo = undefined`
<mq32> no
<mq32> (also, this is an array)
<ifreund> kiedtl: no, and they may never be
<kiedtl> oops, yeah, array
<mq32> kiedtl: the pattern here is to make `foo` just *big enough* (so you know the upper bound) or use a FixedBufferAllocator if you need multiple allocations
<kiedtl> uh huh, so I just choose some number that most likely won't be crossed (like, I dunno, 2048)... and that's idiomatic Zig? (I'd prefer to not use an allocator in what I'm doing)
<mq32> if you know the upper bound: use that
<mq32> FixedBufferAllocator can still be stack-backed
<mq32> and also requires a "big enough" array
<kiedtl> I do know the practical upper bound, so I'll use that. And since I don't want to use an allocator, the only other option is to pass the buffer as an argument, like C, I guess
<mq32> yeah
<mq32> but as said: FixedBufferAlloator can still be used
<mq32> it just uses that array for allocation :)
<xackus> you can let the caller decide and pass a FixedBufferAllocator when needed
<kiedtl> Yup, that's the option I'll use if I opt to use an allocator later
<andrewrk> kiedtl, this is intentionally not supported so that the compiler has a better knowledge of the upper bound stack size
<andrewrk> however I've been thinking about adding another kind of allocator to std.heap, or maybe a feature to ArenaAllocator
<andrewrk> idea would be that you pass this one around for temporary allocations that are tied to the stack, but can't be stack allocated because the amount is runtime known
<andrewrk> it would be equivalent to creating an arena allocator for a stack frame but would be more efficient to reset to a previous state (currently with ArenaAllocator you can only free everything at once)
<ifreund> would it be different from an arena per stack frame?
<mq32> i think the idea is that you can allocate again into the same buffers?
<mq32> i wanted to do something like this as well
<andrewrk> ifreund, I'm guessing you wrote that after reading 3/4 of my monologue :D
<mq32> it's stupid to unmap() your pages when you just know that you're going to allocate them again anyways in the next cycle
<andrewrk> yeah
<ifreund> andrewrk: indeed :D
<mq32> i want such a thing for games as well
<mq32> var temp_allocator = TempAllocator.init(); while(true) { temp_allocator.reset(); … }
<andrewrk> this would be a very reasonable alternative when someone inquires about alloca
<ifreund> would alloca with a comptime known upper bound really be that bad?
<andrewrk> I considered that and came to the conclusion that it's just too obscure a use case, for not enough of a benefit, with too much of a footgun associated with it
<andrewrk> especially when the alternative is so comfy and performant
<andrewrk> "obstack" is a reference to a third party library for doing the thing I mentioned above with a new allocator or with a feature of ArenaAllocator, but with a less comfy API
<ifreund> makes sense to me, I'm very curious to see how the API of the "new allocator" shapes up
<marler8997> andrewrk, there's another concern about calling WSAStartup as a fallback
<marler8997> WSAStartup is not thread safe
<andrewrk> fuck me
<mq32> oh, yeah
<mq32> i remember :D
* mq32 burns the world down, only to hit WSAStartup
<andrewrk> marler8997, I'm *this close* to banning ws2_32.dll from zig std lib altogether
<mq32> ^^
<mq32> ban windows.
<marler8997> I have a weird idea, creating a wrapper dll around ws2_32.dll and using DllStartup to call WSAStartup
<andrewrk> no
<andrewrk> ws2_32.dll is a local maximum
<mq32> it's not a maximum at all /o\
<mq32> it#s more a global minimum
<marler8997> what do you mean by local maximum?
<marler8997> lol, I know what local maximum means, but how is ws2_32.dll a local maximum?
<andrewrk> ws2_32.dll is all userland. we could use the lower level API and not depend on ws2_32.dll at all
<marler8997> gotcha
<andrewrk> we don't have to play games with this wsastartup bullshit
<marler8997> I'm very unfamilair with AFD to be any help with analyzing that
<andrewrk> I'd be curious what some of the other std libs do
<marler8997> I'm extremely familiar with ws2_32. I had to implement my own version of it
<mq32> probably in net_imp module?
<mq32> self.0.peer_addr()
<mq32> what
<ifreund> self is a tuple :P
<mq32> ah
<andrewrk> I see a WsaStartup call
<marler8997> yeah, not sure if sys/windows/net.rs: init is supposed to be called by the user?
<andrewrk> probably not since they use the init_once pattern
<marler8997> very similar to GCC'c c constructor
<mq32> aaand another user ran into -msvc being the bad default on windows
<andrewrk> wsastartup not being thread safe is no big deal, we already have the init_once abstraction as well
<marler8997> init_once makes it thread safe?
waleee-cl has quit [Quit: Connection closed for inactivity]
<andrewrk> marler8997, yeah init_once is just a mutex + flag so that multiple threads can race to initialize something and only 1 wins and the others wait
<marler8997> ooo, that might cause an issue
<marler8997> it's using atomicLoad
<marler8997> so if the first thread increments the atomic, any thread can come in and start using ws2_32 before the first thread has called WSAStartup
<andrewrk> nah any other threads will wait until the init is finished
<andrewrk> atomic load is the first thing a mutex implementation does
<marler8997> I'm looking at once.zig in "call"
<marler8997> oh, the atomicLoad isn't setting done?
<marler8997> I see, then it's good
<marler8997> so, do you think we should put an "init" call before calling anything in ws2_32, or do a fallback?
<andrewrk> I don't really care. if I were implementing it, I'd probably do the error handling
<marler8997> actually the init method may be problematic if an applications want's a specific version
<marler8997> with the fallback method, if the user calls WSAStartup themselves they can select their own version
<companion_cube> welp, the zig Tracy stubs helped me do my own tracy bindings
<companion_cube> 💃
<companion_cube> ___tracy_emit_zone_begin_alloc and all that jazz
<andrewrk> looks like go std lib uses ws2_32.dll
<andrewrk> goodness I had no idea how much of a train wreck networking on windows was
<andrewrk> windows APIs get some stuff right
<andrewrk> networking is not one of them
<g-w1> and it seems like go reimplements everything lol
<marler8997> networking is especially weird, they've got windows-specific functions, and then a half-baked version of the posix ones
<marler8997> looks like go is taking advantage of some type of "static module constructor" as well
<ifreund> ew
<marler8997> so andrewrk, if I'm making a zig library that requires some sort of initialization with optional parameters, wouldn't you say that I should provide some type of "init" function? Couldn't zig's `std.os` do the same thing?
<andrewrk> yes I would, but I don't think there should be a global os init(). it would not be appropriate to initialize networking for all applications for example
<ifreund> it also wouldn't be needed if we eventually ditched ws2_32.dll right?
<andrewrk> right
<andrewrk> lithdew pointed out ws2_32.dll might be the best short term path, with AFD something to look into in the future
<andrewrk> another way to design a cross platform abstraction would be if you had to create a "Networking" instance to pass around
<marler8997> I wonder if there are other platforms that would need something like that?
<marler8997> UEFI actually might be able to use that
<andrewrk> the abstraction is still leaky with WSAStartup though, since it's global
<andrewrk> one thing that comes to mind is globally ignoring the SIGPIPE it is that happens when a client disconnects from a server
<andrewrk> it's the same problem, global state the OS has rudely forced upon the application
<marler8997> what's the leaky problem with WSAStartup?
<andrewrk> you can't contain it to a Networking instance. if you deinitialized one, it would also mess up the others
<marler8997> WSAStartup does maintain a reference count
<andrewrk> I see
<marler8997> double checking...
<marler8997> > The WSAStartup function typically leads to protocol-specific helper DLLs being loaded. As a result, the WSAStartup function should not be called from the DllMain function in a application DLL.
<marler8997> scratch the DllStartup idea
<marler8997> > An application must call the WSACleanup function for every successful time the WSAStartup function is called. This means, for example, that if an application calls WSAStartup three times, it must call WSACleanup three times. The first two calls to WSACleanup do nothing except decrement an internal counter; the final WSACleanup call for the task does all necessary resource deallocation for the task.
<marler8997> yup, like I remembered
<mq32> marler8997: DllMain() would be an option here…
<mq32> you get PROCESS_ATTACH and PROCESS_DETACH
<mq32> but it's also the most horrible hack
<marler8997> right, but you can't call WSAStartup inside DllMain
<mq32> and will not make those wonderful single-file executables
<marler8997> you can't load other Dlls inside DllMain, and WSAStartup can load other Dlls
<mq32> hand → face
<mq32> okay, going to bed. just remove networking, nobody needs this
<marler8997> yeah, I got bit by that one years ago, there's tons of restrictions on DllMain
<marler8997> WSAStartup would still be leaky though because it's global (not thread local). So any thread that calls it and changes the version would affect all other threads within the same process
pretty_dumm_guy has quit [Quit: WeeChat 3.2-dev]
Guest43271 has quit [Ping timeout: 268 seconds]
chivay has quit [Quit: RIP]
chivay has joined #zig
notzmv has joined #zig