Search code examples
ceylon

Tuple without last element with correct type


Is it possible to remove the last element from a tuple in typesafe manner for arbitrary arity?
I want something like this:

[A,B,C] abc = [a,b,c];
[A,B] ab = removeLast(abc);

Solution

  • No, unfortunately it's not possible, the reason being that a tuple type is represented within the type system as a linked list of instantiations of Tuple, but the type system can't express loops or recursion within the signature of a function. (And having loops/recursion would almost certainly make the type system undecidable.)

    One way we could, in principle, solve this in future would be to have a built-in primitive type function that evaluates the last element type of a tuple type.

    By "primitive" type function, I mean a type function that can't be written in the language itself, but is instead provided as a built-in by the compiler.

    Ceylon doesn't currently have any of these sorts of primitive type functions, but there are a couple of other similar problems which could be solved in this manner.