3

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???

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 declarations)(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 Black for fill for instant 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.

Update:

Well now I know that it is calling a setter via syntax sugar however I check that setter and I don't understand what is going on there

Check it out:

def fill: ObjectProperty[jfxsp.Paint] = delegate.fillProperty
  def fill_=(v: Paint) {
    fill() = v
}

how come the setter is calling the getter to update the value?

delegate.fillProperty

is a function that return a value

legramira
  • 598
  • 6
  • 13

1 Answers1

0

The syntax for anonymous classes in Scala is borrowed from Java; you can see it here, too:

class Coder {
    protected String name;
    public Coder(String name) {this.name = name;}
}

public static void main(String[] args) {
    // Prints "Mr. Noble"
    new Coder("Noble") {
        private String prefix = "Mr.";
        {
           System.out.println(prefix + " " + name);
        }
    }
}

Because Scala lets you write your constructor code in the class body, all of your x = y statements are executed when you instantiate those anonymous classes. As @resueman points out, these are actually getters and setters in this format:

class Scene { 
    var _fill
    def fill_=(color: Color) // Setter, has some hidden logic to set up the UI
    def fill = _fill // Getter
}

And your statement fill = Black is desugared to fill_=(Black).

Schalat
  • 116
  • 1
  • 1