ChanServ changed the topic of #zig to: zig programming language | ziglang.org | be excellent to each other | channel logs: https://irclog.whitequark.org/zig/
atk has quit [Quit: Well this is unexpected.]
atk has joined #zig
<andrewrk> bb010g, I've never tried building stage 3
<andrewrk> stage 2 isn't complete so we can't build stage 3 yet
<andrewrk> the most stage 2 does is `zig fmt`
<darithorn> is there a way to make sure that a function i've written is done at compile time?
<andrewrk> darithorn, at the callsite, you can use `comptime`
<darithorn> ah okay i just wasn't sure how strict it was yet. a lot is available at compile time then! :D
<bb010g> andrewrk: Ok. It probably wouldn't hurt to note that in the readme. :)
<andrewrk> bb010g, good idea
<darithorn> is there a reason you can't assign a variable of type 'type' even though it's comptime? i try to assign it after initialization and i get "attempt to dereference non pointer type 'type'"
sherjilozair has quit [Quit: Leaving...]
<MajorLag2> darithorn: possibly it's #740? https://github.com/zig-lang/zig/issues/740
<MajorLag2> nope, nevermind
<andrewrk> darithorn, this is https://github.com/zig-lang/zig/issues/588
<andrewrk> I'm working on this issue as we speak
darithorn has quit [Quit: Leaving]
darithorn has joined #zig
donpdonp has joined #zig
<donpdonp> greetings, zigonauts.
<donpdonp> im trying to take the first step with using a C library from zig.
<donpdonp> I believe the code is fine, I can run it with $ zig build-exe src/main.zig --library c and everything is happy. what I cant figure out is what to put in build.zig so that $ zig build will produce the same output
<andrewrk> hi donpdonp. yeah apologies for the lack of documentation
<andrewrk> donpdonp, here's an example: https://github.com/andrewrk/tetris/blob/master/build.zig
<andrewrk> I'm guessing exe.linkSystemLibrary("c"); is what you were missing
<donpdonp> andrewrk: thx! I found it by dgging into the zig code
<donpdonp> for fun i thought id try connecting zig to https://github.com/andlabs/libui#readme
<andrewrk> ah neat, I was reading about that earlier today
<donpdonp> what would i use to add a path for finding these system libraries?
<donpdonp> ive got libui in a local folder so linkSystemLibrary("ui") isnt finding it
<donpdonp> oh here it is: b.addLibPath("../libui/build/out");
<donpdonp> and naturally, the next thing would be: b.addCIncludePath("../libui");
<donpdonp> ui.h has "typedef struct uiInitOptions uiInitOptions; struct uiInitOptions { size_t Size; };
<donpdonp> given const ui = @cImport({ @cInclude("ui.h"); });, how would I define a zig variable of type uiInitOption?
<andrewrk> var x: ui.uiInitOption = undefined;
<andrewrk> I would recommend doing all your C imports in 1 @cImport block and naming the namespace `c`
<donpdonp> andrewrk: that var declaration worked. wow. thx. zig++
darithorn has quit [Remote host closed the connection]
davr0s has joined #zig
<donpdonp> warn("ui init sizeof is {}\n", @sizeOf(ui.uiInitOptions)); => main.zig:26:34: error: compiler bug: integer and float literals in var args function must be casted.
<donpdonp> o^O
<andrewrk> donpdonp, you can work around like this: usize(@sizeOf(ui.uiInitOptions))
<andrewrk> you can also print that valueu at compile time:
<andrewrk> @compileLog(@sizeOf(ui.uiInitOptions));
<donpdonp> woah thats gonna be my new best friend :)
<donpdonp> @compileLog(@sizeOf(ui.struct_uiInitOptions));
<donpdonp> | 8
<donpdonp> /home/donp/code/zig/hello/src/main.zig:26:3: error: found compile log statement
<donpdonp> @compileLog(@sizeOf(ui.struct_uiInitOptions));
<donpdonp> haha. it worked then said hey you cant use compile log! :)
<andrewrk> yep, that's to prevent a working build from having messy output
<donpdonp> o^O
<donpdonp> im guessing that has to do with build.zig: const mode = b.standardReleaseOptions();
<andrewrk> look at the output of `zig build --help` with and without b.standardReleaseOptions()
<donpdonp> are you saying the output of 'zig build --help' changes depending on whats in the local build.zig ??
<donpdonp> im not seeing anything obvious from the output about release options or compilelog
<andrewrk> yes, zig build is declarative
<andrewrk> I need to add ReleaseSmall to these docs
<donpdonp> const mode = b.smallReleaseOptions(); => no member named 'smallReleaseOptions' in struct 'Builder'
<donpdonp> is that a post 0.20 feature?
<donpdonp> 0.2.0
<donpdonp> if so, whats the flag to allow compileLog in 0.2.0 without it being an aerror
<donpdonp> here's another one that comes from the docs but doesnt compile. trying to set the bytes of the struct to 0 with: for (o[0..o_size]) |*b| b.* = 0; -> error: expected token 'Symbol', found '*'
<donpdonp> for (o[0..o_size]) |*b| b.* = 0;
<donpdonp> ^
<donpdonp> sorry for the barrage of questions :)
davr0s has quit [Quit: My MacBook Pro has gone to sleep. ZZZzzz…]
<andrewrk> donpdonp, you can find the documentation for 0.2.0 here: https://ziglang.org/documentation/0.2.0/
<andrewrk> however I suggest tracking master branch
<andrewrk> I'm going to sleep. have a good night, I will check for questions tomorrow
davr0s has joined #zig
<donpdonp> goo dnight
alexnask has joined #zig
<donpdonp> what format characters does zig like?
<donpdonp> both {h} and {%h} fail (for hex chars). cant find docs on it
davr0s has quit [Quit: My MacBook Pro has gone to sleep. ZZZzzz…]
Ichorio has joined #zig
davr0s has joined #zig
quc has joined #zig
Hejsil has joined #zig
<Hejsil> donpdonp, {x} for hex :)
* donpdonp slaps forehead
<donpdonp> warn("hex {x}", 0x123a); error: compiler bug: integer and float literals in var args
<donpdonp> should i be using zig master-branch?
davr0s has quit [Quit: My MacBook Pro has gone to sleep. ZZZzzz…]
<donpdonp> warn("hex {x}", usize(0x123a)); works as expected though im not sure what usize() is doing
<Hejsil> Zig has the concept of a compile time int (and float). These are bigints, and cannot exists in the final binary. They cast to all integer types that they fit into
<Hejsil> For now, you can't pass comptime ints to varargs functions
<Hejsil> usize() is a cast
<donpdonp> Hejsil: can i use "{}" for a function that return a c-string
<donpdonp> or rather, how would I? I get "u8@7fab84312fc8"
<Hejsil> You could do std.debug.warn("{}", std.cstr.toSliceConst(c"Hello World!\n"))
<Hejsil> I don't think there is any way to specify in the format string that your '&const u8' is a c strings
<Hejsil> I think https://github.com/zig-lang/zig/issues/770 will make this possible though
<donpdonp> std.cstr.toSliceConst(my_cstr)) => expected type '&const u8', found '?&const u8'
<donpdonp> how would i convert a "nullable" str pointer to a zig-friendly str pointer
<donpdonp> @ptrCast(&const u8, nullable_u8_ptr); works. yea zig for letting me do the quick and dirty thing
Hejsil has quit [Ping timeout: 260 seconds]
Hejsil_ has joined #zig
<Hejsil_> donpdonp, you probably wonna unwrap the nullable pointer instead :)
<Hejsil_> like this: ??nullable_ptr
<donpdonp> o^O
<Hejsil_> Or: nullable_ptr ?? something_else
<donpdonp> i see. that is more zig-ish. ok thx.
<donpdonp> zig is a pretty wonderful language.
Hejsil_ has quit [Ping timeout: 260 seconds]
Hejsil has joined #zig
<donpdonp> ok so how do i go from a '[]const u8' to a c-string?
<donpdonp> ive tried addNullByte(allocator, str) w/o success
<Hejsil> It depends
<Hejsil> Do you know that you're string have a null terminating byte?
<Hejsil> If soo, then you can just do slice.ptr
<Hejsil> If not, then do const s = addNullByte(allocator, slice); useS(s.ptr);
<donpdonp> well im using curl(c"http://") works fine. func(url: []const u8) { curl(url) } dies
<donpdonp> where func("http://") is called (not a c"")
<Hejsil> c"" gives you &u8, while "" gives []u8
<Hejsil> Aka, "" gives you a slice which is a ptr and a length
<Hejsil> "" won
<Hejsil> "" wont have a null terminated byte
<Hejsil> Because you're suppose to use s.len to figure out the end
<Hejsil> c"" will have a null terminated byte, so you just get a pointer to the start of the string
<donpdonp> where does 'allocator' come from
<Hejsil> If you want to append an extra charactor to your string (the null terminator in this case) you have to allocate (on the heap or somewhere else)
<Hejsil> In Zig, the way you allocate memory is by passing around allocators
<donpdonp> okay so how i get an allocator
<Hejsil> For debugging, you can use std.debug.global_allocator
<donpdonp> var allocator = std.debug.global_allocator;
<donpdonp> var cstr = std.cstr.addNullByte(allocator, url);
<donpdonp> curl(url) still fails :( (where c"url-here" works)
<donpdonp> curl(cstr) I mean
<Hejsil> cstr.ptr and it should work
<donpdonp> c.curl_easy_setopt(curl, c.CURLOPT_URL, cstr.ptr); => error: type '@typeOf(addNullByte).ReturnType.ErrorSet![]u8' does not support field access
<Hejsil> Aaah
<Hejsil> Right
<Hejsil> addNullByte can fail
<Hejsil> So you have to catch the error
<Hejsil> Either by passing it up
<Hejsil> try cstr.addNullByte(allocator, slice)
<Hejsil> Or by handeling it
<donpdonp> ug. allocating a single byte shouldnt require exception handling :)
<Hejsil> cstr.addNullByte(allocator, slice) catch |err| { handle error here };
<donpdonp> thx for the syntax. what is 'slice'
<Hejsil> 'slice' is just your string
<donpdonp> ohh
<Hejsil> The section on error handling :) https://ziglang.org/documentation/0.2.0/#Errors
<donpdonp> wait how is cstr declared
<Hejsil> What do you mean?
<donpdonp> cstr.addNullByte implies cstr already exists
<donpdonp> but im trying to create cstr
<Hejsil> Ooh, cstr is the namespace 'cstr' in the zig standard library
<donpdonp> i cant say var cstr = cstr.addNullByte(..) :
<Hejsil> std.cstr.addNullByte
<donpdonp> ohh
<donpdonp> well that what I was doing
<donpdonp> btut.. not catching the err
<donpdonp> /home/donp/code/zig/zigdeck/src/net.zig:15:53: error: incompatible types: 'void' and '[]u8'
<donpdonp> var cstr = std.cstr.addNullByte(allocator, url) catch |err| { warn("{}",err); };
<donpdonp> ^
<Hejsil> Right
<Hejsil> catch is a binary operator
<Hejsil> It returns the left hand side when no error is there
<Hejsil> and the right hand side if there was an error
<Hejsil> A block '{}' returns void by default
<Hejsil> You could return from the block, and the block returns 'noreturn' instead
<Hejsil> the 'noreturn' type casts to any type
<Hejsil> because a result will never be returned from a 'noreturn' expression
<Hejsil> var cstr = std.cstr.addNullByte(allocator, url) catch |err| { warn("{}",err); return; }; will work
<donpdonp> ug. i guess im too tired to grok this
* donpdonp ries that
<Hejsil> Well, if you're all new to Zig, and want to use a C lib, then there are some things to get used to :)
<donpdonp> warn("alloc {}",err); never sends output
<donpdonp> ie it doesnt fail
<donpdonp> thx for the help, i apprecieate it
<Hejsil> Np
<donpdonp> i need to sign off before i do any more damange to this code
<donpdonp> the project is here, in case you wanted to see it, https://nest.pijul.com/donpdonp/zigdeck
alexnask has quit [Quit: Leaving]
darithorn has joined #zig
darithorn_ has joined #zig
darithorn has quit [Ping timeout: 268 seconds]
Ichorio has quit [Ping timeout: 260 seconds]
davr0s has joined #zig
Ichorio has joined #zig
davr0s has quit [Quit: My MacBook Pro has gone to sleep. ZZZzzz…]
davr0s has joined #zig
<donpdonp> omg this is the hardest part of this whole project
<donpdonp> going from a zigstr to a cstr.
<donpdonp> anyone have an example code?
<donpdonp> var cstr: []u8 = std.cstr.addNullByte(&allocator.allocator, url); -> error: expected type '[]u8', found '@typeOf(addNullByte).ReturnType.ErrorSet![]u8'
<donpdonp> https://ziglang.org/documentation/master/#Memory is blank. <sad trombone>
<Hejsil> donpdonp, did you read the docs on errors?
<donpdonp> apparently noy
<donpdonp> not
<donpdonp> var cstr = try std.cstr.addNullByte(&allocator.allocator, url);
<donpdonp> error: expected type 'void', found '@typeOf(addNullByte).ReturnType.ErrorSet'
<donpdonp> nothing makest sense
<MajorLag2> you called try in a function that doesn't return errors
<MajorLag2> 'void' in this case is referring to the return type of the function. The error message is a little unhelpful on this one.
<donpdonp> i just want var str="me" to take the place of curl(c"me") so I can curl(str)
<donpdonp> seems like one utility function and I should be done.
<donpdonp> even c++ makes this fairly easy.
<MajorLag2> `try` is equivelent to `expresion catch |err| return err`.
<donpdonp> the !value as a union is another interesting language feature.
<donpdonp> ive used functional langs that have a Type T *or* Error type.
<donpdonp> but im frustrated cause i just want to get going on this app and not learn a 40pg chapter on error handling :)
<MajorLag2> If you want to just ignore any errors, you can use `expression catch unreachable`. In debug builds this will assert if there is an error.
<MajorLag2> `var cstr = std.cstr.addNullByte(&allocator.allocator, url) catch unreachable;`
<donpdonp> its not throwing an error
<donpdonp> its returnign sometihng bogus
<donpdonp> var url="http://abc" i believe is coming back as c"http://abcgarbagefd<0>" because curl doesnt like the url
<donpdonp> which I would look at with warn "{}" but the substitution codes arent published
<MajorLag2> if you're trying to view a cstring with warn: `warn(std.cstring.toSliceConst(the_c_string));` should work.
<MajorLag2> sorry, that's `std.cstr.toSliceConst()`
<donpdonp> std.cstr.toSliceConst(cstr) -> expected type '&const u8', found '@typeOf(addNullByte).ReturnType.ErrorSet![]u8'
<donpdonp> here's the bits that are causing trouble https://gist.github.com/donpdonp/74ebce153602f692e3ea469d17b914d8
<MajorLag2> you didn't unwrap the error from addNullByte. `var cstr = std.cstr.addNullByte(allocator, url);` => `var cstr = std.cstr.addNullByte(allocator, url) catch unreachable;`
<donpdonp> i added it, the result is unchanged
<Hejsil> That's because addNullByte returns []u8 and not &u8
<Hejsil> Use cstr.ptr
<MajorLag2> I guess that makes sense, since you'd need a slice to free it later, though I wouldn't have expected that either to be honest.
davr0s has quit [Quit: My MacBook Pro has gone to sleep. ZZZzzz…]
davr0s has joined #zig
<donpdonp> Hejsil: yup the catch unreachable, and using cstr.ptr together is working.
<donpdonp> thx.
sherjilozair has joined #zig
steveno_ has joined #zig
<andrewrk> donpdonp, it's understandable, you just want to get something working
<andrewrk> here's my recommendation:
<andrewrk> put `try` in front of every function that can fail
<andrewrk> put `!` in front of all your return types
<andrewrk> and now you don't really have to think about error handling too much
<sherjilozair> Hey andrewrk, do you have a roadmap towards 1.0 somewhere?
<andrewrk> sherjilozair, it's encoded in github milestones
<andrewrk> every issue has a milestone attached to it
steveno_ has quit [Quit: Leaving]
<sherjilozair> andrewrk: Any plans on adding UFCS?
<sherjilozair> I see. So the answer is no. Slightly disappointed, but I still like the clear focus on being a low-level magic-free language.
sherjilozair has quit [Quit: Leaving...]
Dodo has joined #zig
Ichorio has quit [Ping timeout: 245 seconds]
bowman has joined #zig
bowman has quit [Quit: Page closed]
Dodo has quit [Quit: Page closed]
monteslu_ is now known as monteslu
Hejsil has quit [Ping timeout: 260 seconds]
davr0s has quit [Quit: My MacBook Pro has gone to sleep. ZZZzzz…]
darithorn_ has quit [Ping timeout: 260 seconds]
darithorn_ has joined #zig
davr0s has joined #zig
darithorn has joined #zig
alandipert has quit [Ping timeout: 256 seconds]
alandipert has joined #zig
atk has quit [Quit: Well this is unexpected.]
atk has joined #zig
atk has quit [Remote host closed the connection]
atk has joined #zig
darithorn_ has quit [Quit: bye]
isd has joined #zig
isd has quit [Quit: Leaving.]
davr0s has quit [Quit: My MacBook Pro has gone to sleep. ZZZzzz…]