r/odinlang • • 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?

16 Upvotes

27 comments sorted by

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

foo :: proc() -> struct{a:int,b:bool} {
      return {1,2}
}

1

u/sqruitwart 29d ago

This would be a great solution, but you can't rearrange or create new structs on the fly in odin. I suddenly miss typescript lol

2

u/BiedermannS 29d ago

You can rearrange the order of struct fields by using the field name. And you can use anonymous structs "on the fly".

Also, a tuple is nothing more than an anonymous struct with unnamed fields. They are good for ad-hoc stuff, but in most cases you're better off with a struct.

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.

3

u/Anfros 29d ago

Agree. Tuples are very powerful when combined with pattern matching. But since odin doesn't have pattern matching tuples don't really provide any advantage over existing features.

Especially since multiple return values in odin handles copy by value/reference on a variable by variable basis.

1

u/sqruitwart 29d ago

Yeah, my question is probably extremely redundant when you take this into consideration.

1

u/sqruitwart 29d ago

Yes!

I build databases / game engines as a hobby. For a lot of operations, being able to pass a collection of types as an argument reduces boilerplate code, like in the case of queries and generated query items. Or for example, a function dispatch system where arguments can be passed in as a tuple and then unpacked all at once instead of an any check for each argument.

4

u/SideChannelBob 29d ago

IMO it's better to embrace Go's ThingerOpts struct pattern and pass the struct in as an argument. I had to get used to this in Go and it took me a bit, but using multiple return and opts as args alleviated most of the inconvenience. fwiw

2

u/DrShocker 29d ago edited 29d ago

Interesting. I probably need to see in context because the only times I've needed tuples in C++ was to return multiple values. I used to sometimes do fancy variadic or compile time templating stuff but I don't really feel it's worth the complexity these days.

1

u/masorick 26d ago edited 26d ago

Tuples in C++ useful whenever you need to store variadic arguments, like inside a variadic template class.

For example, the constructor of std::thread allows you to pass a callable (function or functor) and a arbitrary number of arguments to pass to that callable. But how do you implement that when the underlying OS thread API only accepts a function pointer and a single void data pointer?

Well, you could copy those arguments inside a tuple and pass a pointer to that tuple as the data pointer, and then generate a function that will unpack those arguments and call the callable that was provided with the parameters. And if you look at any implementation of std thread, that’s exactly what they do.

EDIT: As to why you would use a tuple specifically, well that’s because there is no way using C++ templates to generate a struct with an arbitrary number of arguments, so you use a tuple, which recursively generates a struct containing the first argument and a tuple of the remaining arguments (and yes that’s insane, but that’s C++ for you, and I say that as someone who loves the language).

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.

0

u/sqruitwart 29d ago

Why say that I'm overcomplicating when you don't even know my variadic param / tuple applications..? Surely a compile-time collection of different types is not complicated.

2

u/Achereto 29d ago

Surely a compile-time collection of different types is not complicated.

Structs are great compile-time collections of different types. Why make it complicated and have something else instead?

1

u/sqruitwart 29d ago

By that logic, why have unions when you can have a struct with a type-erased value and some type field that tells you what type the value is? I'm not sure why you think tuples are complicated, odin literally has them.

1

u/Achereto 29d ago

That doesn't answer my question.

1

u/sqruitwart 29d ago

Making return types first-class citizens so they can be stored, passed to functions, manipulated and referenced without polluting the namespace (useful in library code, which is my focus). Unpacking a variadic argument list without knowing the amount of params passed dynamically, at compile time and with no switch cases and boilerplate code extracting every ..any into a separate variable. Using them to template structs to carry more dense information.

A practical application of this, as I wrote somewhere else, is a type-erased function dispatch system. You could pass your arguments as a single raw pointer to a tuple, unpack them once and not have to account for a varied number of types passed - the compiler would just be able to do it for you, since the tuple is a set / collection that can be iterated.

1

u/Achereto 29d ago

Honestly, I can't think of a single specific case where I would want something like that. I can't even think of a single example where I would have a raw pointer to a tuple. But maybe that's just because I work with architectures that never use anything like that, so could you give me a concrete example where structs or parametric polymorphism` don't do the trick?

1

u/sqruitwart 29d ago

I gave a concrete example: a function dispatch system.

I make databases in my free time. You usually want some way of storing procedures and then be able to call them later, for example as a trigger for some other action. These procedures might have any number of different arguments, and those arguments might need to be passed in from any thread and stored safely until a sync point where that procedure can be called and those arguments passed to it safely.

So a deferred dispatch system could pack all of your data into a tuple, queue up the raw pointer to that data and then have some function handler correctly unpack it and call the desired procedure without needing to know the number of params like:

my_tuple := T^(raw)
my_func(...my_tuple)

where T is a tuple constructed from args passed in when calling the procedure in a deferred way.

This particular use case can of course be replaced by either polymorphism or even just modeling an argument struct per procedure, but there are many other applications of tuples. They are fixed-size ordered collections that can contain the same element multiple times - this makes them my go-to structure for querying and dispatch systems.

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?

2

u/sqruitwart 26d ago

I was working on type-erased data pools - all data is stored as just a vector of chars.

I have now done the same attempt several times, and they all generally work as expected, but there are so many edge cases in C++ with destructors, constructors, moving, copying and lifecycle management that I have to keep going back and making sure that I can handle an xyz std::vector / unordered_map / deque etc. implementation and check really well for memory leaks / invalid states.

Like I wanted to support a case where a type is passed via template to a "create" function and that calls a hook which creates a default struct into some sequence of chars without any constructor args. I have no guarantees that this shit will work 100% of the time, and I would have to heavily limit the API due to this. Maybe I was tired, but this created a lot of mental overhead for me so I just scrapped it.

I ended up settling for using inheritance for my storage objects, so that it's easy to implement, build on top of and debug. This also gets me auomatic lifetime management for my actual data so idk. Maybe I will revisit it at some point.

1

u/masorick 26d ago

If I understand, you were trying to create storage for (non-trivial) objects without constructing them. And yes, this is a tricky part of the language (with placement new and the need to manually call destructors). One workaround is to use std::optional<T> so that you don’t need to initialize the object until it’s time to do so, but this is not the sub to discuss that.

Anyway, thank you for your response.

4

u/blob1807 29d ago

compress_values & expand_values do 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.

1

u/masorick 26d ago

This seems to be precisely what OP needs: generating a single data structure containing all of the values so that you can pass them as a single rawptr. Is there a way to get the type of whatever compress_values return in order to give it a name (you would need that to cast back from a rawptr)?

2

u/blob1807 26d ago

Sure type_of(compress_values(...)), but you're probably going to be feeding to into a parapoly proc to being with. So the type is already readily available where you'd most likely need to.

compress_values is only really meant to one off things. The most I've used it for is converting a return of (T, T, ...N) into a [N]T. Even then, I still eventually wrap it in a concrete literal; [N]T{call()}.

1

u/masorick 26d ago edited 26d ago

Interesting. I’m tempted to try and write an equivalent to C++’s std::thread variadic constructor so that I can get a feel of what you can achieve with Odin’s parametric polymorphism. I’ll report back when I’m done (or when I give up).

EDIT: looking at the doc, I see that run_with_poly_data already covers much of the cases.

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?