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/
<sm2n_> what's the idiomatic way to assert some invariant at at the function call boundary? Am I correct in assuming that you should return an error type in that case?
ryan_greenblatt has quit [Quit: Connection closed]
<dok> Hi there
<dok> quick question: Is there an equivalent to scanf or strtoll in zig ?
ryan_greenblatt has joined #zig
<ugla> dok: std.fmt.parseInt
<dok> ugla: uh ! thanks !
<dok> my brain is probably too much used to think about weird libc names, that I could have never though to look at "parseX"
<ugla> :)
elusive has joined #zig
<spacelove> I ask this having checked the documentation: what is the syntax for "for x in range(n)"?
elusive has quit [Client Quit]
dongcarl has joined #zig
elusive has joined #zig
<dok> spacelove: using a while ? https://ziglearn.org/chapter-1/#while
<spacelove> Thanks, so to confirm there is no "for [..]" syntax?
<spacelove> I can do with a while ofc.
<dok> spacelove:I don't know, I am learning
<spacelove> Ah o.
<spacelove> ok*.
<dok> But I think `for` loops are only for slice and arrays
<spacelove> Sure, I guess what I mean is, is there a way to quickly say: "array from 0 to n" or something like that.
<spacelove> Like [0..n], so that you'd do "for ([0..n]) |x| {}" etc.
<ryan_greenblatt> TLDR no
<ryan_greenblatt> See also https://github.com/ziglang/zig/issues/358 spacelove
<spacelove> How is it done usually? Seems like the most common pattern in the world. Using a while loop?
<ryan_greenblatt> Yep
<spacelove> Whoah
<ryan_greenblatt> standard is:
<ryan_greenblatt> ```
<ryan_greenblatt> var i: usize = a;
<ryan_greenblatt> while (i < b) : (i += 1) {}
<ryan_greenblatt> ```
<ryan_greenblatt> consider also https://github.com/ziglang/zig/issues/358#issuecomment-551321048 if your feeling spicy
<ryan_greenblatt> *you're
<spacelove> I'm always feeling spicy
decentpenguin has quit [Ping timeout: 245 seconds]
ryan_greenblatt has quit [Quit: Connection closed]
<andrewrk> that's the spirit!
<noam> Maybe change your diet?
<noam> :)
decentpenguin has joined #zig
neptunepink has quit [Ping timeout: 264 seconds]
<spacelove> Guys, I built something!!!!!
<spacelove> This finds the minimum edit distance between two words <3
<spacelove> So cool!
<spacelove> It runs, if you're feeling spicy you can let me know if it's good code or whatever. Somehow I feel that x and y are backwards on my matrix and I'm not sure why... but it works! Muahahaha.
neptunepink has joined #zig
<andrewrk> spacelove, check out https://zigbin.io/ :D somebody from the discord made it
<spacelove> WOW!!!
earnestly has quit [Ping timeout: 240 seconds]
happyalu has joined #zig
<noam> freaking comptime.
<noam> `struct call_graph runtime, comptime;` ← it's apparently more sensible to build two call graphs for zig
<noam> :P
<andrewrk> noam, the zig compiler got a lot simpler when I introduced IR instead of trying to go straight from AST
<noam> andrewrk: this *is* at the IR level :P
<noam> It's useful information for the comptime engine and for optimizations
<andrewrk> ah, call graph, nvm :)
<noam> yep
<andrewrk> where's our code for rendering something as a string literal syntax?
<noam> bright side: i think I can tack on a comptime engine that's good enough for duck typing in like ten minutes of effort once the call graphis done :D
<andrewrk> looks like std.fmt.fmtSliceEscapeLower only does \xNN
<andrewrk> ah there it is: std.zig.fmtEscapes
<noam> for every element in the comptime call graph of a function, invoke the comptime engine to produce the result, append the result - properly typed and everything! - to the node list, and return its index
spacelove has quit [Quit: Connection closed]
lilulo has joined #zig
* noam sighs
<noam> > call_graph.c:46: error: TODO call graph analysis of calls depends on identifier resolution
<noam> okay, so I'm also doing scoping rules now :P
happyalu has quit [Ping timeout: 240 seconds]
osa1 has quit [Ping timeout: 256 seconds]
lilulo has quit [Quit: Connection closed]
cole-h has joined #zig
<noam> Done today so far: call graph generation (comptime+runtime), arm64 codegen, test suite, function call parsing...
<noam> almost able to compile my first bootloader test file :P
<noam> hopefully by midnight :P
<noam> heehee, string interning means checking if something is in scope is blazing fast :)
ave_ has quit [Remote host closed the connection]
lunamn has quit [Remote host closed the connection]
linuxgemini has quit [Write error: Connection reset by peer]
sm2n_ is now known as sm2n
elusive has quit [Remote host closed the connection]
dyeplexer has joined #zig
<noam> Need to work on error messages, butttttt... > ERROR("Identifier '%s' shadows another!", buf)
<noam> :D
<noam> Minor issue that some commits are apparently dated 51 years ago because the system clock was wrong lol
osa1 has joined #zig
<andrewrk> ifreund, g-w1: I'm done for the day. this test case is passing: `export fn _start() noreturn {}` (it correctly reports a compile error)
yyp has joined #zig
<andrewrk> next up is hello world which hits a few TODOs :)
<noam> Nice :)
<noam> Just finished scoping, just need to analyze all functions present on the call graph instead of direct exports and I should have the first testpassing :D
<andrewrk> noam, the big change in this branch is the overhaul of source locations so that and error messages survive incremental compilation and still report the correct location even when the decl gets moved around
<noam> See, this is why I'm still pretty sure I'll have zyg done months before stage2: I do so much less that I can make progress much faster even with the occasional slump in activity :P
<noam> Today was a very productive day for zyg :)
<noam> Even if git disagrees XD
osa1 has quit [Remote host closed the connection]
cole-h has quit [Ping timeout: 264 seconds]
earnestly has joined #zig
<noam> Got the first rk bootloader test passing :D
<noam> It's really nothing much, just start() noreturn calling an infinite_loop()noreturn function, but that's still much more than was working earlier :)
<noam> Now to make it more fun, and make the infinite_loop call deferred :)
bitmapper has quit [Quit: Connection closed for inactivity]
sord937 has joined #zig
kenran has joined #zig
nycex has quit [Remote host closed the connection]
nycex has joined #zig
euantorano_ has joined #zig
euantorano has quit [Ping timeout: 240 seconds]
euantorano_ is now known as euantorano
yyp has quit [Ping timeout: 246 seconds]
yyp has joined #zig
yyp has quit [Quit: now it's safe to turn off your computer]
yyp has joined #zig
Kuraitou_ has joined #zig
<Kuraitou_> can someone explain why x is 000... here? https://godbolt.org/z/3a9qjn
<yyp> Kuraitou_: maybe it should be comptime?
<ikskuh> good question…
<Kuraitou_> I tried sprinkling comptime in various places but I can't seem to get it to work
<Kuraitou_> it's always 000...
<ikskuh> Kuraitou_: i feel like you've found some weird compiler bug
<ikskuh> yep, that's a bug
<noam> Defer working :D
<noam> (though it's currently only correct for function-scope without extra control flow, it can easily be fixed once I can actually handle more :P)
<yyp> Btw, it I use defer in a loop, will it get triggered before next iteration?
<yyp> s/it/if
<noam> i believe so
<yyp> It doesn't
<ikskuh> yyp: defer will always trigger at the end of *scope*
<ikskuh> { defer y(); x(); }
<ikskuh> x will always be called before y
<yyp> Doesn't work even if you put it in inner scope: while (true) { { /* code */ } }
<ikskuh> can you share an example?
<ikskuh> works for me in over a thousand places :D
<dutchie> https://0x0.st/-NUB.zig does what i'd expect it to
<dutchie> prints "starting loop 0" "ending loop 0" "starting loop 1" etc
<ikskuh> dutchie: that code doesn't need defer though :D
<dutchie> well no, but it's a sanity check to show they run each loop iteration
<yyp> nevermind
<yyp> I forgot about sleep at the end
<ikskuh> looks perfectly fine
<yyp> Yep, now it works
<yyp> I put sleep in that scope which meant that mutex is never released, my bad
TheLemonMan has joined #zig
<ikskuh> it got released, but not for long :D
<yyp> Yes, for one tick
<yyp> Which was not enough for another thread to pick it up
<Kuraitou_> ikskuh yyp: thanks for having a look, I filed an issue
FLOKLI is now known as flokli
<yyp> Are there any usable SSL/TLS libraries for Zig besides zig-bearssl?
<ikskuh> iguanaTLS
<yyp> Can you give a link?
<yyp> Thanks!
<TheLemonMan> time for zig-boarssl
<dutchie> all zig tls libs must be named after animals
<dutchie> new rule
<yyp> Interesting, why did nobody make openssl bindings?
<ikskuh> maybe because of OpenSSL? :D
<yyp> I guess I'll vendor it :)
<yyp> OpenSSL is the only thing I trust in production
<TheLemonMan> there's also in-kernel TLS if you're feeling frisky
<yyp> I could have used socat for SSL <-> unix socket, and then connect to it with std.net :)
<TheLemonMan> that'd be the unix way™
<yyp> That's actually what suckless guys think is the best way to use TLS and that's terrible
<earnestly> TheLemonMan: I bristle! Sockets were a blight put upon unix by Berkley students :(
<earnestly> Real unix would have used bi-directional pipes (which they did)
<earnestly> Berkeley*
<TheLemonMan> heh
<TheLemonMan> well if you squint hard enough sockets are bidirectional pipes
<TheLemonMan> for some definition of pipe
<earnestly> TheLemonMan: What plan9port does: https://0x0.st/-NUl.c
<earnestly> (also what a slug)
<TheLemonMan> oh I've seen that trick in quite a few places
<noam> plan9 has namespace entries ("virtual files", but not really) for network access; tls simply wraps them
ave_ has joined #zig
lunamn has joined #zig
linuxgemini has joined #zig
<dok> yyp: can you explain why this is terrible ?
<yyp> What is terrible?
<dok> > I could have used socat for SSL <-> unix socket, and then connect to it with std.net
<dok> > That's actually what suckless guys think is the best way to use TLS and that's terrible
<ikskuh> because the TLS is in a second process
<yyp> Because you need to learn how to use an entire extra program just to connect your stupid small program
<ikskuh> if the TLS library has a vuln, only this single TLS connection is fucked, but you cannot gain access to any other data
<dok> ikskuh: isn't that a good thing ?
<ikskuh> dok: the problem is that it's a horrible way of doing things
<ikskuh> even if it's the *good* way
<dok> yeah maybe using a socat is a bit ugly but using a program that does tls -> unix socket and then use that socket doesn't sound bad to me.
<dok> but I am not an network expert
<ikskuh> dok: true
<dok> quite the oposite
<dok> yeah maybe it's gets ugly if the tls connect closes but not the socket, or even if the socket closes: the program will need to restart a new socat process...
<yyp> Hmmm, ArrayList has a memory leak?
<yyp> testing.Allocator borked at me for using arraylist.append despite that I deinit it in the end: https://termbin.com/52at
<yyp> Here's the code: https://termbin.com/tdjhj
<ikskuh> yyp: you have to free MessagePrefix.nick
<ikskuh> with the same allocator
<ikskuh> are you doing this?
mikdusan has quit [Quit: WeeChat 3.0.1]
<yyp> Ooops, I don't
<yyp> Thanks for pointing out!
<TheLemonMan> speaking of networking, join ikskuh and me in #7844 and check if the proposed API suits your use-case
<yyp> Looks like I would have to rewrite all of my networking code if this gets merged..
<s-ol> is there any pattern for .init() declarations that need a pointer to themselves?
<s-ol> e.g. when registering with an external C api that needs a "cookie" void* for callbacks
<s-ol> do I make the init signature `pub fn init(allocator: Allocator, ...) !*@This()` and allocate "self" inside init?
<s-ol> that doesnt allow using it inside of parent structs though.
<yyp> s-ol: that sounds like a reasonable thing to do
<s-ol> Alternatively I can add a second `pub fn postInit(self: *@This(), ...)` that does the rest of the setup once the struct is "in place"
<s-ol> is there a pattern like this in the stdlib already somewhere?
<TheLemonMan> the latter looks better as it allows to place @This() on stack
<s-ol> yeah thats the benefit there
<s-ol> or a third option: `pub fn init(self: *@This(), ...) void` - let the "owner" allocate w/ undefined contents wherever and then call init on it
<s-ol> went with that for now
<yyp> TheLemonMan: new networking API looks good, I like the new structure with TcpClient and stuff
<ikskuh> TheLemonMan: when the new API is established, the innards need some overhauling. daurnimator proposed that stdlib should use happy eyeballs to connect to both ipv6 and ipv4 simultaneously
<TheLemonMan> indeed, that use-case is taken into account in connectToAddress accepting a slice of endpoints
<noam> Ha! Finally got the annoying scoping rules working properly :D
<yyp> Is it possible to have Reader over a string (aka `[]const u8`)?
<yyp> nevermind, found io.fixedBufferStream
lilulo has joined #zig
<yyp> Is there a way to make a function accept ANY io.Reader regardless of what it holds
<yyp> ?
<g-w1> anytype as the type
<yyp> Ok, that worked, thanks!
Kuraitou_ has quit [Quit: Leaving]
craigo has joined #zig
yyp has quit [Quit: now it's safe to turn off your computer]
wootehfoot has joined #zig
lunamn has quit [Remote host closed the connection]
ave_ has quit [Remote host closed the connection]
linuxgemini has quit [Read error: Connection reset by peer]
CommunistWolf has quit [Remote host closed the connection]
CommunistWolf has joined #zig
<marler8997> TheLemonMan, why a Socket struct instead of using os.socket_t directly?
<TheLemonMan> because everything must be opaque
<TheLemonMan> in case we want to change the inner representation
<TheLemonMan> and what the fuck is a socket_t
wootehfoot has quit [Quit: Leaving]
<marler8997> isn't socket_t just as opaque as Socket?
<marler8997> you could replace socket_t with anything as well right?
wootehfoot has joined #zig
<marler8997> is it that you're saying socket_t is a socket handle for platforms that support such a thing, and Socket is a socket object that may or may not be a handle, it could be anything?
<TheLemonMan> I don't know where the socket_t comes from, the _t suffix makes it sound part of some C API but there's no such thing afaics
<TheLemonMan> you, the user, should not care of what's inside Socket
<marler8997> I added socket_t :)
<TheLemonMan> today there's a file descriptor
<TheLemonMan> got you!
<marler8997> The fact that a socket is a handle/file descriptor is required knowledge to use it with certain APIS, such as select/epoll/kqueue/IOChannels
<marler8997> so, will there be a Socket.asFileDescriptor() or something?
<TheLemonMan> that'd be wrong on windows, where ws2 sockets are not file descriptors
<marler8997> some API's actually take an array of socket handles, how would that work?
<marler8997> on windows they are Handles
<TheLemonMan> I'd just leave the handle there with no extra accessors
<TheLemonMan> and Socket lets us stuff more fields without breaking everything
<marler8997> So on linux, can the user assume that Socket is just a handle?
<marler8997> so if they pass an array of Socket to a function, then it can be case to an array of file descriptors?
<marler8997> *cast
<TheLemonMan> yep
<marler8997> when would someone use Socket vs socket_t?
<marler8997> are you saying we should remove socket_t?
<TheLemonMan> yep
<marler8997> how would you expect people to pass a Socket to a function that accepts a file descriptor?
<marler8997> cast?
<TheLemonMan> no, socket.handle
<TheLemonMan> that's it
<TheLemonMan> no casts, no anything
<Nypsie> It's just a field inside the Socket struct
<marler8997> and an array of file descriptors?
<TheLemonMan> that doesn't make sense?
<marler8997> why?
<TheLemonMan> it's a single handle
<TheLemonMan> sure, you can consider that as an array with just one element
<marler8997> I have an array of Socket
<marler8997> and I want to pass my array to a function that accepts an array of file descriptors
<marler8997> how to do?
<TheLemonMan> don't
<TheLemonMan> tbh I didn't think of that use case
<marler8997> well, that's kinda why we have socket_t and/or fd_t
<marler8997> the fact that socket_t is an fd_t is important for the type system to know
<TheLemonMan> socket_t is not always a fd_t
<marler8997> it's platform dependent, just like the APIs
<TheLemonMan> you're the Windows developer, you should know
<marler8997> if you're on a platform where a socket is a file descripts, then you want to be able to pass socket_t to functions that accept fd_t
<TheLemonMan> that's why there's an handle in Socket...
<TheLemonMan> and fd_t nor socket_t follow zig's convention of PascalCase types
<marler8997> I'm fine changing the casing to Socket
<marler8997> what I'm talking about is Socket = extern struct { ...} vs Socket = fd_t
<Nypsie> Having it wrapped inside a struct also allows for declarations such as readers
<Nypsie> marler8997: Does this mean you also want something such as File = fd_t?
<marler8997> Nypsie, nope, we have fd_t
<marler8997> TheLemonMan is proposing we remove socket_t
<TheLemonMan> no?
<marler8997> > are you saying we should remove socket_t? > yep
<TheLemonMan> and replace it with a type with proper casing
<marler8997> socket, Socket = fd_t on linux?
<marler8997> of Socket = extern struct { handle: fd_t }?
<TheLemonMan> I need a break from this discussion
<marler8997> ok no probs
<TheLemonMan> let's try again, leave socket_t alone.
<TheLemonMan> Socket is a simple abstraction to let us upgrade the type without breaking the API compatibility with previous versions
<TheLemonMan> Socket.handle is a socket_t. full stop.
<TheLemonMan> no, you can't pass an array of Socket to socketpair
<TheLemonMan> that's done on purpose (and because I didn't think that'd be useful)
<marler8997> I've seen people create wrappers for Socket too many times and it always seems to make things more complicated
<marler8997> It makes sense when you're only working with 1 of them, but once you start to get into multiplexing and real-world applications then tend to break down I find
<TheLemonMan> that's why I opened that PR, collecting opinions
<marler8997> opening a PR for networking is one good way to get some opinions out of me :)
spacelove has joined #zig
<TheLemonMan> at this point I don't know if that's a good thing or not heh
<spacelove> I'm trying to read a file, but getting an error that "open" takes new parameters "perm" and "flags." What those parameters are is not in the docs. Any tips? My code: https://zigbin.io/9d234c
<spacelove> I'm simply trying to read in data line by line.
<g-w1> use std.fs.cwd().readFile
<marler8997> I'm not really sure either, but I do know that introducing a wrapper has some problems and I'm just trying to make everyone aware of them
<spacelove> g-w1 thanks.
<TheLemonMan> ok fuck that Socket thing, the type is not exposed to anything outside of net
<marler8997> here's the docs for "open" as well: https://pubs.opengroup.org/onlinepubs/007904875/functions/open.html
<TheLemonMan> you've focused so much on that single type and didn't notice where it's used
<marler8997> it's not exposed to anything outside of net? what do you mean? it's pub
craigo has quit [Read error: Connection reset by peer]
<TheLemonMan> it's only being used in the *Client types
<spacelove> Is there example code floating around for how to open a file? I've found some examples online but none of them work with my version of zig...
<g-w1> whats ur version
<TheLemonMan> spacelove, the stdlib is full of examples
<g-w1> spacelove: are you using master, 0.7.1, ... ?
<spacelove> 0.8-dev
<spacelove> I downloaded it so I could practice with ziglings, which required it.
<g-w1> that should help you
<spacelove> g-w1Yeah I have that open right now.
<spacelove> If that's considered simple.. I'm wondering if I'm getting in over my head.
<g-w1> hmm, the std.fs.cwd().readFile{_, Alloc} should do it
<g-w1> std.fs.cwd().openFile
<Nypsie> Plenty of examples in the tests: https://github.com/ziglang/zig/blob/master/lib/std/fs/test.zig
<spacelove> g-w1 Thanks, will try.
mikdusan has joined #zig
<spacelove> This works: https://zigbin.io/a62f13
<spacelove> Thanks for the help.
<spacelove> Thanks for the help.
<spacelove> Shoot! I always hit up + enter lmao. Sorry.
<Nypsie> `defer` means it executes the expression right of the keyword at the end of current scope
<Nypsie> Meaning that after calling `readFile()` it will close your file when it leaves the scope by either an error or the return expression
cole-h has joined #zig
<spacelove> Wow, that's really cool
<spacelove> It's like something which turns the stove off automatically in case I forget
<Gliptic> only if you remember to set the timer
<spacelove> Why doesn't this work? Replacing the array length with file_size instead of 1024*4: https://zigbin.io/e5cb7d Error is "error: unable to evaluate constant expression"
<g-w1> arrays must have a comptime known length
<g-w1> readFileAlloc for variable length
<spacelove> Assume I am 5 years old. Where can I learn about this at a simple level.
<spacelove> I think I get it actually
<spacelove> So, the compiler needs to know the length of the array at compile time. Which it doesn't, because it needs to run the program to get the array length because... it has to run "readFile" or whatever.
<g-w1> ziglearn.org
<spacelove> Mamma mia.
<g-w1> top of this page https://ziglearn.org/chapter-2/
<spacelove> I think you might be overestimating how readable that is to someone seeing it for the first time :-)
<spacelove> I mean that in the kindest way possible. Curse of knowledge and all that
<TheLemonMan> you know the difference between stack and heap?
<spacelove> Sort of, not really. Heap is accessed during execution time, or something, maybe
<Gliptic> so is stack, that's not the difference
<Gliptic> the difference is in the allocation pattern, stack has to be deallocated in the opposite order from how it was allocated
<Gliptic> as the name suggests
<spacelove> I see. LIFO
<Gliptic> yes
<spacelove> I knew that, somewhere down beneath the baseboards of my brain is a piece of paper with that written on it
<spacelove> I'm not a compsci dude, I did physics, but never had to use a computer
<spacelove> Except Mathematica. And python
<TheLemonMan> no Fortran?
<spacelove> So I'm missing a lot of concepts I think, to get started. But I'm trying
<Gliptic> although that fact alone doesn't mean you couldn't allocate something of dynamic size on the stack, later versions of C allows that for instance
<spacelove> Nah. I used C when I did high energy but that was just for a minute, for simulating in GEANT
<Gliptic> but zig makes the choice not to allow that for various reasons
<spacelove> Ok
<Gliptic> one being that the stack isn't very large and the allocation could easily fail
<spacelove> Got it
<spacelove> So the way to read a file into memory is to choose a variable length array std.ArrayList, and this memory is allocated on the heap
<spacelove> Is that right? Do I sound like I know what I'm talking about
<spacelove> Let me try it before answering
<TheLemonMan> that's one way of solving the problem
zie has joined #zig
<TheLemonMan> but if you open lib/std/io/reader.zig you may find something even easier
<g-w1> just std.fs.cwd().readFileAlloc
<TheLemonMan> the solution in itself is not important, learning how to get to the solution is what matters
<g-w1> that makes sense
yyp has joined #zig
yyp has quit [Quit: now it's safe to turn off your computer]
yyp has joined #zig
<g-w1> andrewrk: just so there is no duplicated work, im taking on astgen if/condbr stuff
moo has joined #zig
<spacelove> I've been trying for about an hour to get this to work: https://zigbin.io/1d2ecf
<spacelove> I'm kind of lost.
wootehfoot has quit [Ping timeout: 240 seconds]
sepples has joined #zig
moo has quit [Client Quit]
<spacelove> The best I can do is a successful compile with an error: fish: 'zig run word_chains.zig' terminated by signal SIGSEGV (Address boundary error) which suggests something about how I'm allocating the memory
<spacelove> But I really don't know at this point
<TheLemonMan> readFileAlloc allocates the memory for you
<TheLemonMan> you only have to pass a valid allocator pointer to it, right now you're giving it garbage `= undefined`
<spacelove> I need to initialize the pointer somehow
<dutchie> there are allocators in the standard library, see https://ziglearn.org/chapter-2/#allocators for some examples
<TheLemonMan> yes, but putting garbage there won't get you anywhere
<spacelove> Guys, I appreciate the help and I'm not trying to sound ungrateful, but I know where the docs are and I'm finding them confusing, that's why I'm here
<dutchie> normally you can just put `var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); const allocator = &gpa.allocator;` at the top of main
<andrewrk> zig showtime is live, starting in 10 minutes: https://www.twitch.tv/kristoff_it topic is an interview ith Abner Coimbre from the Handmade community, followed by show & tell
<spacelove> using std.heap.page_allocator as the allocator works, thanks.
<TheLemonMan> spend some time understanding _why_ it works
<spacelove> My only guess is that it's a heap vs stack thing. Is there a way to learn Zig without knowing C first? I'm feeling like in order to understand what's in the Zig docs, I've had to read the C docs.
<TheLemonMan> *hint* it's not a heap vs stack thing
<TheLemonMan> the allocator parameter is telling the function you're calling how to ask for memory
<TheLemonMan> initializing that pointer to undefined gave you a nice crash
<TheLemonMan> initializing it to something meaningful, an allocator instance, gave you the expected result
<dutchie> you have to learn how pointers work, i guess. which is also necessary for c, and zig's are quite similar in a lot of ways
<spacelove> I know the basics of pointers, but I thought when TheLemonMan said "allocator pointer" he meant a pointer to an allocator
<spacelove> But I guess allocators are already of a pointer type, or something.
<TheLemonMan> allocators are structure types, nothing less and nothing more
<TheLemonMan> there's no difference between a *i32 and a *mem.Allocator beside the type they point to
<andrewrk> spacelove, one way to think about it is that every variable is represented in memory at some particular address. even pointers. so when you set something to undefined, or to a value, try to imagine exactly which bytes of memory are getting set
<andrewrk> and when you think of pointers, try to think of two sets of bytes: one is the bytes of the pointer itself, and one is the bytes the pointer points to
<spacelove> Right, address/house analogy is what I keep in mind.
<andrewrk> but the address is also a house :)
<andrewrk> it's houses all the way down
<spacelove> Right. @TheLemonMan what did you mean by pointer to an allocator then? If you can pass the allocator directly as a variable.
<spacelove> or "allocator pointer" rather.
<TheLemonMan> read the prototype for `readFileAlloc`
<TheLemonMan> it's expecting a `*mem.Allocator` as first parameter
<TheLemonMan> std.heap.page_allocator is of type `*mem.Allocator`
<spacelove> Jesus.
<spacelove> Well so, then saying that the allocator was already of a pointer type... wasn't that for off.
<spacelove> Maybe I used the wrong words or whatever, but that's what I was asking.
<spacelove> Anyway, thanks for the help, I have a lot more to read I guess.
<spacelove> I managed to print the lines in a file which was a huge success.
<ugla> spacelove: You could also have used an allocator that's backed by a fixed-size array on the stack, for example. Doesn't have to be heap. You get to choose, by passing in the allocator. In C you typically don't get to choose, so less to think about, but less flexibility.
<TheLemonMan> it's a bit more nuanced than that, the page allocator is already instantiated and you only have to get a pointer to it
<TheLemonMan> if you use a GeneralPurposeAllocator, as dutchie suggested, the initialization is a bit different
<TheLemonMan> but there you can see how the Allocator (with capital A) is instantiated and how you "extract" a pointer out of it
<spacelove> I'll try it dutchie's way as well then.
<dutchie> the point is you have to choose an allocator explicitly from the standard library. you can't just pass undefined in, zig wants you to be explicit
<dutchie> (or you could always define your own, if you really want/need to)
<andrewrk> as a general rule of thumb, avoid `undefined`
<spacelove> I think Zig might be above my level right now honestly.
<txdv> how many days have you been trying zig out?
dyeplexer has quit [Remote host closed the connection]
<andrewrk> spacelove, don't give up :) you're learning how computers actually work, at the low level. it will be worth it!
Akuli has joined #zig
notzmv has quit [Ping timeout: 260 seconds]
craigo has joined #zig
Guest66423 has joined #zig
Guest66423 has quit [Remote host closed the connection]
notzmv- has joined #zig
notzmv- is now known as notzmv
gazler__ has quit [Quit: Leaving]
<spacelove> If data is an ArrayList, is the idiomatic way of reading lines into the array list using "file.reader().readUntilDelimiterArrayList(data, buffer,'\n',);" if buffer is the file data? Or do you loop over the lines using a while loop
<spacelove> And if it is a while loop, I thought this would work, but it does not: while (file.reader().readUntilDelimiterOrEof(buffer,'\n') != null)
<spacelove> Since, from the docs, readUntil...Eof returns null if the end of file is reached.
<andrewrk> spacelove, I recommend to find some example code that uses this - I think probably some Advent of Code repositories will have an example
<spacelove> Andrew, man, I love you. You are a genius. But please trust that I looked.
<spacelove> I even found a reddit post on that but it linked to a github repo that had been deleted. lmao
<spacelove> About AoC
<andrewrk> ah bummer
<spacelove> I was even taking notes of my process of trying to solve this in case someone asked... just to give a flavor
<noam> andrewrk: you mentioned that I should focus on behavior tests. Is the best way to do that to implement `zig test` and start running it manually on stdlib/stage2? That seems like a hassle, but I'm not sure Ican think of anything better long-term
<ugla> spacelove: it can also return an error I think. So you could do `while (try readUntil...()) |line| { … }` (without the != null)
<andrewrk> spacelove, if your data is in an array list, probably the most convenient is std.mem.tokenize or std.mem.split on the arraylist.items
<spacelove> Thanks.
nycex has quit [Remote host closed the connection]
nycex has joined #zig
TheLemonMan has quit [Quit: "It's now safe to turn off your computer."]
kenran has quit [Ping timeout: 264 seconds]
kenran has joined #zig
<spacelove> andrewk that solution works, ugla that one does not seem to, for me. Thanks for the help. Now I am onto putting this stuff into an array of strings.
bitmapper has joined #zig
<spacelove> I am a genius. I figured it out in the shower.
<ikskuh> 👍
jcmdln has left #zig ["User left"]
nvmd has joined #zig
wootehfoot has joined #zig
moo has joined #zig
wootehfoot has quit [Ping timeout: 264 seconds]
<v0idify> An Internet ETSes out of the Rust Evangelism Strike Force and enlists in the Zig Foreign Legion. In the comments section, Hackernews divide up and argue about which sets of compiler abstractions are necessary to support human life. thanks n-gate
spacelove has quit [Quit: Connection closed]
<andrewrk> v0idify, let's just keep it zig coding focused in here, no spicy topics plz (because I am a lazy moderator)
<andrewrk> I hear spicy topics are allowed on the discord if you want ;)
<v0idify> hehe sorry i didn't realize it could be spicy, it's all fun and games for me
* noam hesitantly looks
<v0idify> i don't know much about n-gate, i see how it can be... controversial. sorry
<andrewrk> v0idify, no worries, you didn't do anything wrong
moo has quit [Quit: Leaving]
sord937 has quit [Quit: sord937]
<noam> v0idify: it's deliberately written to mock basically everybody XD
<v0idify> yeah, and i have bad taste so i like it most of the time
<earnestly> Oh thanks for reminding me about ngate
<v0idify> then again it's asshole-y
wootehfoot has joined #zig
<noam> v0idify: i love it, it's great; I was pointing out that it's deliberately *intended* to piss people off and so probably isn't something thatbelongs in this "be excellent to each other" space
<noam> Language bashing is for the discord, not here! /s
<v0idify> that's why i was apologising
fputs has quit [Quit: WeeChat 3.1]
marler8997 has quit [Quit: Leaving]
<apotheon> v0idify: I find n-gate amusing sometimes, but yeah, I could see how some might find it a bit uncomfortable.
lunamn has joined #zig
ave_ has joined #zig
linuxgemini has joined #zig
vincent_ has joined #zig
vincent_ has quit [Client Quit]
wootehfoot has quit [Quit: Leaving]
Sphax has joined #zig
spacelove has joined #zig
<spacelove> I am trying to generate a 2d array at runtime -- wondering what is wrong with this: var matrix = std.ArrayList([] u8).init(allocator); // matrix.append({0,0});
<spacelove> Basically, an array of zeros of width x_dim and height y_dim.
<noam> That's an arraylist of slices
<spacelove> I need to allocate the memory beforehand, and then somehow slice it down to the right length. I think I get that much.
<noam> Do you intend constant dimensions?
<noam> e.g. NxM?
<noam> Or should each line be variable-width?
<spacelove> Constant dimensions, but the dimensions will change with each function call.
<g-w1> spacelove: for the append, you want matrix.append(&.{0,0});
<noam> uhh
<noam> That's... going to go wrong, if you return from the current scope
<spacelove> So, in other words, I'm calling it multiple times, each time it generates a new matrix.
<g-w1> then you dont want an arraylist, but just allocator.alloc([n]u8, m)
<noam> ^
<spacelove> I'm trying to compute the minimal edit distance between strings, and I do it using a matrix calculation.
<noam> ArrayList would be if dimensions change
<noam> If dimensions are static, you want an array / slice
<spacelove> allocator.alloc([8]u8, 5) returns a 5x8 array? For example
<noam> Yeah
<noam> IIRC at least
<spacelove> That is mindblowing.
<spacelove> Amazing.
<noam> It returns [N]T, and you give it T,N IIRC
<noam> So [5][8]u8
<g-w1> if you want the second dimension to be runtime known you must alloc that and have an array of slices for example allocator.alloc([]u8, 5)
<g-w1> multidimensional_array[0] = allocator.alloc(u8, 7)
<spacelove> Is this in the docs somewhere? I don't want to break your guys' boxes, but I want to learn more
<g-w1> https://ziglang.org/documentation/master/#Multidimensional-Arrays kind of, but only with comptime known length
<spacelove> Yea I started with that
<g-w1> but if you know how to allocate a regular array, just extend it to make an allocated arrays of allocated arrays!
yyp has quit [Quit: now it's safe to turn off your computer]
kenran has quit [Quit: leaving]
lilulo has quit [Quit: Connection closed]
<spacelove> So I need to learn how to allocate a regular array then
<g-w1> allocator.alloc(T, n) returns a []T, where the len is n
<v0idify> how do i get a slice from an array? for some reason the type is different
<g-w1> &array
<v0idify> fs.path.join(allocator, &.{ path orelse &.{} });
<v0idify> doesn't work
<g-w1> well really a *[N]T coerces to []T where len is N
<g-w1> thats probably a stage1 bug
<g-w1> i would make path orelse &.{} a seperate variable and then put it there
<g-w1> maybe even use explicit types: path orelse [_]u8 {} if stage1 does not contain the power
<v0idify> i already tried doing explicit types
<g-w1> maybe one is const and the other is not?
<g-w1> without an error, I cant help you
<spacelove> g-w1 something like https://zigbin.io/5e6daf ?
<g-w1> almost :)
<spacelove> Ahahaha
<g-w1> var mat = try allocator.alloc([]u8, x_dim);
<spacelove> It hurts
<spacelove> My brain
<g-w1> you want it to be an array of slices, not an array of u8s
<g-w1> nice job!
<spacelove> It didn't like your suggestion
<spacelove> error: expected type '[]u8', found 'std.mem.Allocator.Error![]u8'
<g-w1> put a try on the allocation
<g-w1> because allocations can return errors if you run out of ram
<spacelove> mamma mia *kiss on the forehead*
<spacelove> It doesn't work now but at least it compiles. So smart, such a smart boy
<spacelove> I will let you be the first to use my program g-w1
<spacelove> When it's done
<spacelove> I'll give you first dibs when I make an NFT out of it
<g-w1> lmao
<andrewrk> g-w1, mind fixing up #8307 real quick so I can merge it?
<andrewrk> looks good other than one little oopsie
Akuli has quit [Quit: Leaving]
<spacelove> gw-1 I have basically the whole thing here: https://zigbin.io/14fcb2 but there's an issue with memory allocation. I've allocated (presumably) enough memory for each array, yet SIGSEGV doesn't like it apparently. I've marked the line that throws the issue, in case you want to look
nvmd has quit [Quit: Later nerds.]
<g-w1> seems like a logic error to me
<g-w1> andrewrk: sure thing
<g-w1> i will delete the token starts to make sure future bugs are harder
<spacelove> Wait don't tell me
<spacelove> I think I can get it
<spacelove> I think it's the memory allocation part
<spacelove> Works now <3 https://zigbin.io/4a2f2e
<g-w1> andrewrk: done
<spacelove> I beat you
<andrewrk> g-w1, thanks!
<andrewrk> g-w1, I'm working on function calls
<g-w1> yeah i would def say distinct types are needed
<g-w1> ok nice
<g-w1> for CondBr s, it has then_body_len and else_body_len. it says these follow after the cond instruction (extra Data), does this mean I should turn this line `.instructions = try block_scope.arena.dupe(zir.Inst.Ref, block_scope.instructions.items),` into gen_zir.addAll(block_scope.instructions.items) or something? I am a little confused on what to do there
<noam> +1 for distinct types
<andrewrk> g-w1, addFnType and addFnTypeCc are examples of how to implement addCondBr
<g-w1> oh, thanks
<g-w1> ah, didn't see because in Module
<g-w1> ohh I see
squeek502 has joined #zig
skuzzymiglet has quit [Read error: Connection reset by peer]
skuzzymiglet has joined #zig
<spacelove> What would be the returntype of this function, if not ArrayLIst?
LewisGaul has joined #zig
<g-w1> its an ArrayList(Node)
<g-w1> in zig functions can take types and return types
<spacelove> Ty
<spacelove> ./word_chains.zig:68:18: error: use of undeclared identifier 'ArrayList'
<spacelove> fn buildGraph() !ArrayList(Node) { <-- same error
<g-w1> std.ArrayList(Node)
<spacelove> How are you so smart
<LewisGaul> Just spent ages narrowing down what looks like potentially a weird bug relating to reporting of a memory leak, does this look weird to others too? https://gist.github.com/LewisGaul/c0a2ff38607f55ab589700ada452f7c4
<g-w1> I have become a ziguana
<g-w1> LewisGaul: why does it look weird to you?
<LewisGaul> why does the first testcase give a memory leak error but not the other two?
<g-w1> just a guess, maybe cuz you copy hthe arena?
<LewisGaul> that wouldn't explain difference between tests 1 and 2 would it?
<g-w1> does look weird at second thought yeah