#Function that can return one possible structure from two

1 messages · Page 1 of 1 (latest)

steady horizon
#

Hello folks. New to Zig here.

Is it possible to write a function that, based on the parameter it receives and the calculation inside, returns one from two possible structures? It'd also be nice if errors can be handled by returning an error enum, but I think that's asking too much.

deft fox
#

given

};
const B = struct {
};

you can do a function with this prototype

a: A,
b: B,
}```
where ! indicates the function can return anyerror and if it doesn't error, it will return a union of the two structs (think of it as it can return one of the fields of the union). If the parameter is comptime known you can do fancier
#

Parameter here is whatever type is your parameter

plain mantle
#

! doesn't indicate anyerror, it tries to infer the error set
sometimes the compiler can't infer the error and you have to explicitly tell it to except any error with anyerror!

proven nacelle
#

See the return type of something like std.mem.asBytes.

#

You can write a type translation function and use it in the return type.

steady horizon
#

I need to figure some stuff out first as it seems. Maybe rethink the logic of my code, move some stuff here and there to make it work more nicely than it does now. Thanks for everyone's inputs.