Scala - instantiation of a class with curly braces

217 views
Skip to first unread message

Erwin Ramírez Millán

unread,
Mar 5, 2015, 5:39:35 PM3/5/15
to scala...@googlegroups.com

I am starting with Scala and with ScalaFX, I understand most of the code but I don't understand this code using for the examples in ScalaFx;

where instantiate an anonymous class follow it by curly braces, How this works???

import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.geometry.Insets
import scalafx.scene.Scene
import scalafx.scene.effect.DropShadow
import scalafx.scene.layout.HBox
import scalafx.scene.paint.Color._
import scalafx.scene.paint.{LinearGradient, Stops}
import scalafx.scene.text.Text
object ScalaFXHelloWorld extends JFXApp {

  stage = new PrimaryStage {

    title = "ScalaFX Hello World"

    scene = new Scene {

      fill = Black

      content = new HBox {

        padding = Insets(20)

        children = Seq(
          new Text {
            text = "Hello"
            style = "-fx-font-size: 48pt"
            fill = new LinearGradient(
              endX = 0,
              stops = Stops(PaleGreen, SeaGreen)
            )
          },
          new Text {
            text = "World!!!"
            style = "-fx-font-size: 48pt"
            fill = new LinearGradient(
              endX = 0,
              stops = Stops(Cyan, DodgerBlue)
            )
            effect = new DropShadow {
              color = DodgerBlue
              radius = 25
              spread = 0.25

            }
          }
        )

      }

    }

  }

}


the part I don't understand is why in the creation of an anonymous class is follow by curly braces (with some more statements)(Scene is not a trail to be filling the abstract parts of that class) and even fill or content are functions not a variables and for instant Black for fill is a val meaning that this line

fill = Black

is doing calling a function fill and assigning a val to it(don't make sense for me ), this is fill definition 

def fill: ObjectProperty[jfxsp.Paint] = delegate.fillProperty

and this is Black 

val Black = new Color(jfxsp.Color.BLACK)

how works this instantiation of a new object with curly braces please help, want to understand. This is because ScalaFx is wrapping JavaFx and something special is going on here?. Thank you guys.


Oliver Ruebenacker

unread,
Mar 5, 2015, 5:49:49 PM3/5/15
to Erwin Ramírez Millán, scala-user

     Hello,

  You can do it just like this:

scala> class A { var x: Int = 0 }
defined class A

scala> val a = new A { x = 2 }
a: A = $anon$1@1787bc24

  So, new A {...} creates, as you pointed out, an anonymous class. It extends A. Here, constructor of A assigns 0 to x, and then the constructor of the anonymous class re-assigns 2 to x.

     Best, Oliver

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Solutions Architect at Altisource Labs
Be always grateful, but never satisfied.

Jon Pretty

unread,
Mar 5, 2015, 5:53:01 PM3/5/15
to Erwin Ramírez Millán, scala...@googlegroups.com
Hi Erwin,

I'm not familiar with ScalaFX, but I'll try to answer.

You're right that there are a variety of anonymous classes here. In your example, no `val`s, `var`s or `def`s are getting (re)defined, so everything that appears between the curly braces of the anonymous class is side-effecting code that is run during instantiation of those classes.

It *looks* like these calls are reassigning new values to `var`s, but given you've just shown me that, for example, `fill` is implemented by a `def`, this means that it can't be a `var`. A call to `someIdentifier = value` will get translated by the compiler into a call to `someIdentifier_=(value)` if `someIdentifier` is not a `var`. You should find that there's a method defined something like `fill_=(c: Color)`. So where you're seeing `fill = Black`, that's becoming `fill_=(Black)`.

This sort of side-effecting code in constructors is discouraged, though. Not many projects are using this style any more.

Cheers,
Jon

Reply all
Reply to author
Forward
0 new messages