Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you alias a tuple type?

I'm playing around with Ceylon and I'm trying to create an alias for a tuple. The following do not work:

class MyPair(Integer i, Float f) => [i, f];
class MyPair(Integer i, Float f) => [Integer, Float](i, f);
class MyPair(Integer i, Float f) => 
        Tuple<Integer|Float, Integer, Tuple<Float, Float, Empty>>(i, [f]);
class MyPair(Integer i, Float f) => 
        Tuple<Integer|Float, Integer, Tuple<Integer|Float, Float, Empty>>(i, [f]);
class MyPair(Integer i, Float f) => 
        Tuple<Integer|Float,Integer,Tuple<Float,Float,Empty>>(i, Tuple<Float,Float,Empty>(f, []));

The error I get on the first two revolves around the use of brackets:

Incorrect syntax: missing statement-ending ; at [ expecting statement-ending ;

There are two separate errors on the second:

Some variation of

Alias parameter distance must be assignable to corresponding class parameter rest: Integer is not assignable to [Integer]

on class MyPair and

Argument must be a parameter reference to distance

on f, [f], or the tuple construction.

Is there a way to do this?

like image 418
Jeffrey Avatar asked Feb 26 '26 16:02

Jeffrey


2 Answers

Yeah, the instantiation expression on the RHS of the => in a class alias declaration is currently extremely restricted, not by design, but just because it will take some extra work to implement full support for arbitrary instantiation expressions in the compiler backends.

But what I would actually do for now would be to use a regular type alias, like this:

alias MyPair => [Integer,Float];

And use it like this:

MyPair pair = [1, 1.0];

I think that's actually even cleaner than using a class alias.

HTH.

like image 77
Gavin King Avatar answered Mar 01 '26 08:03

Gavin King


After tinkering around a bit I came across

class MyPair(Integer i, [Float] f) => 
        Tuple<Integer|Float, Integer, Tuple<Float, Float, Empty>>(i, f);

which works.

like image 20
Jeffrey Avatar answered Mar 01 '26 07:03

Jeffrey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!