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

RFC: Syntax for read- and write- access to properties #284

Conversation

asajeffrey
Copy link
Collaborator

Still a draft as we are exploring the design space.

@asajeffrey asajeffrey added the rfc Language change proposal label Dec 8, 2021
Comment on lines +87 to +88
One corner case is that type inference may deduce different read- and
write-types, which need to be presented to the user. For example the
Copy link
Collaborator

Choose a reason for hiding this comment

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

Doesn't seem possible with current inference rules for read-only and write-only properties:
https://github.com/Roblox/luau/blob/master/rfcs/property-writeonly.md#adding-read-only-and-write-only-properties

In the example below, assignment of Dog.new() to x.pet will make pet property read-write.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Once we've got local type inference, we'll get different read and write types. In this case we'll get a free type X with constraints X <: { get pet : Animal } and X <: { set pet : Dog }, so solving those constraints gives different read and write types.

* ... as per "names for modifiers", only prefixed by `@` ...

These both have the advantage of being unambiguous and easier to
parse. Symbols are terser, whch is both good and bad.
Copy link
Contributor

Choose a reason for hiding this comment

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

Adding a new sigil to Luau is probably not the best course of action here. I vote against @-attributes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We may end up with attribute for things like API documentation, deprecation, etc. The syntax which most languages seem to have adopted is @attribute.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

(Sorry for the delay, it's been the winter break!)

@alexmccord
Copy link
Member

I think this RFC as proposed leaks too much implementation detail and increases the complexity of type annotation grammar in general. I was hoping that we would arrive at something like:

{ animal: Animal } means animal is read-write.
{ readonly animal: Animal } means animal is read-only.
{ writeonly animal: Animal } means animal is write-only.

This also means it's not too confusing because you don't have two fields of the same name with different types with a read/write dichotomy.

Even better yet, we don't need to expose the writeonly syntax whatsoever because there's no need for it in Luau.

@asajeffrey
Copy link
Collaborator Author

@alexmccord We could just provide new syntax for read-only and write-only properties, and then use intersections to construct types with different read and write types.

@zeux
Copy link
Collaborator

zeux commented Oct 30, 2023

This PR is closed as part of RFC migration; please see #1074 (comment) for context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rfc Language change proposal
Development

Successfully merging this pull request may close these issues.

None yet

5 participants