3 ms·
A language like Kotlin can do some of these things using delegates, interfaces and extension methods. For example a MutableList<T> can be dropped down to a Lis
by vbsteven 5y ago
A language like Kotlin can do some of these things using delegates, interfaces and extension methods.
For example a MutableList<T> can be dropped down to a List<T> which is not mutable. And a generic conversion from Collection<T> to NonEmptyCollection<T> should be trivial to write as an extension method.
- TeMPOraL 5y agoCan Kotlin handle multiple "tags" on a type as a set, and not a sequence? I'm not familiar with the language, so I'll use a C++ analogy. If you tried to tag types in C++, you'd end up with something like: TaggedType<std::vector, NonEmpty, Sorted> but such type is strictly not the same as: TaggedType<std::vector, Sorted, NonEmpty> What I mean by "set" instead of a "sequence" is to have the two lines above represent the same type, i.e. the order of tags should not matter.
- vbsteven 5y agoYou can maybe get there in kotlin with a generic type with multiple constraints in a where clause. Let’s say you have Sorted and NonEmpty as interfaces (could be empty marker interfaces so they behave like tags). Then you can write a method fun <T> doSomething(values: T) where T: Sorted, T: NonEmpty {} And that function will take any type that has both Sorted and NonEmpty interfaces.
- brabel 5y agoYou don't need Kotlin to do this, even Java can do it: <T extends Sorted & NonEmpty> void doSomething(T values)