ChanServ changed the topic of #zig to: zig programming language | | be excellent to each other | channel logs:
francis36012 has joined #zig
zachcarter has joined #zig
litonico has joined #zig
<litonico> hello! anyone up for fielding some zig/c interop questions?
<andrewrk> hi litonico
<litonico> hi
<litonico> first, when I include a c header in zig with @cInclude and @cImport, is there a way to see what C compiler command runs? (I'm on OSX, and trying to figure out why zig's C import paths aren't finding particular system-level headers)
<andrewrk> litonico, good question. there is not currently, but now that you mention it, I think it would be a good idea to have --verbose-cimport print that information, which would be here:
oconnor0 has quit [Quit: Page closed]
<litonico> great, thank you so much!
<andrewrk> litonico, I pushed a commit to master to enable this. you can build zig from source or wait 2 hours for the CI server to update the download page with a new static build
<litonico> amazing, thank you
<litonico> my second question is: does zig have a syntax similar to doublequote #includes? @cInclude generates angle-brace #includes, so I've been setting C_INCLUDE_PATH to include a header in the current directory. Is this the way to do it?
<litonico> (for a command-line build, haven't yet gotten in to using build.zig)
<andrewrk> do you know about the -isystem command line arg to zig?
<litonico> I did NOT know about -isystem
<andrewrk> for some reason I never added `-I` but -isystem is kinda the same thing
<litonico> yep, that does the thing
litonico has quit [Quit: Page closed]
<knebulae> andrewrk: any idea why io.zig would get pulled in with only a single call to a c function? that's what's firing off this posix variable and compiler error.
<knebulae> I guess a better question might be why std is getting brought in at all.
Ichorio has quit [Ping timeout: 245 seconds]
diltsman has joined #zig
<andrewrk> knebulae, to be clear zig has lazy top level declarations, so the standard library, or even individual files, are not all pulled in together
<andrewrk> if you push your code to a branch and upload it somewhere, I'll take a look
<daurnimator> andrewrk: any thoughts on my import suggestion above?
<scientes> nvm
<daurnimator> scientes: "The uploader has not made this video available in your country."
<scientes> stupid
francis36012 has quit [Ping timeout: 240 seconds]
IntoxicatedHippo has joined #zig
return0e_ has joined #zig
return0e has quit [Ping timeout: 246 seconds]
knebulae has quit [Read error: Connection reset by peer]
meheleventyone has joined #zig
meheleventyone has quit [Remote host closed the connection]
meheleventyone has joined #zig
meheleventyone has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
meheleventyone has joined #zig
meheleventyone has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
IntoxicatedHippo has quit [Ping timeout: 244 seconds]
meheleventyone has joined #zig
meheleventyone has quit [Client Quit]
meheleventyone has joined #zig
meheleventyone has quit [Client Quit]
steveno has joined #zig
zachcarter has quit [Ping timeout: 244 seconds]
unique_id has joined #zig
<unique_id> Imagine if the C++ standard library weren't documented and you had to learn it by reading the impossible task! Compare that to the Zig standard library which is super easy to learn by reading the code. It tells you Zig is doing something right :)
knebulae has joined #zig
unique_id has quit [Quit: Leaving]
Ichorio has joined #zig
hooo has quit [Quit: Connection closed for inactivity]
hooo has joined #zig
<MajorLag> andrewrk, re: #1851 it looks like `@bytesToSlice` is also unaware of the alignment vs size problem.
steveno has quit [Ping timeout: 240 seconds]
steveno has joined #zig
diltsman has quit [Ping timeout: 256 seconds]
<andrewrk> MajorLag, good catch, and thanks for the writeup
<andrewrk> daurnimator, what problem are you trying to solve with your import suggestion?
THFKA4 has joined #zig
wilsonk has quit [Read error: No route to host]
<MajorLag> andrewrk: my branch didn't run integration with Azure on this latest push. Is that expected for some reason?
spazzpp2 has joined #zig
<spazzpp2> hi
<spazzpp2> where can I find an explainer about std.fmt?
<spazzpp2> or do you just look up the code"
<spazzpp2> ?
<andrewrk> MajorLag, I don't know why that happens
<andrewrk> spazzpp2, you have to look up the code for now. You can track this issue for updates:
<MajorLag> disappointing, but I was only going to use it as a shortcut to testing my changes that require the build environment to exist. I've been planning to set up my own build environment because I inherited some old servers, but I haven't made the time yet.
wilsonk has joined #zig
<spazzpp2> Do you have an example for std.fmt.format ?
<andrewrk> spazzpp2, search that file for `test "fmt.format"`
<andrewrk> those tests might be helpful examples
<spazzpp2> andrewrk, good idea
steveno has quit [Ping timeout: 250 seconds]
steshaw has quit [Quit: Connection closed for inactivity]
<spazzpp2> is a context for std.fmt.format simply any struct with a .remaining []u8?
<andrewrk> spazzpp2, are you sure you need std.fmt.format? maybe you want std.fmt.allocPrint or std.debug.warn
<andrewrk> or std.fmt.bufPrint
<andrewrk> or you want to turn a std.os.File into a and then use the print method
<spazzpp2> andrewrk, actually I want to write an i32 to stdout
<andrewrk> const stdout = &(try;
<andrewrk> stdout.print("x={}\n", x);
<mgxm> andrewrk: a little more progress
steveno has joined #zig
<andrewrk> mgxm, looking good
<andrewrk> interesting, it crashed when cross compiling std lib tests for linux
<mgxm> yes, SEGV_MAPERR
<andrewrk> looks like you have the behavioral tests passing in --release-fast mode though?
<andrewrk> and the self-hosted compiler building successfully, nice
<spazzpp2> andrewrk, `var stdout = try; try stdout.outStream().stream.print("{}\n", 0);` worked
<andrewrk> mgxm, I just looked at your fork of zig fbsd2 branch. looks good. I'll have a few minor suggestions but overall excellent work
<andrewrk> mgxm, btw I don't know if you are aware, but if you use `zig build --help` it prints "steps" which are equivalent of makefile targets
<andrewrk> and there are about 10 different test categories you can select manually
<andrewrk> `test` runs them all
return0e_ has quit [Read error: Connection reset by peer]
return0e has joined #zig
wilsonk has quit [Read error: No route to host]
steveno_ has joined #zig
<mgxm> yeah, I didn't know that until some time ago :D
<mgxm> thank you
wilsonk has joined #zig
steveno has quit [Ping timeout: 250 seconds]
steveno_ has quit [Quit: Leaving]
DutchGh0st has joined #zig
<DutchGh0st> hi!
<andrewrk> hi DutchGh0st
<DutchGh0st> how's advent of code going? haha
<spazzpp2> DutchGh0st, I started today :)
<DutchGh0st> ohh! day 1!
<spazzpp2> DutchGh0st, I trouble around with simple IO
<DutchGh0st> IO?
<DutchGh0st> didn't need IO for day 1
<spazzpp2> reading lines from stdin and parsing to int
<DutchGh0st> in zit you can use @empedFile(...) to insert a constant array of bytes into your programm
<DutchGh0st> *in zig
<spazzpp2> DutchGh0st, I already solved 1 with a bash one-liner
<DutchGh0st> ohhh, reading the lines from stdin, there is a function for reading from stdin...
<spazzpp2> But I want the basic unix program: stdio
<DutchGh0st> hold on
<spazzpp2> readLineSlice()
<spazzpp2> DutchGh0st, ^
<DutchGh0st> ha! yes!
<spazzpp2> But I need a buffer for that
<DutchGh0st> yeahh, and it returns the slice of the buffer it has written to
<DutchGh0st> you need a Bytes buffer iirc?
<DutchGh0st> not a []const u8, but the struct BytesBuffer or whatever its called
<spazzpp2> First I need an Allocator
<DutchGh0st> std.hap.DirectAllocator.init()
<DutchGh0st> *heap
<spazzpp2> Whats the diff to a FixedBufferAllocator?
<DutchGh0st> which is a thin wrapper around mmap and munmap
<DutchGh0st> I dont know how the FixedBufferAllocator works
<DutchGh0st> I only really use the direct allocator, wrapped in an arena allocator :3
<DutchGh0st> soo you only free at the end, not in between\
<spazzpp2> I'll stay close to that guess_number program for it is my first zig-program
<DutchGh0st> ohh I see
<DutchGh0st> is the FixedBufferAllocator in std.heap?
<spazzpp2> DutchGh0st, yes
<DutchGh0st> yeah, oh, it takes a slice of bytes, interesting
<DutchGh0st> wait so you can stack allocate with it? o.O
<spazzpp2> my slice is a const []u8 = undefined
<DutchGh0st> what is the length of it?
<DutchGh0st> because a slice is really just a ' view' of memory,
<DutchGh0st> so you cant create just a slice from nothing and expect it to have a size,
<spazzpp2> I think it's len=0 because it's undefined
<DutchGh0st> const buf: [10]u8 = undefined; <--- this is allowed however
<DutchGh0st> because that is just an array with 10 elements,
<spazzpp2> I didn't give it a length
<spazzpp2> but I'm still unfinished
<DutchGh0st> try giving it a length :)
<spazzpp2> ..erm working on it
<spazzpp2> it expects []u8 rather than [20]u8
<DutchGh0st> pass &buf
<DutchGh0st> that'll conver to a slice
<MajorLag> slices are a kind of pointer, they mean nothing if they don't point to something. If you have an array (and it isn't const), you need to slice it to pass it as a slice.
<DutchGh0st> *convert
<DutchGh0st> the cool thing about slices, is that they dont care if they point to an array, or a heap array
<spazzpp2> it works now, if I give it a tailing \n in the stdin
<DutchGh0st> so the arrayList in std as a function, toSlice(), which gives you a slice to the memory :)
<spazzpp2> I like slices, too
<DutchGh0st> MajorLag: , how do I a saturating subtraction?
<DutchGh0st> such that 0usize - 100usize equals 0
<MajorLag> There's an issue for adding saturating operators or builtins, but as far as I'm aware there isn't any support for that currently. Let me check something...
<DutchGh0st> and the std ArrayList doesn't have a .last() method? that would be nice to havee
<DutchGh0st> instead of a weird - 1)....
<MajorLag> list.items[list.items.len - 1] should work I think.
<DutchGh0st> yeah, but .last() would handle overflows
<DutchGh0st> now I gotta do it myself :3
<DutchGh0st> .last() should return a ?T, where null indicates there was no last item
<MajorLag> you could use .pop() and then immediately append it back.
<MajorLag> there's a popOrNull for the behavior you want, except that it removes the last item from the list obviously.
<DutchGh0st> :(
<MajorLag> var x: usize = undefined; if(@subWithOverflow(usize, 100, 0, &x)) x = 0;
<MajorLag> You can always make a PR to add last(). A lot of the standard library is just written as it comes up for people.
<DutchGh0st> I would like that function yeah, will do
hooo has quit [Quit: Connection closed for inactivity]
<andrewrk> spazzpp2, if you get stuck, there are a bunch of us who have our zig advent of code open source
<andrewrk> I think nearly everyone here for example:
<DutchGh0st> how do I clear an arraylist?
<DutchGh0st> like, set the internal length to 0 again, but keep the current capacity
<spazzpp2> Here's my solution to the first exercise, what do you think?:
<spazzpp2> andrewrk, what's our join code?
<DutchGh0st> you never used the allocator xD
<MajorLag> DutchGh0st: the easiest way that comes to mind is `list.len = 0;`
<DutchGh0st> oh really? you can just assign to that length?
<DutchGh0st> fine! \o/
<MajorLag> zig doesn't have a concept of private fields yet. there may be a function for it, I didn't check
<MajorLag> but yeah, all ArrayLists will have `items`, which is a slice of the available capacity and `len` which is the currently used capacity.
<andrewrk> spazzpp2, your allocator is initialized with an undefined slice. you want something like var bytes: [1024]u8 = undefined;
<spazzpp2> andrewrk, but the compiler complains
<andrewrk> as it stands you are invoking undefined behavior, and zig doesn't have runtime safety checks for branching on undefined yet
<andrewrk> spazzpp2, happy to help if you show the error message you got
<MajorLag> probably it is complaining because he is passing the array instead of passing a slice of the array?
<andrewrk> that was my guess too
<spazzpp2> andrewrk, 1.zig:6:57: error: expected type '[]u8', found '[20]u8'
<MajorLag> `const allocator = &std.heap.FixedBufferAllocator.init(BYTES[0..]).allocator;`
<andrewrk> you can solve that by taking the address of the array, or with slice syntax as MajorLag shows
<MajorLag> so yeah, the alternative: `const allocator = &std.heap.FixedBufferAllocator.init(&BYTES).allocator;`
<MajorLag> A slice is a pointer, using it in an undefined state is a bad idea.
<MajorLag> if it points to undefined data though, that's fine.
<andrewrk> spazzpp2, also, your `catch` blocks are fine, looks good, but just for your education, I would encourage you to experiment with using `try` instead of `catch` for the parseInt case, and try making the input have an invalid number. see what happens and see how it helps you debug it
<andrewrk> DutchGh0st, I gave you a compliment on the stream yesterday
<andrewrk> oh I think you saw it :)
<spazzpp2> andrewrk, did you see my compile error message?
<andrewrk> spazzpp2, yes, see MajorLag's comment above, and mine as well
<andrewrk> we correctly guessed the compile error before you pasted it :)
<DutchGh0st> I saw it yeah! thank you :)
<DutchGh0st> I really enjoy to see some video's of advent of code in zig :), I think you're the only one so far, andrewrk
<spazzpp2> andrewrk, why did it work with an invalid array then?
<andrewrk> spazzpp2, it looks like you didn't use the allocator
<andrewrk> if you did it would probably return error.OutOfMemory, because currently (but not after an issue is resolved) it is typical of undefined values to be zeroes
<spazzpp2> I see
<DutchGh0st> , that was my day 1 part 1
<DutchGh0st> oh and MajorLag, setting the ArrayList's length to 0 works, but what if the arraylist contained arraylists as items?
<DutchGh0st> then we just ' cleared' the arraylist, without deinitting all lists it contains
<DutchGh0st> and that leaks
<DutchGh0st> or is that more an issue of the programmer?
hooo has joined #zig
<spazzpp2> we should use these programs to document zig lib by example
<hooo> I think public/private is an unnecessary thing to add to a system language
<hooo> convince me otherwise guys
<andrewrk> DutchGh0st, the official way to change the length of std.ArrayList is shrink() or resize()
<andrewrk> hooo, I don't think I can even convince myself
<DutchGh0st> but does that deinit its contents, if needed?
<andrewrk> no, you need deinit() to free an arraylist resources
<MajorLag> I am not convinced public/private is a useful notion, so I'm afraid I can't convince you hooo.
<spazzpp2> DutchGh0st, embedFile is a funny hack
<MajorLag> andrewrk, doesn't shrink call shirnk on the allocator though? he wants to keep the capacity. maybe I remember incorrectly. Besides, no method of ArrayList can be aware that it needs to deinit its children.
<spazzpp2> DutchGh0st, but doesn't that embedfile demand pretty much RAM compared to a stream?
<DutchGh0st> ehh
<DutchGh0st> not for advent of code
<DutchGh0st> the inputs are relatively small
<spazzpp2> theoretically
<DutchGh0st> yes, a stream would be more memory friendly
<DutchGh0st> however not as fast, I think
<andrewrk> MajorLag, currently it does not, but it makes no guarantees. I think we need more functions that specify intent
<MajorLag> maybe it is resize that callse allocator.shrink.
<andrewrk> MajorLag, looks like nothing calls allocator.shrink in ArrayList except for toOwnedSlice
<DutchGh0st> in Rust I use a fancy annotation, in which I write something like this: `#[aoc(2018, 19, 2)] fn main(input: &str) -> ...`, which will automatically download the inputs, stores them into a file, and then reads the whole file into 1 string
<DutchGh0st> sooo thats as memory friendly as @embedFile, but at runtime
<andrewrk> ...which is arguably not the best behavior. The semantics of ArrayList could use some scrutiny
<DutchGh0st> and, if you use a stream, you can never solve a problem at comptime :3
<MajorLag> You know, we could add a field to array list that specifies a function to call on each item before it is removed. That would be useful for the ArrayList(ArrayList(T)) case, among a few others.
<spazzpp2> andrewrk, InvalidCharacter could print that char for debugging maybe.
<andrewrk> spazzpp2, that's a tricky problem because the only thing it does in the context where it knows the character is return an error code
<MajorLag> Well the whole of std needs some going over, but my understanding is that that's a problem we're saving for near 1.0.0.
<andrewrk> MajorLag, yeah that's right. although that's not to say we can't make improvements along the way
<DutchGh0st> yeahh that would solve the issue of nested resources
<DutchGh0st> but gotta go! keep up the vids andrewrk! Have a nice day / evening all! :)
DutchGh0st has quit [Quit: - A hand crafted IRC client]
<MajorLag> Since we were talking about allocators, that reminded me that I threw together an incomplete first draft of an 'intro to manual memory management in Zig' document. It is likely full of errors:
<andrewrk> oh neat!
<spazzpp2> cool
<spazzpp2> andrewrk, can I convert a char to it's ord(c) like in python?
<andrewrk> what type do you have?
<andrewrk> `char` isn't a zig type
<spazzpp2> a cast from u8 to i8 maybe
<andrewrk> if you have a u8 or i8, I'm guessing it's already the ordinal representation
<andrewrk> are you trying to print it?
<andrewrk> {c}
<spazzpp2> I want to use it as an array index.
<spazzpp2> (aoc day 2)
<andrewrk> you can use a u8 as an array index
<MajorLag> In Zig, `@typeOf('c') == u8`
<spazzpp2> oh really? no type restrictions??
<spazzpp2> myarray['c'] would work??
<MajorLag> assuming myarray was large enough, yes.
<spazzpp2> there's std.fmt.charToDigit
<andrewrk> spazzpp2, that's a utility function used by parseInt. you probably don't need charToDigit
<MajorLag> oh interesting, I'm not correct in saying `@typeOf('c') == u8`. It would be `comptime_int` as a literal.
<spazzpp2> true.. returns u8.. still need to learn reading that syntax
<spazzpp2> andrewrk, however, can I cast from u8 to i8?
<MajorLag> `@intCast(i8, my_u8);`
<MajorLag> which in debug modes will saftey check that your value can be stored in an i8.
<MajorLag> if you want to treat your u8 value as an i8 at the bit level: `@bitCast(i8, my_u8);`
<MajorLag> *debug and safe modes.
<andrewrk> spazzpp2, do you really need a signed integer? why can it be negative?
<spazzpp2> andrewrk, lol.... thought u8 stands for unicode
<spazzpp2> I think I need a break
<spazzpp2> andrewrk, does leaving the scope care for some kind of garbage collection?
<spazzpp2> hm, I'll use defer to be sure :)
<andrewrk> spazzpp2, zig has no hidden code execution
<andrewrk> you can always tell exactly what code will run by looking in the function that you're in
<spazzpp2> great
<spazzpp2> andrewrk, how do you init an empty [255]u8 (zero-filled)?
<andrewrk> var data = [1]u8{0} ** 255;
<andrewrk> don't you want 256?
<spazzpp2> ok 256
<spazzpp2> very expressive
<spazzpp2> good night!
spazzpp2 has quit [Remote host closed the connection]
porky11 has joined #zig
porky11 has quit [Ping timeout: 260 seconds]
francis36012 has joined #zig