-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class C1Inv | ||
A | ||
|
||
class C1Cov | ||
+A | ||
|
||
class C1Con | ||
-A | ||
|
||
class C2InvInv | ||
A, B | ||
|
||
class C2InvCov | ||
A, +B | ||
|
||
class C2InvCon | ||
A, -B | ||
|
||
class C2CovInv | ||
+A, B | ||
|
||
class C2CovCov | ||
+A, +B | ||
|
||
class C2CovCon | ||
+A, -B | ||
|
||
class C2ConInv | ||
-A, B | ||
|
||
class C2ConCov | ||
-A, +B | ||
|
||
class C2ConCon | ||
-A, -B | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import scala.quoted.* | ||
|
||
inline def classVariances[A <: AnyKind]: String = | ||
${variancesImpl[A]} | ||
|
||
def variancesImpl[A <: AnyKind: Type](using Quotes): Expr[String] = | ||
import quotes.reflect.* | ||
val variances = TypeRepr.of[A].typeSymbol.typeMembers.filter(_.isTypeParam).map { sym => | ||
if sym.paramVariance == Flags.Covariant then "+" + sym.name | ||
else if sym.paramVariance == Flags.Contravariant then "-" + sym.name | ||
else sym.name | ||
} | ||
val res = variances.mkString(TypeRepr.of[A].typeSymbol.toString + "\n", ", ", "\n") | ||
Expr(res) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class C1Inv[A] { type T } | ||
class C1Cov[+A] { type T } | ||
class C1Con[-A] { type T } | ||
|
||
class C2InvInv[A, B] { type T } | ||
class C2InvCov[A, +B] { type T } | ||
class C2InvCon[A, -B] { type T } | ||
class C2CovInv[+A, B] { type T } | ||
class C2CovCov[+A, +B] { type T } | ||
class C2CovCon[+A, -B] { type T } | ||
class C2ConInv[-A, B] { type T } | ||
class C2ConCov[-A, +B] { type T } | ||
class C2ConCon[-A, -B] { type T } | ||
|
||
@main def Test = | ||
println(classVariances[C1Inv]) | ||
println(classVariances[C1Cov]) | ||
println(classVariances[C1Con]) | ||
println(classVariances[C2InvInv]) | ||
println(classVariances[C2InvCov]) | ||
println(classVariances[C2InvCon]) | ||
println(classVariances[C2CovInv]) | ||
println(classVariances[C2CovCov]) | ||
println(classVariances[C2CovCon]) | ||
println(classVariances[C2ConInv]) | ||
println(classVariances[C2ConCov]) | ||
println(classVariances[C2ConCon]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters