Scala:有没有办法创建内联类型?

Tur*_*rin 7 types type-systems scala inline singleton-type

基本上,我希望能够写出这样的东西:

val x :('k1.type, Int) = 'k1 -> 1
val y :('k2.type, Int) = 'k2 -> 2
Run Code Online (Sandbox Code Playgroud)

如果x和y的类型不兼容,但是要么共享一个超类型,要么可以用上下文边界注释,允许我做这样的事情:

def mlt[T :MyLittleType](x :(T, Int)) = ???
mlt(x); mlt(y)
Run Code Online (Sandbox Code Playgroud)

这里仅使用关键字作为示例,目标是能够为某些标识符/关键字/字符串提供文字和单例类型.这些类型也可能在运行时被擦除/统一,我只对静态类型检查感兴趣.我想应该可以使用宏来实现这一点,但我宁愿不这样做.

dk1*_*k14 1

您可以内联构造结构类型:

scala> val a = new {def hello = null} -> 1 // by the way hello is accessible in a (but scala uses reflection for that)
a: (AnyRef{def hello: Null}, Int) = ($anon$1@68e2da47,1)

scala> var b = new {def hello = null} -> 2
b: (AnyRef{def hello: Null}, Int) = ($anon$1@77147ad6,2)

scala> b = a
b: (AnyRef{def hello: Null}, Int) = ($anon$1@68e2da47,1)

scala> var c = new {def helloooo = null} -> 1
c: (AnyRef{def helloooo: Null}, Int) = ($anon$1@38def4a2,1)

scala> c = a
<console>:15: error: type mismatch;
 found   : (AnyRef{def hello: Null}, Int)
 required: (AnyRef{def helloooo: Null}, Int)
       c = a
           ^
Run Code Online (Sandbox Code Playgroud)

因此,您可以将它们与对象结合起来,赋予它们类型唯一性:

 new {def myTypeName = null} -> myObject //now your myObject tagged with 'myTypeName', but your methods should be aware about tuples

 def mlp(x: ((Any, YourObjectsType), Int)) = x
Run Code Online (Sandbox Code Playgroud)

或者(由于反射而慢一点)

scala> def mlp( x: ({def obj: Symbol}, Int)) = x._1.obj -> x._2
warning: there were 1 feature warning(s); re-run with -feature for details
mlp: (x: (AnyRef{def obj: Symbol}, Int))(Symbol, Int)

scala> mlp(new { def a1 = null; def obj = 'a1 } -> 1)
res18: (Symbol, Int) = ('a1,1)

scala> mlp(new { def a2 = null; def obj = 'a2 } -> 1)
res19: (Symbol, Int) = ('a2,1)
Run Code Online (Sandbox Code Playgroud)

您可以将其与标签结合起来注释您的类型,例如:

import scalaz._
import Scalaz._

scala> def markALittle[T](a: T) = Tag[T, MyLittleType](a)
markALittle: [T](a: T)scalaz.@@[T,MyLittleType]

scala> markALittle(new {def hello: Aaa = null})
res15: scalaz.@@[AnyRef{def hello: Aaa},MyLittleType] = $anon$1@a8c48e8
Run Code Online (Sandbox Code Playgroud)

更多标记示例:

scala> trait MyLittleType

scala> trait Spike extends MyLittleType; val x = Tag[Symbol, Spike]('k1) -> 1
x: (scalaz.@@[Symbol,Spike], Int) = ('k1,1)

scala> trait Rainbow extends MyLittleType; val y = Tag[Symbol, Rainbow]('k2) -> 1
y: (scalaz.@@[Symbol,Rainbow], Int) = ('k2,1)

scala> val y: (scalaz.@@[Symbol,Spike], Int) = Tag[Symbol, Rainbow]('k1) -> 1
<console>:22: error: type mismatch;
 found   : (scalaz.@@[Symbol,Rainbow], Int)
 required: (scalaz.@@[Symbol,Spike], Int)
       val y: (scalaz.@@[Symbol,Spike], Int) = Tag[Symbol, Rainbow]('k1) -> 1


scala> val z: (scalaz.@@[Symbol,_ <: MyLittleType], Int) = Tag[Symbol, Rainbow]('k1) -> 1
z: (scalaz.@@[Symbol, _ <: MyLittleType], Int) = ('k1,1)
Run Code Online (Sandbox Code Playgroud)

所以你可以:

scala> def mlt[T <: MyLittleType](x :(scalaz.@@[Symbol,T], Int)) = x
mlt: [T <: MyLittleType](x: (scalaz.@@[Symbol,T], Int))(scalaz.@@[Symbol,T], Int)

scala> mlt(x)
res42: (scalaz.@@[Symbol,Spike], Int) = ('k1,1)

scala> mlt(y)
res43: (scalaz.@@[Symbol,Rainbow], Int) = ('k2,1)
Run Code Online (Sandbox Code Playgroud)

或者只是使用:

scala> val x = Tag[Int, Rainbow](1)
x: scalaz.@@[Int,Rainbow] = 1

scala> val y = Tag[Int, Spike](1)
y: scalaz.@@[Int,Spike] = 1
Run Code Online (Sandbox Code Playgroud)

您可以 x同时Int使用进行操作Tag.unwrap(x),或者只是定义implicit def t[T] = Tag.unwrap[Int, T] _以使 Tag 和 Int 之间没有区别,但这里要小心 - 任何非面向标签的函数都会删除标签)

另一种内联类型构造函数解决方案:

一)丑陋

scala> class ___
defined class ___

scala> class __[T,U] extends ___
defined class __

scala> val x = Tag[Symbol, ___ __ ___]('k1) -> 1
x: (scalaz.@@[Symbol,__[___,___]], Int) = ('k1,1)

scala> var y = Tag[Symbol, ___ __ ___ __ ___]('k1) -> 1
y: (scalaz.@@[Symbol,__[__[___,___],___]], Int) = ('k1,1)

scala> y = x
<console>:59: error: type mismatch;
 found   : (scalaz.@@[Symbol,__[___,___]], Int)
 required: (scalaz.@@[Symbol,__[__[___,___],___]], Int)
       y = x
           ^

scala> def mlp[X <: scalaz.@@[Symbol, _]](x: (X, Int)) = x
mlp: [X <: scalaz.@@[Symbol, _]](x: (X, Int))(X, Int)

scala> mlp(x)
res106: (scalaz.@@[Symbol,__[___,___]], Int) = ('k1,1)
Run Code Online (Sandbox Code Playgroud)

b) 有趣:

class - [B <: -[_, _], A <: symbolic[A]] (a: A, b: B) { 
    def -[T <: symbolic[T]](c: T) = new - (c, this)
}

trait symbolic[F <: symbolic[F]] { 
    def - [T <: symbolic[T]](b: T) = new - [single[F],T](b, new single(this.asInstanceOf[F]))
}

class single[T <: symbolic[T]](a: T) extends - [single[_],T](a, null)

val a = new a_; class a_ extends symbolic[a_]
val b = new b_; class b_ extends symbolic[b_]
val c = new c_; class c_ extends symbolic[c_] 
...

scala> val x = h-e-l-l-o -> 1
x: (-[o_,-[l_,-[l_,-[h_,end[e_]]]]], Int) = ($minus@350bc88,1)

scala> var y = h-e-l-l-o-o -> 2
y: (-[o_,-[o_,-[l_,-[l_,-[h_,end[e_]]]]]], Int) = ($minus@46d7fdc0,2)

scala> y = x
<console>:13: error: type mismatch;
 found   : (-[o_,-[l_,-[l_,-[h_,end[e_]]]]], Int)
 required: (-[o_,-[o_,-[l_,-[l_,-[h_,end[e_]]]]]], Int)
       y = x
           ^

scala> var z = h-e-l-l-o-o -> 2
z: (-[o_,-[o_,-[l_,-[l_,-[h_,end[e_]]]]]], Int) = ($minus@6b899d5d,2)

scala> z = y
z: (-[o_,-[o_,-[l_,-[l_,-[h_,end[e_]]]]]], Int) = ($minus@46d7fdc0,2)
Run Code Online (Sandbox Code Playgroud)