#concatenate 3 **comptime-unknown** strings but can't afford to use try catch or if

1 messages · Page 1 of 1 (latest)

dusty rivet
#

reason I can't use this is because I am sending data to a C function which can't use errors

shadow basin
#

you cant concatenate without the possibility of errors because allocation can always fail

dusty rivet
#

hmm, let me give my code example

#
pub fn send(x: header_with_body) []const u8 {
    const allocator = std.heap.page_allocator;
    const dz = [_][]const u8{ x.header, "\n\n", x.body };
    const res = try std.mem.concat(allocator, u8, &dz);
    return res;
}

error: Function expected to return []const u8, but is returning whatever![]const u8

#

how can I make this function use std.mem.concat(allocator, u8, &dz); but not return an error

shadow basin
#

youd just do const res = std.mem.concat(allocator, u8, &dz) catch return "server_error"

#

but there's something wrong with that too

#

now the function can either return a heap allocated string or a statically allocated string
which is bad
since you have to free heap allocated strings but cant free statically failed allocated strings

#

I dont understand what issue youre trying to solve either
this function can return an error, there's no reason it shouldnt be able to
you only cant return an error from a function that follows the C calling convention

dusty rivet
shadow basin
#

show me that function

dusty rivet
#

uh, its very complex, like then that function is inside a wrapper which is called by another function, then passed as argument to other function.... its huge code!
Now, if I go and add ! to this it will take me through all my source code reaching the c file.

#

so I was trying if it was possible we can make this function not return an error and catch it in some way

shadow basin
#

well either way
just simply dont return an error from the callconv(.C) function lol
catch it in the body and do something else

dusty rivet
#

yes, let me try and I'll get back

dusty rivet
#

like, I want to get the 3rd index in zig, similar code in python:

of.split(" ")[3]
shadow basin
dusty rivet
#

hmm, can you please tell me a way to split string with delimiter " " like the code and get the 3rd index of it.

shadow basin
#

youd do something like

_ = res.next().?;
_ = res.next().?;
third_index = res.next().?;

note that this asserts that there are at least three items in the iterator

dusty rivet
#

🥹 we are back with !

shadow basin
#

and the reason you cant just take an index is because it's lazy
it's not a list
you cant get the third item without computing the second item and you cant get the second item without computing the first one

dusty rivet
shadow basin
#

no
.next() reutrns a ?[]const u8

#

but again
if there arent at least three entries in that iterator, this will crash your program on Debug/ReleaseSafe builds and cause illegal behavior on ReleaseFast builds

#

so if you arent 100% sure that's the case, you should handle those cases

dusty rivet
#

error: expected type '*mem.TokenIterator(u8,.any)', found '*const mem.TokenIterator(u8,.any)'

#

I am getting this for some reason

shadow basin
#

yeah
because you declared res as a constant

#

even though it needs to be mutated to get the next token

dusty rivet
#

OH WOW!

#

I can't believe it, it is working

#

Thanks alot!!!!

shadow basin
#

no problem