Skip to content
bakkdoor edited this page Sep 14, 2010 · 5 revisions

Expressions

In Fancy almost everything is an expression and thus yields a value (an object). If there is no sensible return value, nil is returned.
In contrast to e.g. Ruby, a expression terminator is needed to let the parser be able to determine the end of one expression and the beginning of the next.
The usual expression seperator is a linebreak (newline character) that get used to seperate independent expressions within an expression list, usually inside class or method definitions – or generally any block of code that gets executed in order. An alternative is the semicolon (;), which can be used to seperate expressions on the same line (as in e.g. Ruby).

Example:

"hello, world" println
5 times: |i| { i println }

This would work as well (with the semicolon):

"hello, world" println;
5 times: |i| { i println }

Or even on a single line:

"hello, world" println; 5 times: |i| { i println }

Aside from the mentioned expression seperators for independent expressions, you might want to group expressions together. This is also common in most programming languages.

For example, in Ruby you might want to call a method on the result of another methodcall. You usually would do so like this:

var.method_1(param_1).method_2

Since Fancy has a Smalltalk-like keyword syntax for message sending, you’d have to use parentheses to group and distinguish one message send from another:

(var method_1: param1) method_2

As this is a common operation in object-oriented programming, Fancy offers two special operators to help with that: . (dot) and $ (dollar). Any Haskell programmer might know the dollar operator already.
Here’s an explanation though:

The Dot Operator

The Dot operator groups everything left to it into one expression and interprets everything right of it as a message send to the left expression. This makes chained message sends easier to type and reduces the overall amount of parentheses you need to use.

For example, "hello world" from: 0 to: 5 will return the substring beginning at index 0 and ending with index 5 (returning "hello"). If you then want to send a message to the return value from that expression, you would either need to use parentheses:

("hello world" from: 0 to: 5) each: |char| { char upcase println }

or you can use the Dot operator:

"hello world" from: 0 to: 5 . each: |char| { char upcase println }

It looks a little like the way you chain methods in most object-oriented languages (if you forget about the spaces in between).

The Dollar Operator

The Dollar operator has the exact opposite effect of the Dot operator. It groups everything right of it into one expression and interprets its value as an argument to the expression on the left. It has been copied from Haskell and also results in having to use less parentheses throughout your code. It’s main usage is for message sends with a large subexpression as an argument.

Example:

Console println: $ 10 upto: 20 . map: |x| { x squared } . join: ", "
instead of:
Console println: (((10 upto: 20) map: |x| { x squared }) join: ", ")

The output of the two equivalent expressions is: 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400

Clone this wiki locally