Literal instance slots, 10 foo := 1

View: New views
10 Messages — Rating Filter:   Alert me  

Literal instance slots, 10 foo := 1

by Mike Austin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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'

1. Does it introduce any overhead, or only when the first slot is set?
2. Is there a good use for this functionality, other than being "correct"?

Thanks,
Mike


Re: Literal instance slots, 10 foo := 1

by Steve Dekorte :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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.

> 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.

- Steve

Re: Magic Literal instances

by spir :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Re: Magic Literal instances

by Steve Dekorte :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 2009-04-26, at 2:13 AM, spir wrote:
> (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.

String literals are symbols. Symbols are global and there is only one  
instance of each. Even if a symbol isn't referenced, it will live  
until the garbage collector removes it. For example:

Io> "foo" uniqueId
==> 6564192
Io> "foo" uniqueId
==> 6564192
Io> Collector collect
==> 5196
Io> "foo" uniqueId
==> 5405424


Re: Magic Literal instances

by Steve Dekorte :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


FTM, literals in Io aren't special in any way - they are just normal  
objects that are set on a Message object's "cachedResult" slot. In  
this way, you can make anything you like a "literal".


Re: Magic Literal instances

by quercerandjanath :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

--- In iolanguage@..., Steve Dekorte <steve@...> wrote:

>
>
> On 2009-04-26, at 2:13 AM, spir wrote:
> > (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.
>
> String literals are symbols. Symbols are global and there is only one  
> instance of each. Even if a symbol isn't referenced, it will live  
> until the garbage collector removes it. For example:
>
> Io> "foo" uniqueId
> ==> 6564192
> Io> "foo" uniqueId
> ==> 6564192
> Io> Collector collect
> ==> 5196
> Io> "foo" uniqueId
> ==> 5405424

I like this behaviour, and it doesn't seem at all like magic to me.  It seems more consistent than the way other languages treat literals.  Literals in Io work just like other objects.  Good!


Re: Magic Literal instances

by spir :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Le Sun, 26 Apr 2009 02:28:20 -0700,
Steve Dekorte <steve@...> s'exprima ainsi:

>
> On 2009-04-26, at 2:13 AM, spir wrote:
> > (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.
>
> String literals are symbols. Symbols are global and there is only one  
> instance of each. Even if a symbol isn't referenced, it will live  
> until the garbage collector removes it. For example:
>
> Io> "foo" uniqueId
> ==> 6564192
> Io> "foo" uniqueId
> ==> 6564192
> Io> Collector collect
> ==> 5196
> Io> "foo" uniqueId
> ==> 5405424
>

Thank you.
There's still something I don't get.

Io> "foofoo" attr := 7
==> 7
Io> s = "foo" .. "foo" ; s attr
==> 7

Io> "foofoo" uniqueId
==> 158710360
Io> s = "foo" .. "foo" ; s uniqueId
==> 158710360

How does Io proceed to keep a single instance of "foofoo" in such a case? How does it "know" there is already a "foofoo" somewhere?
Also, does this apply to all literals?

Denis
------
la vita e estrany

Re: Magic Literal instances

by Guy Hulbert :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sun, 2009-26-04 at 13:16 +0200, spir wrote:
> How does Io proceed to keep a single instance of "foofoo" in such a
> case? How does it "know" there is already a "foofoo" somewhere?

Hash table ?

> Also, does this apply to all literals?

I think Steve said that already.

--
--gh



Re: Magic Literal instances

by Steve Dekorte :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 2009-04-26, at 4:16 AM, spir wrote:
> Io> "foofoo" uniqueId
> ==> 158710360
> Io> s = "foo" .. "foo" ; s uniqueId
> ==> 158710360
>
> How does Io proceed to keep a single instance of "foofoo" in such a  
> case? How does it "know" there is already a "foofoo" somewhere?

There is an internal symbol hash table in the IoState. When the append  
method creates the new character array, it looks to see if there is  
already a symbol with the same string, if so it will return it and  
discard the temporary array. If not, it will create a new symbol using  
the array.

Btw, a Symbol is just an immutable Sequence and Sequences can contain  
any of the standard C data types. So you could do things like have a  
Symbol containing a vector of 64 bit floats, etc.

- Steve

Re: Magic Literal instances

by Mike Austin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

quercerandjanath wrote:

> --- In iolanguage@... <mailto:iolanguage%40yahoogroups.com>,
> Steve Dekorte <steve@...> wrote:
>  >
>  >
>  > On 2009-04-26, at 2:13 AM, spir wrote:
>  > > (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.
>  >
>  > String literals are symbols. Symbols are global and there is only one
>  > instance of each. Even if a symbol isn't referenced, it will live
>  > until the garbage collector removes it. For example:
>  >
>  > Io> "foo" uniqueId
>  > ==> 6564192
>  > Io> "foo" uniqueId
>  > ==> 6564192
>  > Io> Collector collect
>  > ==> 5196
>  > Io> "foo" uniqueId
>  > ==> 5405424
>
> I like this behaviour, and it doesn't seem at all like magic to me. It
> seems more consistent than the way other languages treat literals.
> Literals in Io work just like other objects. Good!
>

I agree, literals seem to just fit in like everything else.

Mike