Can a method from a Generic class return a Tuple with the same types as its argument-list?

StackOverflow https://stackoverflow.com/questions/21392615

  •  03-10-2022
  •  | 
  •  

Question

I wanted to have something like this:

[String, Integer] values = SomeClass("Hi", 1).values();
[Boolean, Float] others = SomeClass(true, 2.0).values();

// EDIT - should return tuple with same type as the arguments list
[String] strs = SomeClass("Strings").values();

Is this possible with Ceylon generics?

I suspect something similar to this should be possible after reading about currying in Ceylon, but I can't figure this out right now!!

Was it helpful?

Solution

class SomeClass<Args>(Args args)
            given Args satisfies Tuple<Anything, Anything, Anything[]> {
    shared Args values() => args;
}

[String, Integer] foo = SomeClass(["Hi", 1]).values();
[Boolean, Float] bar = SomeClass([true, 2.0]).values();
[String] baz = SomeClass(["Strings"]).values();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top