'this' and its block

In conversations, we often use pronouns like "she" and "this" as a convenient way to refer to some specific person or thing we are talking about. Cone uses the pseudo-variable this for the same purpose. It allows a block of code to focus its attention on some specified value referred to by this.

A 'this' block is simply an expression followed by a code block. Throughout the block, this is understood to hold the value of the specified expression. Multiple actions may be performed using that value.

// Calculate the value of 'this' (0.174533)
Float.Pi/180
	// 'this' block that makes use of calculated 'this' value
	// to convert from degrees to radians
	quarter = 90 * this
	acute = 10 * this

'this' blocks can be nested within each other. For any statement, the value of this is established by the inner-most 'this' block it lies within.

Although a 'this' block can focus on any value, it is typically a copyable or borrowed mutable reference to some compound or collection value whose parts we want to read or modify or whose methods we want to invoke over a sequence of statements. 'this' blocks offer an easy-to-read and more versatile alternative to method chaining or method cascades.

Operator support for implicit 'this'

To make use of this more convenient and concise, several operators implicitly work with this. When these operator are used as a prefix (rather than as infix operators), this is implied to the left.

.
Method call or field access. (.size is equivalent to this.size)
<<
Append, placing the value at the end of an ordered collection. (<<4.7 is equivalent to this<<4.7)
>>
Prepend, inserting the value at the start of an ordered collection. (>>event is equivalent to this>>event)

Additionally, the ':' infix operator implicitly works with 'this':

color: 'brown'    # equivalent to: this.color = 'brown'

So far, 'this' blocks and operators sound rather dull and boring... What value does this syntactic sugar offer? Let's finish with examples that illustrate how use of 'this' sugar makes code more concise and readable.

Method chaining/cascade:

// Perform several methods on on root
root.findById("canvas")
	.add(anode)                    // Unlike chaining, the method need not return self
	.hide(.statuspane)
	angle = 20 * .degrees          // Unlike cascades, any logic can be placed between method calls
	.add(anode2)
	.add(node) each node in nodes  // ... even within conditional branches and loops

Formatted stream output or string creation:

console
	<< "Score: "       // Append to console stream
	color: blue        // Alter a field
	<< score           // Append with auto-conversion
	color: white
	<< "!\n"
	.flush             // Invoke methods

console << {"And another thing ";name;"!\n"}

List construction (or expansion):

// Use of << at the start means append all the block's expression values
names = &own List() <<
	me
	"Judy"
	"Davos"
	you.middlename

Dictionary construction:

toDutch = &gc Dict{Text, Text}()
	"girl": "meisje:
	"horse": :paard"
	"dog": "hond"

Builder blocks

_