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

type:hassubtypeof #101

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

gaymeowing
Copy link
Contributor

@gaymeowing gaymeowing commented Feb 21, 2025

Proposes adding a new method to the type userdata for subtype checking.

Rendered

@gaymeowing gaymeowing marked this pull request as draft February 21, 2025 14:15
@gaymeowing gaymeowing marked this pull request as ready for review February 21, 2025 14:15
@gaymeowing gaymeowing marked this pull request as draft February 21, 2025 14:15
@aatxe
Copy link
Contributor

aatxe commented Feb 24, 2025

Haven't read this yet, but just wanted to say that this is, in general, a reasonable idea and something we'd like to consider, for sure. The devil is in the details, but this was originally part of the plan and got cut to scope down the MVP for user-defined type functions.

gaymeowing

This comment was marked as resolved.

| ------------- | ------------- | ------------- |
| `hassubtype(arg: string)` | `boolean` | returns true if self has a subtype of the same tag as the argument. List of available tags: "nil", "unknown", "never", "any", "boolean", "number", "string", "singleton", "negation", "union", "intersection", "table", "function", "class" |
| `hassubtypeof(arg: type)` | `boolean` | returns true if self has a subtype of the argument. |
| `musthavesubtype(arg: string)` | `boolean` | returns true if self has only a subtype of the same tag as the argument. List of available tags: "nil", "unknown", "never", "any", "boolean", "number", "string", "singleton", "negation", "union", "intersection", "table", "function", "class" |
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't have an idea on what the description for the musthave methods should be, so its just the same as the has methods.

@gaymeowing gaymeowing marked this pull request as ready for review March 4, 2025 16:21
@gaymeowing
Copy link
Contributor Author

Making this not a draft as its much more well described now, plus to hopefully have someone contribute a description for the musthave methods.

@gaymeowing
Copy link
Contributor Author

Would an exact method be a good idea to include aswell? Although It'd be basically rendered useless by generic constraints. But It'd basically not allow unions when the provided subtype isnt a union.
ie

type meow = {
     hi: string
}

type mrrp = {
     hi: "hello"
}

type meowers = meow | mrrp

print(meowers:exacthassubtypeof(meow)) -- false
print(mrrp:exacthassubtypeof(meow)) -- true

@vegorov-rbx
Copy link
Collaborator

You seem to be too focused on hard-coding some behaviors exactly targeting unions, which leads to the problems of structural subtyping.
Luau actually has moved to use semantic subtyping (more at: https://corp.roblox.com/newsroom/2022/11/semantic-subtyping-luau)

So to me, having anything aside from hassubtypeof doesn't make sense (even hassubtype - there is no notion of a 'union' or 'intersection' as a standalone type).
I was waiting for the other team members to notice this and didn't comment initially. Would be surprised if they disagree. (they are probably just busy)

@gaymeowing
Copy link
Contributor Author

You seem to be too focused on hard-coding some behaviors exactly targeting unions, which leads to the problems of structural subtyping. Luau actually has moved to use semantic subtyping (more at: https://corp.roblox.com/newsroom/2022/11/semantic-subtyping-luau)

So to me, having anything aside from hassubtypeof doesn't make sense (even hassubtype - there is no notion of a 'union' or 'intersection' as a standalone type). I was waiting for the other team members to notice this and didn't comment initially. Would be surprised if they disagree. (they are probably just busy)

Yeah, the rest would be better solved with generic constraints admittedly.

@gaymeowing gaymeowing changed the title Subtype Checking methods for User-Defined Type Functions type:hassubtypeof Mar 6, 2025
@gaymeowing
Copy link
Contributor Author

Removed everything else and just left hassubtypeof

@vegorov-rbx vegorov-rbx requested review from vegorov-rbx and aatxe and removed request for vegorov-rbx March 26, 2025 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants