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

What should happen when a class's fields are mutated? #40

Open
ltratt opened this issue Jun 5, 2020 · 2 comments
Open

What should happen when a class's fields are mutated? #40

ltratt opened this issue Jun 5, 2020 · 2 comments

Comments

@ltratt
Copy link

ltratt commented Jun 5, 2020

What should the output of the following be?

instance_fields = (
    | x y |
    run = (
        | fds |
        fds := instance_fields fields.
        x := 1.
        y := 2.
        fds at: 1 put: #z.
        z println.
    )
)

Java SOM prints:

ERROR: Unable to resolve z

yksom would probably print the same if/when I implement the fields primitive. In a sense, it seems like reasonable behaviour to me, but I'm not sure if anyone has thought of this before and/or has opinions on what should happen.

@smarr
Copy link
Member

smarr commented Jun 5, 2020

In Squeak, you'll have to trigger the compiler manually to regenerate methods (tested on Squeak 5).
There's no "dynamic" binding/updating mechanism in place.

Pharo uses a more elaborate system, but I think the key there is that the return array is simply a snapshot of the class at that point.
So, any mutation to the array is without any effect.

I don't know what the right thing for SOM is.

Considering also #35, these things should ideally be following a consistent approach.

One option would be to say, yes, these methods always return snapshots, and any mutation is without effect on the actual system.

Another option would be to have methods bind dynamically to fields. Though, this would mean unknown fields raise #unknownGlobal:, which isn't ideal.
And your example wouldn't even allow for an approach that relies on recompilation of a method, but indeed needs a field lookup + invalidation at run time.

@ltratt
Copy link
Author

ltratt commented Jun 6, 2020

Having thought about this a bit, I think a plausible course of action is to diverge from #35. The difference between fields and methods is that the former returns an array of names and the latter returns an array of methods. [IMHO, it would probably have made more sense for both to return an array of names because, but that might be a bigger change than you're willing to make (and perhaps not consistent with Smalltalk?).] I think mutations to the array of field names should be ignored because it would then turn SOM from a statically scoped language to a dynamically scoped language and I have learnt to be very wary of dynamic scoping over the years! Consider, for example, if I changed a field name from x to system: with dynamic scoping the program's bindings would change in a way that I don't think anyone could find helpful.

ltratt added a commit to ltratt/yksom that referenced this issue Jun 8, 2020
This returns a class's fields. However, SOM's semantics on this are currently
unclear (see SOM-st/SOM#40). Although we allow users
to mutate the 'methods' array, this is less sensible with fields in my opinion,
since it would turn SOM from a statically into a dynamically scoped language.
Thus, each time you call 'fields' you get a fresh array back such that any
mutations made are ignored (see the `mutate_fields` test).
bors bot added a commit to softdevteam/yksom that referenced this issue Jun 8, 2020
142: Add the 'fields' primitive. r=ptersilie a=ltratt

This returns a class's fields. However, SOM's semantics on this are currently unclear (see SOM-st/SOM#40). Although we allow users to mutate the 'methods' array, this is less sensible with fields in my opinion, since it would turn SOM from a statically into a dynamically scoped language. Thus, each time you call 'fields' you get a fresh array back such that any mutations made are ignored (see the `mutate_fields` test).

Co-authored-by: Laurence Tratt <[email protected]>
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

No branches or pull requests

2 participants