3

私はセイロンで遊んでいて、タプルのエイリアスを作成しようとしています。以下は機能しません。

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, []));

最初の 2 つのエラーは、ブラケットの使用に関係しています。

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

2 番目に 2 つの別個のエラーがあります。

いくつかのバリエーション

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

class MyPairと_

Argument must be a parameter reference to distance

f[f]またはタプル構造。

これを行う方法はありますか?

4

3 に答える 3

2

ソリューションよりもはるかに優れていることはできませんが、少なくともRest型パラメーターのショートカットを使用できます。

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

クラス エイリアスのパラメーターの型は、エイリアス化するクラスのパラメーターの型と一致する必要があるため、ここでは制限があります。仕様を正しく解釈している場合:

注: 現在、コンパイラは、エイリアス化されたクラスの呼び出し可能な型が、クラス エイリアスの呼び出し可能な型に割り当て可能でなければならないという制限を課しています。この制限は将来的に削除されます。

その後、これは後続のリリースで機能する可能性があります。

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

または多分:

class Pair(Integer i, Float f) => [i, f];

繰り返しになりますが、タプルを分解することが目的の場合、Ceylon 1.2 ではそれを直接行うことができます。

value [i, f] = [2, 0.5];
于 2015-08-12T11:39:51.800 に答える
2

少しいじくり回した後、私は出くわしました

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

動作します。

于 2015-08-12T07:35:36.490 に答える