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/
<kandinski> I see
<kandinski> so maybe the solution is to have an ._errorbuffer field and a var errormessage declaration with the slice
<kandinski> I like this pattern
<jayschwa> Personally, (without knowing more context), I would have a buffer field and a length field. Then create the slice only at the point where you need it (e.g. const slice = struct_val.buffer[0..struct_val.len]). If the slice is also a field on the struct, and an instance of the struct is copied, the copy's slice pointer will still be to the original
<jayschwa> struct buffer, not the copy's buffer.
<kandinski> jayschwa: so a pub struct function is what I need
<kandinski> foo.errormessage()
<kandinski> jayschwa: the context is that I'm wrapping pcre2 (for now, just enough to have a nice API for myself, I'm not writing a library for public consumption)
<kandinski> so I'm doing a lot of passing out buffers to C for things like error messages
<kandinski> but I want the consuming code to be clean and ziggish
<kandinski> buffer/length are required because that's what pcre2.get_error_message(&buffer) returns: length as the return value and it writes the bytes in the buffer.
<kandinski> given this context, would you say implementing it so it can be consumed as `message: []u8 = re_matcher.errormessage()` is the ziggish approach?
<kandinski> (going forward, the solution would be this one: https://github.com/ziglang/zig/issues/2647, but that's not available now)
tundrax has quit [Remote host closed the connection]
<jayschwa> Looks reasonable to me
<kandinski> ta. I'm not only new to Zig but not a C programmer. I'm only learning enough C to understand memory models etc when interfacing. So I appreciate any advice not only about the technology, but about conventions and design.
<kandinski> (in this case, I also learnt about the dangers of copying structs with slices as members)
nullheroes has joined #zig
a92 has joined #zig
cole-h has quit [Ping timeout: 246 seconds]
a_chou has joined #zig
squeek502 has quit [Remote host closed the connection]
frmdstryr has joined #zig
jj__ has joined #zig
ryuukk_ has joined #zig
jjsullivan1 has quit [Ping timeout: 240 seconds]
earnestly has quit [Ping timeout: 260 seconds]
supercoven_ has joined #zig
supercoven has quit [Ping timeout: 240 seconds]
a_chou has quit [Ping timeout: 260 seconds]
Ondingen has joined #zig
<Ondingen> I've hit a c-to-zig translation error and I'm trying to make a minimal example. How do I get all the defines set as they are when using @cImport(@cInclude(...))? Right now I'm doing: "zig translate-c -target i386-windows-msvc -Ipath/to/includes path/to/winnt.h" but the target doesn't seem to affect the defines.
a_chou has joined #zig
wilsonk has left #zig [#zig]
<kandinski> so I'm writing this C wrapper and would like to test whether I'm freeing all memory (using the c heap) in my deinit() function. Can anyone point me to a test that checks for that?
_whitelogger has joined #zig
<ifreund> valgrind would certainly let you check that, but you can't use it in a test case
<ifreund> I think your best bet for that is using std.heap.GeneralPurposeAllocator with std.heap.c_allocator as the backing allocator
<ifreund> the general purpose allocator will check for leaks
<ifreund> Ondingen: there are no defines set by default. You can set any you like using @cDefine()
<Ondingen> ifreund That can't be true, there are several defines that aren't set when doing translate-c, like _X86_ etc. Am I misunderstanding something?
<Ondingen> and when I manually set them, I get complaints about unknown type names for DWORD, BYTE etc.. Do I have to manually add all the dependencies to winnt.h for it to work?
<ifreund> hmm, maybe you need to pass -lc ?
<Ondingen> Does not make a difference
<kandinski> ifreund: I'm not passing an allocator expliclty to pcre2 though. I'm just letting it allocate and then calling its own freeing functions.
<Ondingen> For the target, i386-windows-msvc, I have to add all the includes manually anyway so that's what I'm doing for translate-c aswell
<kandinski> example: pcre2.pcre2_code_free_8(self.re_code);
<ifreund> kandinski: there's no way for zig code to track allocation if the allocation is done directly through libc
<kandinski> ifreund: I see, thanks
<ifreund> valgrind is your best friend in that case :)
<kandinski> I don't know C, but isn't there something in the current process that knows what the heap is and where it is and how much of it is used?
<Ondingen> or address sanitizer if you need better performance
<kandinski> ifreund: given that this is my first week writing in a c-level language ever, I think I'll put that in the "later" bin, thanks :)
<kandinski> (for "valgrind" values of "that")
<Ondingen> just run your executable with valgrind, there isn't much to learn
<ifreund> yeah, if you're on linux it's really easy
<kandinski> Ondingen: oh, I see, thanks!
<Ondingen> and especially if you're just starting out, without valgrind or address sanitizer it's really hard to know if you're messing something up
<kandinski> so `valgrind zig test ...`?
<Ondingen> kandinski, with valgrind, you only get line numbers if you've compiled your executable with debug mode
<Ondingen> no, valgrind on your final executable
<Ondingen> I don't know if there's some better way to use it with zig
<kandinski> Ondingen: no worries
<kandinski> I'll figure how to do it in build.zig
<kandinski> maybe build.zig --test or something like that can run valgrind?
<Ondingen> There's exe.valgrind_support but I don't know what it does
<kandinski> I'll look into it later, thanks. For now, well, we do clean up on exit, and I'm not allocating all that much so far.
<Ondingen> You could test it with valgrind zig test ... I think but there might be leaks in zig so you get false positives
<kandinski> In fact, I think I'm only allocating the compiled regex, because the matches come back as pairs of pointers or pointer, length tuples.
<kandinski> so the question is more academic than a pressing need.
<Ondingen> :)
<kandinski> I'm making note for a future requirement. Thanks for the explanation!
<ifreund> kandinski: you may want to give fengb's talk a watch to get up to speed on allocators: https://www.youtube.com/watch?v=vHWiDx_l4V0
<kandinski> ifreund: I will!
<kandinski> ifreund: as a learning project I'm writing a toy lisp via https://github.com/kanaka/mal, so I'll need to figure out allocation for sure.
<ifreund> cool :)
a92 has quit [Quit: Leaving]
wilsonk has joined #zig
Ondingen has quit [Remote host closed the connection]
a_chou has quit [Ping timeout: 240 seconds]
jayschwa has quit [Remote host closed the connection]
xackus has quit [Ping timeout: 258 seconds]
luismanfroni has joined #zig
luismanfroni has quit [Remote host closed the connection]
kristoff_it1 has quit [Ping timeout: 260 seconds]
radgeRayden has quit [Ping timeout: 272 seconds]
a_chou has joined #zig
marnix has joined #zig
jjsullivan has joined #zig
luismanfroni has joined #zig
protheory8-new-m has joined #zig
ur5us has quit [Ping timeout: 260 seconds]
a_chou has quit [Remote host closed the connection]
marnix has quit [Ping timeout: 256 seconds]
marnix has joined #zig
gazler has joined #zig
ask6155 has joined #zig
cole-h has joined #zig
cr1901_modern has quit [Ping timeout: 256 seconds]
cr1901_modern has joined #zig
cr1901_modern has left #zig [#zig]
cr1901_modern has joined #zig
skuzzymiglet has joined #zig
LanceThePants has quit [Read error: Connection reset by peer]
LanceThePants has joined #zig
osa1 has joined #zig
skuzzymiglet has quit [Ping timeout: 260 seconds]
omglasers2 has joined #zig
marnix has quit [Read error: Connection reset by peer]
marnix has joined #zig
marnix has quit [Read error: Connection reset by peer]
marnix has joined #zig
marnix has quit [Read error: Connection reset by peer]
marnix has joined #zig
cole-h has quit [Ping timeout: 272 seconds]
marnix has quit [Ping timeout: 265 seconds]
marnix has joined #zig
earnestly has joined #zig
Kingsquee has quit []
hnOsmium0001 has quit [Quit: Connection closed for inactivity]
ask6155 has left #zig [#zig]
ask6155 has joined #zig
marnix has quit [Ping timeout: 264 seconds]
marnix has joined #zig
osa1 has quit [Remote host closed the connection]
radgeRayden has joined #zig
marnix has quit [Ping timeout: 260 seconds]
marnix has joined #zig
squeek502 has joined #zig
haliucinas has quit [Ping timeout: 265 seconds]
haliucinas has joined #zig
ky0ko has quit [Remote host closed the connection]
ky0ko has joined #zig
marnix has quit [Ping timeout: 256 seconds]
marnix has joined #zig
marnix has quit [Read error: Connection reset by peer]
marnix has joined #zig
marnix has quit [Read error: Connection reset by peer]
marnix has joined #zig
ifreund has quit [Read error: Connection reset by peer]
ifreund has joined #zig
radgeRayden has quit [Remote host closed the connection]
marnix has quit [Ping timeout: 256 seconds]
marnix has joined #zig
kristoff_it1 has joined #zig
ask6155 has left #zig [#zig]
marnix has quit [Read error: Connection reset by peer]
marnix has joined #zig
ask6155 has joined #zig
ask6155 has left #zig [#zig]
ask6155 has joined #zig
luismanfroni has quit [Ping timeout: 272 seconds]
luismanfroni has joined #zig
donniewest has joined #zig
nycex has joined #zig
nycex has quit [Quit: Quit]
nycex has joined #zig
r4pr0n has joined #zig
dingenskirchen has quit [Quit: ZNC 1.7.5 - https://znc.in]
dingenskirchen has joined #zig
r4pr0n has quit [Ping timeout: 240 seconds]
r4pr0n has joined #zig
xackus has joined #zig
frmdstryr has quit [Remote host closed the connection]
frmdstryr has joined #zig
tane has joined #zig
hnOsmium0001 has joined #zig
radgeRayden has joined #zig
xackus has quit [Ping timeout: 265 seconds]
ask6155 has left #zig [#zig]
Akuli has joined #zig
xackus has joined #zig
cole-h has joined #zig
xackus has quit [Ping timeout: 240 seconds]
cr1901_modern has quit [Ping timeout: 260 seconds]
r4pr0n has quit [Quit: r4pr0n]
frett27 has joined #zig
ryuukk__ has joined #zig
ryuukk_ has quit [Ping timeout: 258 seconds]
cr1901_modern has joined #zig
janiczek has joined #zig
<janiczek> Hi folks! I'm trying to init a 2D array (50 rows, 6 columns) but I wasn't able to generalize from the examples in the documentation
<janiczek> the best I was able to do was
<janiczek> const lights = [50][6]bool{[_]bool{false} ** 6} ** 50;
<janiczek> but the compiler complains with "expected [50][6]..., found [1][6]..."
<ifreund> const lights = [_][6]bool{[_]bool{false} ** 6} ** 50;
<ifreund> this will do what you want
<ifreund> same as: const lights = [1][6]bool{[1]bool{false} ** 6} ** 50;
<janiczek> so the trick is to only let it infer (with the underscore) the one that I repeat with ** ?
<ifreund> you want to repeat a length 1 array 50 times, not a length 50 array 50 times
<ifreund> that make sense?
<janiczek> I guess so. I was guessing wrong what the [1][6] means at the beginning of the array literal
<janiczek> ie. I thought it's the length after repeating
<janiczek> but it's length before repeating, then it's repeated, and the final length is implicit?
<g-w1> yes
<janiczek> great, thanks folks!
<g-w1> const lights: [50][6];
<janiczek> ^ false = default element if I don't initialize explicitly?
<g-w1> no
<g-w1> im just telling you the type
<janiczek> oh right
<janiczek> I skipped over the : instead of =
siulManfroni has joined #zig
luismanfroni has quit [Read error: Connection reset by peer]
siul_ has joined #zig
janiczek has quit [Remote host closed the connection]
siulManfroni has quit [Ping timeout: 272 seconds]
braket has joined #zig
<justin_smith> similar question: for an API, I want a struct to hold a slice of (variable) N slices of f64, so I declared "outputs: ?[][]f64", and tried to initialize with ".outputs = &[1][]f64{undefined}" - meaning a slice of 1, not yet initialized with the slice it will hold
<justin_smith> I get "expected type '[][]f64', found *const [1][]f64"
<ifreund> that's an issue with constness
<ifreund> array literals are always const but your fuction expects a mutable slice
<justin_smith> ahh, so the number is a red herring, OK
<justin_smith> then I need to initialize this differently, thanks
<ifreund> no problem
<ifreund> to give more detail: *const [1][]f64 is a constant pointer to an array of one f64 slice. A pointer to an array may be coerced to a slice because the length of the array is comptime known
<ifreund> but only if the constness of the array agrees with the constness of the slice
<justin_smith> that makes sense - the idea is that each struct instance will know its expected number of output slices, but I want a single struct that can hold each
<justin_smith> err, that can hold any of them
<ifreund> if the number is comptime known you could make it generic
<justin_smith> that's a good point, it might be time for me to learn generics
<ifreund> not much to learn really, they're just functions that take some comptime parameters and return a type
AceNovo has joined #zig
wootehfoot has joined #zig
Ondingen has joined #zig
wilsonk has left #zig [#zig]
Ondingen has quit [Remote host closed the connection]
riba has joined #zig
siulManfroni has joined #zig
siul_ has quit [Read error: Connection reset by peer]
supercoven_ has quit [Ping timeout: 240 seconds]
siulManfroni has quit [Client Quit]
moo has joined #zig
ur5us has joined #zig
moo has quit [Read error: Connection reset by peer]
ryuukk_ has joined #zig
ryuukk__ has quit [Ping timeout: 246 seconds]
wootehfoot has quit [Read error: Connection reset by peer]
donniewest has quit [Read error: Connection reset by peer]
donniewest has joined #zig
osa1 has joined #zig
nyaayaya has quit [Quit: Ping timeout (120 seconds)]
nyaayaya has joined #zig
marnix has quit [Read error: Connection reset by peer]
marnix has joined #zig
Akuli has quit [Quit: Leaving]
marnix has quit [Remote host closed the connection]
marnix has joined #zig
skuzzymiglet has joined #zig
frett27_ has joined #zig
AceNovo has quit [Remote host closed the connection]
frett27 has quit [Ping timeout: 260 seconds]
tane has quit [Quit: Leaving]
riba has quit [Ping timeout: 256 seconds]
osa1 has quit [Remote host closed the connection]
skuzzymiglet has quit [Ping timeout: 260 seconds]
frett27_ has quit [Ping timeout: 246 seconds]
AceNovo has joined #zig
marnix has quit [Ping timeout: 264 seconds]
donniewest has quit [Quit: WeeChat 2.9]
AceNovo has quit [Quit: Konversation terminated!]
AceNovo has joined #zig
AceNovo has quit [Quit: Konversation terminated!]
AceNovo has joined #zig
AceNovo has quit [Quit: Konversation terminated!]
AceNovo has joined #zig
omglasers2 has quit [Read error: Connection reset by peer]
AceNovo has quit [Client Quit]
AceNovo has joined #zig
AceNovo has quit [Client Quit]
AceNovo has joined #zig
AceNovo has quit [Client Quit]
AceNovo has joined #zig
CodeSpelunker has joined #zig
AceNovo has quit [Quit: Konversation terminated!]
AceNovo has joined #zig
AceNovo has quit [Client Quit]
AceNovo has joined #zig
kristoff_it2 has joined #zig
kristoff_it1 has quit [Ping timeout: 265 seconds]
daurnimator has quit [Ping timeout: 272 seconds]
AceNovo has quit [Quit: Konversation terminated!]
AceNovo has joined #zig
xackus has joined #zig
AceNovo has quit [Ping timeout: 264 seconds]
AceNovo has joined #zig
casaca has quit [Ping timeout: 246 seconds]
Wolf480pl has quit [Ping timeout: 246 seconds]
Wolf480pl has joined #zig