获取“单身类型"类型. [英] Get type of a "singleton type"
问题描述
我们可以通过无形创建文字类型:
We can create a literal types via shapeless:
import shapeless.syntax.singleton._
var x = 42.narrow
// x: Int(42) = 42
但是如果什至不可能创建类型别名,该如何使用Int(42)
作为类型呢?
But how can I operate with Int(42)
as a type if it's even impossible to create type alias
type Answ = Int(42) // won't compile
// or
def doSmth(value: Int(42)) = ... // won't compile
推荐答案
1)在 Typelevel Scala 中,您可以只写
1) In Typelevel Scala you can write just
val x: 42 = 42
type Answ = 42
def doSmth(value: 42) = ???
2)在 Dotty Scala中,您可以编写同样的文字.
2) In Dotty Scala you can write the same.
3)在 Lightbend Scala (即标准Scala)+ Shapeless中,您可以编写
3) In Lightbend Scala (i.e. standard Scala) + Shapeless you can write
import shapeless.Witness
import shapeless.syntax.singleton._
val x: Witness.`42`.T = 42.narrow
type Answ = Witness.`42`.T
def doSmth(value: Witness.`42`.T) = ???
在情况1)build.sbt应该是
In case 1) build.sbt should be
scalaOrganization := "org.typelevel"
scalaVersion := "2.12.3-bin-typelevel-4"
scalacOptions += "-Yliteral-types"
在情况2)build.sbt应该是
In case 2) build.sbt should be
scalaOrganization := "ch.epfl.lamp"
scalaVersion := "0.3.0-RC2"
和plugins.sbt
and plugins.sbt
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.1.5")
在情况3)build.sbt应该是
In case 3) build.sbt should be
scalaOrganization := "org.scala-lang"
scalaVersion := "2.12.3"
libraryDependencies += "com.chuusai" %% "shapeless" % "2.3.2"
4),或者您可以同时使用Typelevel Scala和Shapeless.
4) or you can use Typelevel Scala and Shapeless at the same time.
这篇关于获取“单身类型"类型.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!