[squeak-dev] The Trunk: Compiler-eem.480.mcz

Tobias Pape Das.Linux at gmx.de
Thu Nov 24 20:18:33 UTC 2022

Hi Eliot

First things first, I did not raise objections in my admittedly short quip.

> On 24. Nov 2022, at 20:23, Eliot Miranda <eliot.miranda at gmail.com> wrote:
> Hi Tobi,
>     let me try again (https://youtu.be/Cj8n4MfhjUc)…


I already got my comfy chair!

>> On Nov 23, 2022, at 12:23 PM, Tobias Pape <Das.Linux at gmx.de> wrote:
>> Yet, nil is only seldom a good domain object.
> Precisely. Being disjoint from any domain it is the ideal “I am not a domain object” marker. So when one wants a variable to range over a domain and the singleton “not a member of the domain” nil is a great choice.  And that’s exactly how I use it below.

Second things second, I got that.

> There is another excellent marker of a non-domain object, and that is a newly instantiated object. That object is known to not be any other object, since objects are unique.  So if code is searching for something (eg applying a block to every literal in the system), having the newly instantiated object that implements the search use itself as the “I’m not in the domain of all pre-existing objects” is a sensible choice.  This is the pattern InstructionStream uses when scanning for selectors.

And #someObject/#nextObject.
I get that. And it is actually a beautiful thing you cannot do everywhere[0].

My fear is as follows:

I hope we can agree that "nil" is part of the "system domain" of Smalltalk, or - said differently - the meta-level.

So are the concepts of variables, classes etc. 

The non-meta, base, or "domain proper" layer can be anything you want to computationally achieve.[1]
Let's arbitrarily chose finance[2].

A domain object would be an account, a transaction, an account holder, etc.
(Note that we can chose how to represent each, and we do not necessarily need objects for each, but I digress).

My take: in code dealing with such domain objects, nil should appear next to never, because it is an object from the Metalevel.

The problems with accepting nil as the general "nothing to see" marker include:

- There are too many.
  In our example, an account holder could have an instVar "account" which could be nil when not having an account yet, BUT ALSO
  an account could have a "closingDate" for when the account folded, which is "nil" when the account is still open, AND ALSO, a transaction could have
  an "auditor" which is nil as long as no audit has taken place etc. 
    Just like that, nil takes _different roles_ just by being convenient.

- Nil has problematic provenance.
  When somewhere during debugging (the all-known MNU for UndefinedObject) a nil pops up, it is often reallllly hard to say whence it came from.
    So dealing with a lot of nil-bearing code will send you down rabbit holes after the other.

- Nil begets polymorphism, nil defies polymorphism.
  It is one of the most awesome feats of Smalltalk that nil is NOT like NULL, in that it can respond to messages. That is exceptionally powerful
  and has given Smalltalk a lot of resilience.
   But cluttering UndefinedObject with custom, even domain-specifc methods is a really bad idea. However, that means
  it is often unwise to just have object that could be nil be sent arbitrary messages.
  Hence a multitude of #isNil/#ifNil-Checks. Proper domain objects that model absence, pre-valid state, or error conditions can deal much better with that.

- Nil is in a collection-superposition (just like your good old USB-A plug which you have to turn at least twice to fit).
  You only know whether nil _actually_ could be a collection when you know that a non-nil object in its place is a collection [3].
  Said differently: in contrast to LISPy languages, our nil is _by design_ no collection, while LISPy null _by design_ IS the empty list.
  This makes for funny messages like #isEmptyOrNil, which bails on non-nil-non-collection objects.
    So every time you have to deal with nil, you automatically at lease once have to answer the question "could the non-nil version of this object be a collection"?

There are a lot of interesting approaches to each or combinations of these issues.
This includes Null-Object patterns, Sane-default-initializers, exceptions, or explicit models of multiplicity[4].

But back to the beginning.

In code that does, for example


  | fooAccount |
  self accounts processBla: 
    [:ea |
    fooAccount ifNil: [fooAccount := ea].
    fooAccount := (ea doesBork: fooAccount)
      ifTrue: [fooAccount]
      ifFalse: [ea]]
  ^ fooAccount

we find two things:

First, we could inadvertently return nil from that method. But this is technical and I think most here can deal with that.

But second, the line "fooAccount ifNil: [fooAccount := ea]." ACTUALLY says

  "if fooAccount is an uninitialized temporary variable, populate it".

This is technically correct, but conflates domains.
In our world of finance, the idea of a "temporary variable" does no make sense. It is part of the meta-level domain, the system.

I don't say this is wrong _per se_ but people reading, and even more so, people writing such code 
MUST be aware that they are crossing domains, and especially, entering a meta level.

That's why I think these warnings are really ok.
I won't fight the commit "Compiler-eem.480.mcz", especially since it more or less is descriptive of a pervasive style of writing Smalltalk of Squeak Core contributors.

I hope people find theses ideas useful.
Best regards

[0]: I've used it in Python code to much joy.
[1]: Caveat lector: the "domain layer" can surely be the "system layer". In fact that is what a lot of system code deals with. 
     But this is messy for our considerations above and lets treat it as _exceptional_.
[2]: Semi-arbitrarly, just because I received my tax returns :P
[3]: yes, that is a strange sentence. It's late. Also, Ardbeg.
[4]: For example, as in the work of Steimann (https://dl.acm.org/doi/10.1145/2509578.2509582 ). It seems they had a Smalltalk implementation in 2017.

>> -t
>>> On 23. Nov 2022, at 19:34, tim Rowledge <tim at rowledge.org> wrote:
>>> I won't quote it all again but what Eliot wrote is important. There are good solid reasons why Smalltalk has a rigorously defined UndefinedObject. We demand rigorously defined areas of doubt and uncertainty!
>>> tim
> _,,,^..^,,,_ (phone)

More information about the Squeak-dev mailing list