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
  •  | 
  •  

문제

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!!

도움이 되었습니까?

해결책

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top