-description { "Task: {this:title} done: {this:done}". }
The answer is that I would like to do without return statements if and as much as I can. We will see how much that is. In general, I am in favor of expression-orientation in programming languages. A simple example is if-statements vs. conditional expressions. In most languages today, like C, Objective-C and Swift,
if
is a statement.
That means I write something as follows:
if ( condition ) {
do something if true
} else {
do something different if false
}
This seems obvious and is general, but very often you don't want to do arbitrary stuff, you just want to have some variable have some value in one case and a different value in another case.
int foo;
if ( condition ) {
foo = 1;
} else {
foo = 42;
}
In that case, it is annoying that the
if
is defined to be a statement and not an expression, because
you can't just write the following:
int foo;
foo = if ( condition ) { 1; } else { 42; }
In addition, as hinted to in the previous examples, you can't use a statement to initialize a variable, that definitely has to be an expression. Which is why C and many derived languages have the "ternary" operator (?:), which is really just an if/else in expression form.
int foo=condition ? 1 : 42;
That solves the problem, but now you have two conditionals. Why not have just one? LISP, most of the FP languages as well as Smalltalk and Objective-S have an
if
that returns a value.
a := condition ifTrue:{ 1. } ifFalse:{ 42. }.
So that's why expression-orientation is useful in general. What about methods? The same general idea applies. Whereas in Java, for example, a read accessor is called
getX()
, indicating
an action that is performed ("get the value of x") in Objective-C Smalltalk and Objective-S, it is
just called x
, ("the value of x").The same idea applies to dropping return statements where possible. It's not "get me the description of this object", it is "the description of this object is...". And inside the method, it's not "this statement now returns the following string as the description", but, again, "the description is...".
Describing things that are, rather than actions to perform, is at the heart of Objective-S, as discussed in Can Programmers Escape the Gentle Tyranny of Call/Return.
As Guy Steele put it:
Another weakness of procedural and functional programming is that their viewpoint assumes a process by which "inputs" are transformed into "outputs"; there is equal concern for correctness and for termination (and proofs thereof). But as we have connected millions of computers to form the Internet and the World Wide Web, as we have caused large independent sets of state to interact–I am speaking of databases, automated sensors, mobile devices, and (most of all) people–in this highly interactive, distributed setting, the procedural and functional models have failed, another reason why objects have become the dominant model. Ongoing behavior, not completion, is now of primary interest. Indeed, object-oriented programming had its origins in efforts to simulate the ongoing behavior of interacting real-world entities–thus the programming language SIMULA was born.So wherever possible, Objective-S tries to push towards expressing things as statically as possible, pushing away from action-orientation. For example, hooking up a timed source to a pin:
#Blinker{ #seconds: 1, #active: true} → ref:gpio:17.
instead of executing a loop:
while True:
GPIO.output(17, True)
sleep(1)
GPIO.output(17, False)
sleep(1)
The same goes for many other relationships: instead of writing procedural code that initiates and/or maintains the relatinship, with the actual relationship remainig implicit, describe the actual relationship, make that explicit, and instead keep the procedural code that maintains it as a hidden implementation detail.
If the return
statement comes back, and it very well might, I am hoping
it will be in a slightly more general form. I recall Smalltalk's "^" being described
as "send back". I've already taken that and generalised it to mean "send result", using it
in filter definitions, where "^" means "send a result to the next filter in the pipeline".
It is needed there because filters are not limited to sending a single result, they can
send zero or many.
With those more general semantics, "^" might also be used to send back results to the sender of an asynchronous message, which is obviously quite different from a "return".
And of course it would be useful for early returns, which are currently not possible.
What about void methods?
Objective-S does have void methods, after all its procedural part is essentially identical to Objective-C, which also has them. However, I agree with the FP folk that functions (procedures, methods) should be as (side-)effect free as possible, and void methods by definition are effectful (or no-ops).So where do the effects go? Two places:
- The left hand side of the "←".
In most current programming languages, assignment is severely crippled, and therefore not really useful for generalised effects. With Polymorphic Identifiers and Storage Combinators, there is enough expressive power and ability to abstract that we should need far fewer void methods.
- Connecting via "→"
Much of the need for effectful methods in OO is for constructing and connecting objects. In Objective-S, you don't need to call methods that result in a connection being established as a side effect of munging on some state, you define connections between objects directly using "→".
Well, and you define objects using object literals such as
#Blinker{ #seconds: 1, #active: true}
instead of setting instance variables procedurally.