相关疑难解决方法(0)

什么是self =>用于?

可能重复:
"outer =>"究竟是什么意思?

当我查看源代码:scala/src/library/scala/Option.scala

sealed abstract class Option[+A] extends Product with Serializable {
  self =>
Run Code Online (Sandbox Code Playgroud)

我喜欢自己用的东西.我知道自我类型的正常用法是限制特征可以混合的类.例如:

scala> trait A
defined trait A

scala> trait NeedA {self: A =>}
defined trait NeedA

scala> new NeedA {}
<console>:10: error: illegal inheritance;
 self-type java.lang.Object with NeedA does not conform to NeedA's selftype NeedA with A
              new NeedA {}
                  ^

scala> new NeedA with A {}
res39: java.lang.Object with NeedA with A = $anon$1@4d04a0e8

scala>
Run Code Online (Sandbox Code Playgroud)

但"this =>"并非如此.这个"this =>"究竟用于什么?

scala

5
推荐指数
1
解决办法
238
查看次数

Scala outer =>语法

可能重复:
"outer =>"究竟是什么意思?

在哪里可以找到有关的信息

trait After extends Context { outer => xxx
//...
}
Run Code Online (Sandbox Code Playgroud)

什么意思外面=>

scala

5
推荐指数
1
解决办法
1334
查看次数

作为自身列表的类的模式

考虑以下:

object Main
{
  case class Foo(bar: Int) extends FooList {
    val self: List[Foo] = this :: Nil
  }

  abstract class FooList {
    val self: List[Foo]
    def ~(that: Foo) = { val list = self :+ that; new FooList { val self = list } }
  }

  def main(args: Array[String]): Unit = {
    val foo = Foo(1) ~ Foo(2) ~ Foo(3)
    println(foo.self)
 }
}
Run Code Online (Sandbox Code Playgroud)

这行可以:

{ val list = self :+ that; new FooList { val self = list } …
Run Code Online (Sandbox Code Playgroud)

scala

3
推荐指数
1
解决办法
365
查看次数

Scala:下划线的另一种用法?

我正在查看scala actors库,在那里我找到了以下代码:

private[scheduler] trait TerminationMonitor { 
    _: IScheduler =>

    protected var activeActors = 0 
    ...
Run Code Online (Sandbox Code Playgroud)

问题是_的含义是什么:IScheduler =>在这里?
我是Scala的新手,让我感到困惑的是,下划线有很多不同的含义.

感谢您的帮助!

scala

3
推荐指数
1
解决办法
225
查看次数

标签 统计

scala ×4