2017-10-26 8 views
1

Der folgenden CodeKovarianz und höhere kinded Typen in Scala 2.12

type Id[+A] = A 
type ReprF[A, F[_]] = Unit 
type Repr[A] = ReprF[A, Id] 

nicht in Scala 2.12, mit dem Fehler

covariant type Id occurs in invariant position in type 
[A]Playground.this.ReprF[A,Playground.this.Id] of type Repr 

Ich verstehe nicht, warum nicht kompiliert die Kovarianz von Id verhindert Dieser Code wird kompiliert.

ReprF sollte nicht kümmern, wenn F ist kovariant oder nicht, es braucht nur eine Art von Art * -> *.

Fehle ich etwas?

Seltsamerweise kompiliert es in Scala 2.11 korrekt.

Dies ist ein scastie snippet, wenn Sie ein wenig mit dem Code spielen möchten. Jede Hilfe wird sehr geschätzt!

Antwort

2

Ich denke, der Compiler ist sehr durch die kovariante ID verwirrt. Auch bei allen Sein covariant es immer noch die gleichen Fehler gibt:

trait Test { 
    type Id[+A] = A 
    type ReprF[+A, +F[+_]] = Unit 
    type Repr[+A] = ReprF[A, Id] 
} 

Error: covariant type Id occurs in invariant position in type [+A]Test.this.ReprF[A,Test.this.Id] of type Repr 
    type Repr[+A] = ReprF[A, Id] 

Wenn ich die ID nicht definiert verlassen, es ist in Ordnung:

trait Test { 
    type Id[+A] 
    type ReprF[A, F[_]] = Unit 
    type Repr[A] = ReprF[A, Id] 
} 
+0

Ja das Beispiel von einem Code, wie Sie erster Schnipsel extrahiert wurde. Sehr interessant und rätselhaft, was du mit dem zweiten herausgefunden hast! –

Verwandte Themen