Pipe and initializer operators

These two binary infix operators simplify some frequent scenarios

Pipe operator

This operator allows to pass the value from left operand to the right operand and return the result of the right operand.

readLine() -> log(_)  // it's the same as log(readLine())

readLine() ->
   _ == "" ? log("empty line")
               : process(_);

// It's the same as
{
   x = readLine();
   x == "" ? log("empty line")
           : process(x)
};

Initializer operator

This operator allows to perform arbitrary operations on some value (usually newly created) and pass this value further:

form.add(Button.{
     _.flexWidth := 20;     // assign to button fields 
     _.setPaddings(5);      // call button's methods

     // create a delegate associated with the button and store it in the button field
     _onClick := _.&onOk(){ log("Ok") }
     myForm.ok := &_;      // save a weak pointer to the button into some other object
});

// It's the same as
form.add({
   btn = Button;
   btn.flexWidth := 20;
   btn.setPaddings(5);
   btn.onClick := btn.&onOk(){ log("Ok") }
   myForm.ok := &btn;
   btn
});

Leave a Reply

Your email address will not be published. Required fields are marked *