-- Oliver
@Sebastian: Can you set an alias from this to self in Scala ;-)) ? Smalltalkers understand ...
-- Oliver
turtle.cascade(
_.clear,
_.forward(10),
_.turn(90),
_.forward(20),
_.yourself)
In order for the whole expression to evaluate to the List being populated and correctly assigned to "myInstance", the last message sent to this collection needs to be "yourself":myInstance := (OrderedCollection new)add: 'Peter';add: 'John';add: 'Paul';yourself.
Now, to the other question, the reasons why the default value of methods is "self" (or "this") is that by returning "self", you get method chaining for free
Request r = new RequestBuilder().setUrl("url")Java libraries will usually work with mutable instances that get changed by the builder. Scala libraries, on the other hand, often offer immutable instances whose methods return new instances with further modifications. That's what collection manipulation usually looks like.
.setRealm((new Realm.RealmBuilder()).setPrincipal(user)
.setPassword(admin)
.setRealmName("MyRealm")
.setScheme(Realm.AuthScheme.DIGEST).build());
r.execute();
Okay, but this leaves the most baffling question unanswered: why does it return 'Paul' if you omit yourself?
Sure, but that's not what you get by default; if you got it by default then you wouldn't need to use "yourself".
val myInstance = new OrderedCollection {
add "Peter"
add "John"
add "Paul"
}
Okay, but this leaves the most baffling question unanswered: why does it return 'Paul' if you omit yourself?
This seems like either an implementation detail of the Smalltalk VM leaking through
Scala, like Java in recent years, favor fluent interfaces, particularly with build pattern, such as in the Java library example below:
With builders (when mutable) this situation occurs again, except that there is a strong bias for a chainable api with builders.
Are there other typical uses of this 'yourself' feature in smalltalk besides collections and object construction? In those two cases it does not play well with immutability or is replaced with the builder pattern.
Getting something 'builder like' for free is interesting, but setting fields one at a time after construction is not an immutable object builder pattern.
Are there other typical uses of this 'yourself' feature in smalltalk besides collections and object construction?
implicit class YouAreYourself[A](val you: A) extends AnyVal { def yourself: you.type = you }
It comes cheap in any language with extension methods
The builder pattern is useful in Scala and with immutable data structures when the data structure performance is poor when building it one piece at a time (see Vector) or for classes with many fields when named parameters and defaults are not sufficiently clear and flexible.
turtle.cascade(
_.clear,
_.forward(10),
_.turn(90),
_.forward(20),
_.yourself)
implicit class IdW[A](a: A) {class Chain(f: A => Unit) {def andAlso(g: A => Unit) = new Chain(a => { f(a); g(a) })def run = f(a)}def chaining(f: A => Unit) = new Chain(f)}
scala> class X(var i: Int, var s: String, var f: Float) { override def toString = i + " " + s + " " + f }defined class Xscala> val x = new X(0, "", 0.0F)x: X = 0 0.0scala> x chaining (_.i = 4) andAlso (_.s = "a") andAlso (_.f = 1.4F) runscala> xres7: X = 4 a 1.4