« Return to Thread: Literal instance slots, 10 foo := 1

Re: Magic Literal instances

by spir :: Rate this Message:

Reply to Author | View in Thread

Le Sat, 25 Apr 2009 15:16:57 -0700,
Steve Dekorte <steve@...> s'exprima ainsi:

>
> On 2009-04-25, at 11:55 AM, Mike Austin wrote:
> > In Io, I know it's possible to set a slot on a specific literal, for  
> > example:
> >
> > Io> 10 foo := 1
> > ==> 1
> > Io> 10 foo
> > ==> 1
> >> 20 foo
> > Exception: Number does not respond to 'foo'
>
> The caveat is that small numbers (IIRC -10 to 100) are global as a  
> performance optimization.

But it seems that any other literal can be shared as well -- see below.

> > 1. Does it introduce any overhead, or only when the first slot is set?
>
> There's no special overhead.
>
> > 2. Is there a good use for this functionality, other than being  
> > "correct"?
>
> I would guess there are, though I haven't run across them.

I am surprised of the following:

Io> "t" flags := "1010101"
==> 1010101
Io> s := "t"
==> t
Io> s flags
==> 1010101
Io> ss:="t" ; ss flags
==> 1010101

(works also for longer strings)
How come literal values can be shared? Have the impression I must be messing up distinct things -- or overlooking something obvious.
Beyond the design choice, how does Io know that the right-side value happens to be equal to an already-created object? I guess it does not perform a lookup in the set of all currently created things...?
It works like if I had previously named "t" and then always used that name, instead of the value.

More magic:

Io> "tt" flags := "1010101"
==> 1010101
Io> s = "t" .. "t"
==> tt
Io> s flags
==> 1010101

Probably a consequence of the same reason I cannot figure out.
And even more ;-)
Io> t := "tt" ; t flags := "1"
==> 1
Io> s := "tt" ; s flags
==> 1
Io> ss = "t" .. "t" ; ss flags
==> 1

> - Steve

Below is written: "life is weird" ;-)

PS: What about changing Io's command-line prompt '>' to any other (well-chosen) char? Because when pasted into an email post they look like quotations (in most mail-clients).
(You would have to change '==>', too. chr(187)='»' may do the job:
Io: s flags
»»» 1010101
)
Same issue with Python's '>>>'. Guido has resisted for at least 15 years already -- and still does ;-). I'll pay a dozen liters of good belgian beer to anyone who provides a pointer to the reason.

Denis
------
la vita e estrany

 « Return to Thread: Literal instance slots, 10 foo := 1