Scala 类型参数子句中的广义约束?
SLS指定as的语法 将扩展到Scala 类型参数子句中的广义约束?,scala,type-parameter,type-constraints,kind-projector,Scala,Type Parameter,Type Constraints,Kind Projector,SLS指定as的语法 将扩展到 def f[T](implicit ev: T =:= 42) = ??? 类似于上下文绑定 def f[T: Numeric] = ??? 扩展到 def f[T](implicit ev: Numeric[T]) = ??? 在2.13中(如果您对约束单例感到好奇,它支持单例类型),您可以执行以下操作: @ import $plugin.$ivy.`org.typelevel:kind-projector_2.13.1:0.11.0` import $p
def f[T](implicit ev: T =:= 42) = ???
类似于上下文绑定
def f[T: Numeric] = ???
扩展到
def f[T](implicit ev: Numeric[T]) = ???
在2.13中(如果您对约束单例感到好奇,它支持单例类型),您可以执行以下操作:
@ import $plugin.$ivy.`org.typelevel:kind-projector_2.13.1:0.11.0`
import $plugin.
@ type a = 23
defined type a
@ def f[N : * =:= a]: Unit = ()
defined function f
@ f[a]
@ f[23]
@ f[25]
cmd9.sc:1: Cannot prove that 25 =:= Int(23).
val res9 = f[25]
^
Compilation Failed
@ def g[N : * =:= 16]: Unit = ()
defined function g
@ g[16]
@ g[23]
cmd11.sc:1: Cannot prove that 23 =:= 16.
val res11 = g[23]
^
Compilation Failed
所以,是的,这似乎是可能的。你只需要使用投影仪来应用第二个参数
使用
def f[T](implicit ev: Numeric[T]) = ???
@ import $plugin.$ivy.`org.typelevel:kind-projector_2.13.1:0.11.0`
import $plugin.
@ type a = 23
defined type a
@ def f[N : * =:= a]: Unit = ()
defined function f
@ f[a]
@ f[23]
@ f[25]
cmd9.sc:1: Cannot prove that 25 =:= Int(23).
val res9 = f[25]
^
Compilation Failed
@ def g[N : * =:= 16]: Unit = ()
defined function g
@ g[16]
@ g[23]
cmd11.sc:1: Cannot prove that 23 =:= 16.
val res11 = g[23]
^
Compilation Failed
@ def h[N : * <:< 16]: Unit = ()
defined function h
@ h[16]
@ h[17]
cmd13.sc:1: Cannot prove that 17 <:< 16.
val res13 = h[17]
^
Compilation Failed