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

Implement instVarAt:[put:] #152

Merged
merged 4 commits into from
Jun 16, 2020
Merged

Implement instVarAt:[put:] #152

merged 4 commits into from
Jun 16, 2020

Conversation

ltratt
Copy link
Member

@ltratt ltratt commented Jun 15, 2020

These are two relatively simple primitives, although while doing this, I realised that some existing functions should be explicitly marked as unsafe (54422e8).

stderr:
Traceback...
...
Index 2 not valid for array of length 1.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, not the most ideal error message.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conceptually I think instance values are an array, so this seems reasonable enough to me. [I half expected them to be exposed as an Array as methods are!]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't something like "instVarAt: index 2 out of range" be more user-friendly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but not worth the effort.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'll leave that one up to you.

@smarr
Copy link
Contributor

smarr commented Jun 15, 2020

Another test could be with a superclass that has fields.

@vext01
Copy link
Member

vext01 commented Jun 16, 2020

I realised that some existing functions should be explicitly marked as unsafe

Can you expand upon this? The fact that it compiled without the unsafe keyword suggests that -- at-least from a Rust perspective -- the functions are safe. I assume that you are using unsafe as a cautionary note to the programmer then?


run = (
x := 5.
(self instVarAt: 2) println.
Copy link
Member

@vext01 vext01 Jun 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideas for more tests:

  • Getting a larger valid index (e.g. index 5 out of 7).
  • Getting a negative index should fail(?) with appropriate error message.
  • Zero index should fail(?) with appropriate error message.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right... but it's just too much not-very-useful effort to do this for every indexing operation in SOM.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's up to you. I tend to test the edge cases, as I don't trust my ability to get it right first time ;)

@ltratt
Copy link
Member Author

ltratt commented Jun 16, 2020

The fact that it compiled without the unsafe keyword suggests that -- at-least from a Rust perspective -- the functions are safe.

That's not how safety in Rust works. You can package up arbitrary amounts of deadly unsafe code and if you don't mark the function as unsafe, callers will never know. unsafe on a function is really just a reminder to code readers that they need to be extra careful when calling it.

status: success
stdout:
5
instance of inst_var_at_put
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My curiosity gets the better of me again.

This is unusual. So this is some kind of proxy? Is it transparent? Can I use arithmetic on it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's just normal SOM, it's nothing really to do with this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gathered it's language semantics, was just curious.

@ltratt
Copy link
Member Author

ltratt commented Jun 16, 2020

@smarr's idea for a superclass test is a good one. I'll add that.

@vext01
Copy link
Member

vext01 commented Jun 16, 2020

That's not how safety in Rust works. You can package up arbitrary amounts of deadly unsafe code and if you don't mark the function as unsafe, callers will never know.

This doesn't match my understanding. If you include anything Rust considers unsafe in a function, Rust will force you to mark the expression or the containing function unsafe.

So if it compiled without unsafe before, then your annotation can't be a requirement, but rather a hint to the programmer.

You could mark any function unsafe on the virtue that somewhere inside is an unsafe block, but by the same token you'd mark main unsafe.

@ltratt
Copy link
Member Author

ltratt commented Jun 16, 2020

If you include anything Rust considers unsafe in a function, Rust will force you to mark the expression or the containing function unsafe.

If this was true, everything in Rust would need to be unsafe. unsafe blocks are the way you avoid this.

@vext01
Copy link
Member

vext01 commented Jun 16, 2020

I think we are agreeing in that aspect, but that's not what I'm questioning.

Rust will force you to mark the expression [with an unsafe block].

As I understand it, you've added a redundant unsafe keyword as a programmer hint. Am I right?

I'm not saying that's wrong to do so, I've just never seen it done and I'm considering if there's utility in such an idiom.

@ltratt
Copy link
Member Author

ltratt commented Jun 16, 2020

Test added in 89bc1de.

@vext01
Copy link
Member

vext01 commented Jun 16, 2020

The unsafe argument was resolved in a (friendly) external discussion.

I think I buy the reasoning :P

@vext01
Copy link
Member

vext01 commented Jun 16, 2020

Unless @smarr has any further comments, I think this is OK to go in.

@ltratt
Copy link
Member Author

ltratt commented Jun 16, 2020

Reflecting SOM-st/SOM#44, I've expanded the test in cf10c7a (it works the same on Java SOM and yksom).

@vext01
Copy link
Member

vext01 commented Jun 16, 2020

Please squash.

As it so happens, yksom already respects this new facet of the SOM spec
(SOM-st/SOM#44), so the test alone is sufficient.
@ltratt
Copy link
Member Author

ltratt commented Jun 16, 2020

Squashed.

@vext01
Copy link
Member

vext01 commented Jun 16, 2020

bors r+

@bors
Copy link
Contributor

bors bot commented Jun 16, 2020

Build succeeded:

@bors bors bot merged commit d25f555 into softdevteam:master Jun 16, 2020
@ltratt ltratt deleted the inst_var_at branch June 16, 2020 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants