Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
turn unchecked type patterns into checked ones
the pattern `(_: T)` is made checkable using (ct: ClassTag[T]).unapply by rewriting it to `ct(_: T)` (if there's a ClassTag[T] available) similarly for extractors: if the formal type of the unapply method is an uncheckable type, wrap in the corresponding classtag extractor (if available) don't trigger rewrite on non-toplevel unchecked types (i.e., only look at type constructor part of T when looking for unchecked types) TODO: find outer match to figure out if we're supposed to be unchecked would like to give users a chance to opt-out from the wrapping, but finding the match to which this pattern belongs turned out to be tricky...
- Loading branch information
Showing
5 changed files
with
126 additions
and
20 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
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,10 @@ | ||
1 is not a ClassTag[int]; it's a class java.lang.Integer | ||
1 is a ClassTag[class java.lang.Integer] | ||
1 is not a ClassTag[class java.lang.String]; it's a class java.lang.Integer | ||
true is a ClassTag[class java.lang.Object] | ||
woele is a ClassTag[class java.lang.String] | ||
1 is not a ClassTag[int]; it's a class java.lang.Integer | ||
1 is a ClassTag[class java.lang.Integer] | ||
1 is not a ClassTag[class java.lang.String]; it's a class java.lang.Integer | ||
true is a ClassTag[class java.lang.Object] | ||
woele is a ClassTag[class java.lang.String] |
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 @@ | ||
import reflect.{ClassTag, classTag} | ||
|
||
trait Extractors { | ||
type T | ||
implicit val tTag: ClassTag[T] | ||
object ExtractT { | ||
def unapply(x: T) = Some(x) | ||
} | ||
def apply(a: Any) = a match { | ||
case ExtractT(x) => println(x +" is a "+ implicitly[ClassTag[T]]) | ||
case _ => println(a+ " is not a "+ implicitly[ClassTag[T]] +"; it's a "+ a.getClass) | ||
} | ||
} | ||
|
||
object Test extends App { | ||
def typeMatch[T: ClassTag](a: Any) = a match { | ||
case x : T => println(x +" is a "+ implicitly[ClassTag[T]]) | ||
case _ => println(a+ " is not a "+ implicitly[ClassTag[T]] +"; it's a "+ a.getClass) | ||
} | ||
|
||
// the same match as typeMatch, but using an extractor | ||
def extractorMatch[S: ClassTag](a: Any) = | ||
(new Extractors { type T = S; val tTag = classTag[T] })(a) | ||
|
||
typeMatch[Int](1) | ||
typeMatch[Integer](1) | ||
typeMatch[String](1) | ||
typeMatch[Any](true) | ||
typeMatch[String]("woele") | ||
|
||
extractorMatch[Int](1) | ||
extractorMatch[Integer](1) | ||
extractorMatch[String](1) | ||
extractorMatch[Any](true) | ||
extractorMatch[String]("woele") | ||
} |