简体   繁体   English

Scala val语法:val myVal:{def ...}是什么意思?

[英]Scala val syntax: What does val myVal:{ def … } mean?

I am new to Scala and to funcprog. 我是Scala和funcprog的新手。

I have a piece of code (some of you might recognize it): 我有一段代码(你们中的一些人可能会认出来):

    trait SwingApi {

      type ValueChanged <: Event

      val ValueChanged: {
        def unapply(x: Event): Option[TextField]
      }
      ...
     }

where I do not underestand what val ValueChanged: {...} is. 在哪里我没有动摇和val ValueChanged:{...}是什么。

From this post I sort of learned that 从这篇文章我有点了解到

type ValueChanged <: Event

and

val ValueChanged: {
            def unapply(x: Event): Option[TextField]
          }

are two unrelated things because they are in different namespaces, etc, and type ValueChanged is an abstract subtype of Event . 是两个不相关的东西,因为它们在不同的命名空间等,而类型ValueChangedEvent的抽象子类型。

Good, then I try in a Scala worksheet: 好的,然后我在Scala工作表中尝试:

type myString <: String

 val myString: {
    def myfunc(x: String): String
  }

and it shows me an error "only classes can have declared and undefined members"... Isn't it a similar construction? 它显示了一个错误“只有类可以声明和未定义的成员”......这不是一个类似的结构吗?

Finally, the questions are: 最后,问题是:

  • what is ValueChanged in val ValueChanged part of code? val ValueChanged代码中的ValueChanged是什么?

  • is it truely so unrelated to type ValueChanged <: Event 是否真的与TypeChanged <:Event类型无关

  • what does this syntax mean: 这个语法是什么意思:

    val myVal:{def func{x:T}:T} val myVal:{def func {x:T}:T}

? What's the name of the value, its type and its actual value here? 这里的价值名称,类型和实际价值是多少?

Thanks! 谢谢!

{def unapply(x: Event): Option[TextField]}

is a structural Type, it means that it accepts any Object that has an unapply method with a Event as Parameter and Option[TextField] as return value. 是一个结构类型,它意味着它接受任何具有unapply方法的Object,其中Event为Parameter,Option [TextField]为返回值。 It is most often used similar to Duck typing, eg: 它通常类似于Duck类型,例如:

def foo(canQuack: {def quack(): Unit}) = {
  canQuack.quack()
}
object Bar{
   def quack(): Unit = print("quack")
}
object Baz{
   def bark(): Unit = print("bark")
}
foo(Bar) //works
foo(Baz) //compile error

so 所以

type StructuralType = {def unapply(x: Event): Option[TextField]}
val ValueChanged: StructuralType

declares a val with the name ValueChanged and with the Type StructuralType, but assigns no value, that makes only sense in a trait or abstract class, thats why your example doesnt work. 声明一个名为ValueChanged并使用Type StructuralType的val,但是没有赋值,这只在特征或抽象类中有意义,这就是为什么你的例子不起作用的原因。

So what 所以呢

trait SwingApi {
...
  val ValueChanged: {
    def unapply(x: Event): Option[TextField]
  }
...
}

means is that the trait SwingApi can only be applied to Objects/Classes that have a val with the name ValueChanged and whatever value is assigned to it, has an unapply method 意味着特征SwingApi只能应用于具有名称为ValueChanged的val的对象/类以及分配给它的任何值,具有unapply方法

trait SwingApi {
  val ValueChanged: {
    def unapply(x: Event): Option[TextField]
  }
}
//works:
object Bar extends SwingApi{
  val ValueChanged = {
    def unapply(x: Event): Option[TextField] = None
  }
}
//compile error:
object Baz extends SwingApi{
  val ValueChanged = {
    //wrong name
    def foo(x: Event): Option[TextField] = None
  }
}
//compile error:
object Baz2 extends SwingApi{
  val ValueChanged = {
    //wrong input/output type
    def unapply(): Unit = {}
  }
}

All code untested 所有代码未经测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM