restricciones generalizadas dentro de la cláusula parámetro de tipo?

Mario Galic:

Sintaxis SLS especifica de cláusula parámetro de tipo como

TypeParamClause   ::=  ‘[’ VariantTypeParam {‘,’ VariantTypeParam} ‘]’
FunTypeParamClause::=  ‘[’ TypeParam {‘,’ TypeParam} ‘]’
VariantTypeParam  ::=  {Annotation} [‘+’ | ‘-’] TypeParam
TypeParam         ::=  (id | ‘_’) [TypeParamClause] [‘>:’ Type] [‘<:’ Type] {‘<%’ Type} {‘:’ Type}                  {‘<%’ Type} {‘<%’ Type}

donde vemos >:, <:, <%, <%, :las que permita nombres reservados en la cláusula parámetro de tipo. ¿Hay una manera que podríamos utilizar generalizada tipo de restricción nombres simbólicos <:<, =:=en la cláusula parámetro de tipo tal que

def f[T =:= 42] = ???

ampliaría a

def f[T](implicit ev: T =:= 42) = ???

similar a cómo el contexto obligado

def f[T: Numeric] = ???

se expande a

def f[T](implicit ev: Numeric[T]) = ???
Mateo Kubuszok:

En 2.13 (que apoya Singleton tipos si eres curioso acerca de constricción en embarazos únicos) se pueden hacer cosas como:

@ 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

Así que, sí, parece posible. Sólo tienes que utilizar proyectores tipo para aplicar segundo parámetro.

Con <:<Debe ser la misma historia:

@ 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

Supongo que te gusta

Origin http://10.200.1.11:23101/article/api/json?id=414474&siteId=1
Recomendado
Clasificación