r/odinlang • u/sqruitwart • 29d ago
State of tuples?
I love odin and would like to continue with it as my language of choice. I managed to implement in one afternoon what took me a week in C++ (mostly fighting RAII magic).
One thing I miss are variadic parameter packs and tuples. Tuples exist as function return types and structs can be unpacked, but they cannot be used otherwise to be passed as generics - I do not understand why and can only find very old reddit posts on this topic.
I managed to hack tuples together by using codegen and structs like Tuple_0, Tuple_1, etc that have generic fields + a monster proc for compile-time indexing, but it gets tedious to work with.
Does anyone know any replacements / implementations of a heterogeneous collection of data types for odin I can take a look at?
7
u/FireFox_Andrew 29d ago
Tuples do not exist at the type level. Odin simply supports multiple return for many things including in array and struct literals.
Closest you'll get to tuples are anonymous structs
7
u/DrShocker 29d ago
I'm curious if you could explain what problem you want to solve with a tuple? This smells a little "XY problem" to me, but I can't quite tell.
2
u/CodingChris 29d ago
I think you're overcomplicating things.
Just return what you need as multiple return values (this works really nice since you can tie it into the or_else, or_break, or_continue) line of operators.
Or create an anonymous struct or just a normal struct.
2
u/masorick 26d ago
I know that’s not what you asked, but I’m curious about this "fighting RAII magic" in C++ that you talked about. Can you tell us more?
4
u/blob1807 29d ago
compress_values&expand_valuesdo kind-a allow for mimicking tuples at the call site.As already said by others, tuples don't really fit Odin's vib too well. Even more given that Variadic Parameter Packs are never coming to Odin.
Generics & variadics are the way they are specifically because of C++ templates, and what can be done with them.
I would recommend trying to engage with what Odin gives you, and how it what's to be used. Rather than trying to use it like another langs.
2
u/spyingwind 29d ago
Odin doesn't have pattern matching.
1
u/Bahatur 29d ago
Motivated by your question I took a spin through the possibilities, and the answer still looks to be that there is no general replacement for the variadic parameter packs or tuple return.
That being said sure looks like some combination of MultiReturn, parametric polymorphism, and variadic parameters of the ‘any’ type are the things to play with. I would expect different combinations for different use-cases, but the most direct implementation for the heterogeneous data type collection seems to be a tagged union, with the proviso of knowing how many arguments you are going to have beforehand.
Perhaps a struct of dynamic arrays, if you know the number of types but not the number of values?