2016-10-31 6 views
0

AならA型はサブタイプ、B型はスーパータイプであると理解しています。Bの代わりにAを使うと思っています。 B.ここに私の問題がありますスカラー型でスーパータイプを見つける方法

type One 
type Two 
type Three 
type Four 
type Five 
type Six 
type Seven 
type Eight 

type Fun1 = { val a: One } => { val b: Two } 
type Fun2 = { val b: Two } => { val a: One } 

type SuperType = { 
?? 
} 

type TypeOne = { 
    def apply: { val func: Fun1 ; val c: Three } => { val b: Two ; val d: Four } 
    val g: Seven 
} 

type TypeTwo = { 
    def apply: { val func: Fun2 ; val e: Five } => { val b: Two ; val f: Six } 
    val h: Eight 
} 

TypeOneとTypeTwoのスーパータイプであるスーパータイプを作る方法を教えてください。 「Any」というキーワードだけを考え出すことができました。私は見ていないし、他のすべての値

+0

Scalaの非常に特定のサブセットに固執しようとしているようです。そうする理由はありますか? – stefanobaghino

答えて

1

Fun1Fun2の間にある唯一の関係は、それらが両方とも関数なので、SuperTypeは汎用引数を持つ関数だと思います。私はできるだけスニペットで公開しているスタイルに固執しようとしました(ただし、Scalaは型階層を定義するもっと慣用的な方法を提供しています)。

type SuperType = { 
    type A 
    type B 
    def apply: { val a: A } => { val b: B } 
} 

type Fun1 <: SuperType { 
    type A = One 
    type B = Two 
} 
type Fun2 <: SuperType { 
    type A = Two 
    type B = One 
} 
1

との関係はたぶん、これはあなたが探しているものではありませんが、あなたのため、この作業を行うので、私はまた

def apply: {val func: Fun1}=>{val b: Two} 

を試してみましたか?

sealed trait SuperType 
trait TypeOne extends SuperType { 
    def apply: { val func: Fun1 ; val c: Three } => { val b: Two ; val d: Four } 
    val g: Seven 
} 

trait TypeTwo extends SuperType { 
    def apply: { val func: Fun2 ; val e: Five } => { val b: Two ; val f: Six } 
    val h: Eight 
} 
3

あなたが望む関係を得るための方法の1つです。

abstract class SuperType 
class TypeOne extends SuperType { /* your code here */ } 
class TypeTwo extends SuperType { /* etc. */ } 

typeキーワードは、ほとんどのタイプの別名を作るか、またはザは、具体的にどこ階層の鎖下、後に定義する抽象型を宣言するために使用されます。つまり、投稿したコードは実際には何も作成しません。これらのタイプのオブジェクトはインスタンス化できません。

ご参考までに抽象というタイプの関係を宣言すると、type TypeOne <: SuperTypeのようになります。

+0

_abstract_関係ではどういう意味ですか? – stefanobaghino

関連する問題