-
Notifications
You must be signed in to change notification settings - Fork 38
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
Support for single instance types #50
Comments
It looks like this is kinda supported via singleton types, for example: require "rdl"
class A
extend RDL::Annotate
MAGIC_VALUE = 'c'
type("(${A::MAGIC_VALUE}) -> %any")
def b(arg)
arg.upcase
end
end
puts A.new.b('c') # passes
puts A.new.b('d') # 💥 fails But, it requires:
Just thought I'd share in case that arrangement works for anyone else. |
@rmosolgo how do you do the "OR" part? RDL does support literals which I think you're showing off. |
Ohhh right, you don't want just one value, you want "one of a set of values". It's still not quite there but here's what I found: require "rdl"
class A
MAGIC_VALUE_1 = 'c'
MAGIC_VALUE_2 = 'd'
extend RDL::Annotate
RDL.type_alias("%some_enum", "${A::MAGIC_VALUE_1} or ${A::MAGIC_VALUE_2}")
type("(%some_enum) -> %any")
def b(arg)
arg.upcase
end
end
puts A.new.b(A::MAGIC_VALUE_1) # pass
puts A.new.b(A::MAGIC_VALUE_2) # pass
puts A.new.b('z') # 💥 not ideal, and still some caveats:
|
Singleton types work for symbols, nil, and numeric types. But I didn't add direct support for strings because of mutability. For example, suppose we had string singleton types, so we could say @ptarjan, this seems like another case that should be allowed (controlled by some configuration option), since presumably you can't and don't want to change your codebase to use symbols instead of strings. |
Do you plan on having any notion of "this parameter must
==
to either 'foo' or 'bar'"?We use this pretty extensively in our runtime type system and call it
Enum
but it can be phrased as single instance class and theEnum
is just a union over those.The text was updated successfully, but these errors were encountered: