-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converted a Scala 2 example from a script to a program
- Loading branch information
Dean Wampler
committed
Mar 1, 2020
1 parent
cbb3a50
commit b8fe2d1
Showing
2 changed files
with
77 additions
and
71 deletions.
There are no files selected for viewing
71 changes: 0 additions & 71 deletions
71
src/main/scala-2/progscala3/metaprogramming/MatchTypeTags.sc
This file was deleted.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
src/main/scala-2/progscala3/metaprogramming/MatchTypeTags.scala
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,77 @@ | ||
// src/main/scala-2/progscala3/metaprogramming/MatchTypeTags.scala | ||
|
||
package progscala3.metaprogramming | ||
|
||
import scala.reflect.runtime.universe._ | ||
|
||
/** | ||
* This example uses TypeTags, which don't exist in Scala 3. | ||
*/ | ||
object MatchTypeTags { | ||
|
||
def toType2[T](t: T)(implicit tag: TypeTag[T]): Type = tag.tpe | ||
def toType[T : TypeTag](t: T): Type = typeOf[T] | ||
|
||
/** | ||
* Return a tuple of the type "prefix", the type's symbol and its | ||
* possibly empty list of type parameters. | ||
*/ | ||
def toTypeRefInfo[T : TypeTag](x: T): (Type, Symbol, Seq[Type]) = { | ||
val TypeRef(pre, typName, parems) = toType(x) | ||
(pre, typName, parems) | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
println(toType(1)) | ||
println(toType(true)) | ||
println(toType(Seq(1, true, 3.14))) | ||
println(toType((i: Int) => i.toString)) | ||
|
||
println(toType2(1)) | ||
println(toType2(true)) | ||
println(toType2(Seq(1, true, 3.14))) | ||
println(toType2((i: Int) => i.toString)) | ||
|
||
assert(toType(1) <:< typeOf[AnyVal] == true) | ||
assert(toType(1) <:< toType(1) == true) | ||
assert(toType(1) <:< toType(true) == false) | ||
|
||
assert(toType(1) =:= typeOf[AnyVal] == false) | ||
assert(toType(1) =:= toType(1) == true) | ||
assert(toType(1) =:= toType(true) == false) | ||
|
||
// (scala.type, class Int, List()): | ||
println(toTypeRefInfo(1)) | ||
|
||
// (scala.type, class Boolean, List()): | ||
println(toTypeRefInfo(true)) | ||
|
||
// (scala.collection.type, trait Seq, List(AnyVal)): | ||
println(toTypeRefInfo(Seq(1, true, 3.14))) | ||
|
||
// (scala.type, trait Function1, List(Int, java.lang.String)): | ||
println(toTypeRefInfo((i: Int) => i.toString)) | ||
|
||
val t1 = toType(1) | ||
val ts = toType(Seq(1, true, 3.14)) | ||
val tf = toType((i: Int) => i.toString) | ||
|
||
// Use the reflect.api.types$TypeApi. (The output is long!) | ||
def p(msg: String, t: Type): Unit = { | ||
println(s""" | ||
|For input $msg: | ||
| t.typeSymbol: ${t.typeSymbol} | ||
| t.erasure: ${t.erasure} | ||
| t.typeArgs: ${t.typeArgs} | ||
| t.typeParams: ${t.typeParams} | ||
| t.companion: ${t.companion} | ||
| t.decls: ${t.decls} | ||
| t.members: ${t.members} | ||
|""".stripMargin) | ||
} | ||
|
||
p("1:Int", t1) | ||
p("Seq(1, true, 3.14)", ts) | ||
p("i:Int => String", tf) | ||
} | ||
} |