ChanServ changed the topic of #zig to: zig programming language | ziglang.org | be excellent to each other | channel logs: https://irclog.whitequark.org/zig/
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, 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?
<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 code...an 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
<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 std.io.OutStream and then use the print method
<spazzpp2>
andrewrk, actually I want to write an i32 to stdout
<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 ArrayList.at(list.len - 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>
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
<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! :)
<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: https://gist.github.com/tgschultz/5eb221c3d3ff781ee683809d5fcacf33
<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)?