[Vm-dev] Fwd: MiscPrimitive plugin: plan for next 6 months, please read

Levente Uzonyi leves at caesar.elte.hu
Fri Dec 29 02:18:57 UTC 2017

On Thu, 28 Dec 2017, Clément Bera wrote:

> Thing is instVarAt: works for everything
> #[10] instVarAt: 1 => 10
> #(10) instVarAt: 1 => 10
> So why having negative values ?
> Could it only answer a positive index or 0 and never a negative index ?
> Especially I think this makes most sense for variable objects, data or 
pointers, and I don't think negative values there makes sense.

I see three potential use cases (listed by decreasing genericity):
(A) to ask the index of the indexable field or the receiver pointing to 
the argument (similar to primitiveIndexOfAsciiInString)
(B) to ask whether or not the receiver points to argument (same as 
primitive 132)
(C) to ask which field (let it either be indexable or a variable) 
of the receiver points to the argument (handy in pointer explorers)

If the new primitive returned the absolute index (the one which can be 
used with #instVarAt:) as you proposed, then (A) would require subtracting 
the receiver's instance variable count. (B) would not be affected, (C) 
would require a comparison with the number of instance variables of the 

With the sign approach, (A) and (B) would just work, and (C) would only 
require a sign check.

> I don't really mind having a more generic numbered 
primitiveIndexOfAsciiInString instead of a string specific one.
> Maybe #indexOf: value startingAt: startInteger to: lastInteger

I'd call it #identityIndexOf:..., since that's what a primitive can do.
The start and end indices would be useful too, but I'd prefer them be 
optional with the default values 1 and nil.


> So it can be used easily for OrderedCollection too:
> OrderedCollection>>#indexOf: anElement startingAt: start ifAbsent: 
> ^(index := array 
> indexOf: anElement 
> startingAt: firstIndex + start - 1 
> to: lastIndex) = 0 
> ifTrue: [exceptionBlock value]
> ifFalse: [index]
> instead of:
> OrderedCollection>>#indexOf: anElement startingAt: start ifAbsent: 
> firstIndex + start - 1 to: lastIndex do: [ :index |
> (array at: index) = anElement ifTrue: [ ^index - firstIndex + 1 ] ].
> ^exceptionBlock value

More information about the Vm-dev mailing list