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

Create static wrappers for builtin types #4077

Merged
merged 6 commits into from
Jan 30, 2023

Conversation

hubertp
Copy link
Contributor

@hubertp hubertp commented Jan 24, 2023

Pull Request Description

#3764 introduced static wrappers for instance methods. Except it had a limitation to only be allowed for types with at least a single constructor.
That excluded builtin types as well which, by default, don't have them. This limitation is problematic for Array/Vector consolidation and makes builtin types somehow second-citizens.

This change lifts the limitation for builtin types only. Note that we do want to share the implementation of the generated builtin methods. At the same time due to the additional argument we have to adjust the starting index of the arguments.
This change avoids messing with the existing dispatch logic, to avoid unnecessary complexity.

As a result it is now possible to call builtin types' instance methods, statically:

  arr = Array.new_1 42
  Array.length arr

That would previously lead to missing method exception in runtime.

Important Notes

The only exception is Nothing. Primarily because it requires Nothing to have a proper eigentype (Nothing.type) which would messed up a lot of existing logic for no obvious benefit (no more calling of foo=Nothing in parameters being one example).

Checklist

Please include the following checklist in your PR:

  • The documentation has been updated if necessary.
  • All code conforms to the
    Scala,
    Java,
    and
    Rust
    style guides.
  • All code has been tested:
    • Unit tests have been written where possible.
    • If GUI codebase was changed: Enso GUI was tested when built using BOTH
      ./run ide build and ./run ide watch.

@radeusgd
Copy link
Member

The only exception is Nothing. Primarily because it requires Nothing to have a proper eigentype (Nothing.type) which would messed up a lot of existing logic for no obvious benefit (no more calling of foo=Nothing in parameters being one example).

I think we will have to face something like that at some point anyway, am I right @JaroslavTulach ? I think this is related to this Discord discussion where I notice that Truffle assertions do not allow Nothing to be a type (meta object) and a null value at the same time.

@JaroslavTulach
Copy link
Member

The only exception is Nothing.
I think we will have to face something like that at some point anyway, am I right @JaroslavTulach ? I think this is related to this Discord discussion where I notice that Truffle assertions do not allow Nothing to be a type (meta object) and a null value at the same time.

Nothing is exceptional! #4074 provides special exceptions for Nothing to not have a meta-object.

Copy link
Member

@JaroslavTulach JaroslavTulach left a comment

Choose a reason for hiding this comment

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

Except the doubts with "_static" suffix, I don't see any problem. Nice.

@hubertp hubertp force-pushed the wip/hubert/instance-methods-as-statics-184278959 branch from f4fc619 to 15f23d3 Compare January 26, 2023 13:58
@hubertp hubertp mentioned this pull request Jan 26, 2023
4 tasks
@hubertp
Copy link
Contributor Author

hubertp commented Jan 26, 2023

I think we will have to face something like that at some point anyway, am I right @JaroslavTulach ? I think this is related to this Discord discussion where I notice that Truffle assertions do not allow Nothing to be a type (meta object) and a null value at the same time.

@radeusgd so you are OK with the PR? I'd rather keep the Nothing status as-is as changing it may potentially get out of hand. And this PR is blocking me on Array/Vector consolidation.

Copy link
Member

@radeusgd radeusgd left a comment

Choose a reason for hiding this comment

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

Looks good, I'd appreciate adding one more test to clarify how the statics work in an edge case which is unclear to me currently.

Error.to_text . should_equal "Error"
Error.to_text Error . should_equal "Error"
Copy link
Member

Choose a reason for hiding this comment

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

I get where this is coming from, but it is quite surprising. Should the Error argument be supported anymore?

Other than that - I'd like to see a test like

x = Error
x.to_text . should_equal "???"

What will this print? Will it be Error or a function now? Would be great to have a test documenting this behaviour, as it's non obvious.

Copy link
Contributor Author

@hubertp hubertp Jan 30, 2023

Choose a reason for hiding this comment

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

What will this print? Will it be Error or a function now?

The latter. OK, I will add case.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, is this definitely the way it is supposed to work? Shouldn't the static approach only work for static references to the types?

Anyway, even if not, I'm not suggesting to do anything about it in the current PR.
Thanks for adding the test, documenting the currently expected behaviour.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would even argue this now works as it should have worked to be begin with. Let's take further discussion offline.

#3764 introduced static wrappers
for instance methods. Except it had a limitation to only be allowed for
types with at least a single constructor.
That excluded builtin types as well which, by default, don't have them.
This limitation is problematic for Array/Vector consolidation and makes
builtin types somehow second-citizens.

This change lifts the limitation for builtin types only. Note that we do
not want to share the implementatin of the generated builtin methods.
At the same time due to the additional argument we have to adjust the
starting index of the arguments.
This change avoids messing with the existing dispatch logic, to avoid
unnecessary complexity.

As a result it is now possible to call builtin types' instance methods,
statically:
```
  arr = Array.new_1 42
  Array.length arr
```
That would previously lead to missing method exception in runtime.
The only exception is `Nothing`. Primarily because it requires `Nothing`
to have a proper eigentype (`Nothing.type`) which would messed up a lot
of existing logic.
@hubertp hubertp force-pushed the wip/hubert/instance-methods-as-statics-184278959 branch from 15f23d3 to 63bb9b4 Compare January 30, 2023 14:53
@hubertp hubertp added the CI: Ready to merge This PR is eligible for automatic merge label Jan 30, 2023
@mergify mergify bot merged commit be91b1e into develop Jan 30, 2023
@mergify mergify bot deleted the wip/hubert/instance-methods-as-statics-184278959 branch January 30, 2023 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI: Ready to merge This PR is eligible for automatic merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants