Feature request: Automatically convert a variant to a parameter whenever it's safe to do so #1728
Replies: 2 comments
-
I partially get what you're saying but maybe simplifying the example a bit further might help 😅? Based on my understanding on both the problem and Mojo as a language this would require a custom trait that can encompass all allowed types but if one doesn't want a custom Int, custom String, etc that seems like it'd make the issue worse rather than better. The example you gave in the beginning however should actually work (it should become valid Mojo IMO) as the Variant should break down given that you're using an Int. If one gives one of the allowed types the Variant part should fizzle away and just leave the accepted type. |
Beta Was this translation helpful? Give feedback.
-
I would imagine this is a problem with a lack of subtyping in mojo? I suppose you could run into issues with covariance and contravariance though. I'm not even sure what mojo's rules for covariance, contravariance and invariance are. Also, will mojo support structural subtyping of some form? My understanding is that structs will not support nominative hierarchical subtyping relationships (eg OOP style inheritance), but what about structural subtyping (ie, a more formal duck typing)? I'm not a PLT guy, but wouldn't subtyping solve this problem? |
Beta Was this translation helpful? Give feedback.
-
Apologies if this does not make any sense, I'm not an expert in language design.
The current issue with Variant:
When we look at Mojo's Variant, we can see that they're not easy to use (compared to, for example, Python's unions).
Because of the internal representation, a function that works with let's say, an Variant, will not accept as argument only one element of a Variant. Currently, we do a trick with
Optional[T]
in mojo to go around this and have automatic conversion, but that won't work as soon as we work with collections. Consider the following piece of code, which is very common (any serializer is full of this type of functions):To write intuitive libraries here, and avoid requirering data conversion (which can be slow for arrays), we're a bit stuck, we could use overloads, but that's a lot of code duplication. If I want my function to work with all possible types, I would have to write something like this (if unions were available as a trait):
But even then, that's very painful, especially if there are more than two types in the variant. With
Variant[Int, Float64, String]
, I would have to writeTo make sure all possible inputs are supported, without requirering data conversion.
The proposed solution
I hope I'm not missing anything here.
I believe If we have a
borrowed
argument using a Variant, since the code path would be correct with any combinaison of input types, could the compiler automatically create the correct overloads with all the combinations of types mentionned above. It would ensure that we can write very intuitive functions in our libraries. For example, it would allow us to write easily json.dumps() that accepts many inputs:I believe it only works with borrowed because if the input is mutable, we might want to modify the type in the input value.
I hope I was clear enough, and that I'm making sense, and that it will be easier to use variant in the future without forcing data conversion (which can be an issue for arrays).
I guess we could start by having a decorator which does this, to see if people find it useful?
Beta Was this translation helpful? Give feedback.
All reactions