Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array>>#at:put: should return the stored value #65

Open
smarr opened this issue Feb 23, 2021 · 5 comments
Open

Array>>#at:put: should return the stored value #65

smarr opened this issue Feb 23, 2021 · 5 comments
Labels
spec Needs specification, or is specification related.

Comments

@smarr
Copy link
Member

smarr commented Feb 23, 2021

This is currently inconsistent between different SOM implementations.

The following program should result in the same output for all SOMs:

arr := Array new: 4.
(arr at: 1 put: 43) println.

Current behavior:

There's also an assumption in #25 that Array>>#at:put: returns the value just stored.

/cc @ltratt

@smarr smarr added the spec Needs specification, or is specification related. label Feb 23, 2021
@ltratt
Copy link

ltratt commented Feb 23, 2021

Which of those is the preferred / intended semantics?

@smarr
Copy link
Member Author

smarr commented Feb 23, 2021

Returning the just stored value seems the most useful semantics to me.
My guess is that the returning of the receiver was implemented in the original SOM, because it requires fewer stack operations. Not something that seems particularly useful.

@ltratt
Copy link

ltratt commented Feb 23, 2021

I have no strong opinions here, but returning the array does allow you to chain put's and the like.

@smarr
Copy link
Member Author

smarr commented Feb 23, 2021

@krono @fniephaus I checked on SqueakJS, and Squeak returns the value instead of the array.
From your perspective, would there be a reason to deviate from it?
Well, I guess in Squeak the question isn't really one because it got message cascades.

@fniephaus
Copy link

fniephaus commented Feb 23, 2021

Here's what ArrayedCollection class>>#with:with:with:with:with:with: looks like in Squeak:

with: firstObject with: secondObject with: thirdObject with: fourthObject with: fifthObject with: sixthObject
	"Answer a new instance of me, containing only the 6 arguments as elements."

	| newCollection |
	newCollection := self new: 6.
	newCollection at: 1 put: firstObject.
	newCollection at: 2 put: secondObject.
	newCollection at: 3 put: thirdObject.
	newCollection at: 4 put: fourthObject.
	newCollection at: 5 put: fifthObject.
	newCollection at: 6 put: sixthObject.
	^ newCollection

That could of course be rewritten using a cascade. If at:put: returned self, I'd still prefer a cascade over nested brackets:

(Array new: 2)
 at: 1 put: true;
 at: 2 put: false;
 yourself

vs.

((Array new: 2) at: 1 put: true) at: 2 put: false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
spec Needs specification, or is specification related.
Projects
None yet
Development

No branches or pull requests

3 participants