Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala val syntax: What does val myVal:{ def ... } mean?

I am new to Scala and to 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.

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.

Good, then I try in a Scala worksheet:

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?

  • is it truely so unrelated to type ValueChanged <: Event

  • what does this syntax mean:

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

? What's the name of the value, its type and its actual value here?

Thanks!

like image 768
alisa Avatar asked Mar 16 '23 00:03

alisa


1 Answers

{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. It is most often used similar to Duck typing, e.g.:

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.

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

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

like image 60
Siphor Avatar answered Mar 19 '23 02:03

Siphor