-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 restriction a : ModFoo & ModBar
#7534
Comments
Madskillz, but if you really need to restrict with both you can do something like this: private def need_foo_and_bar_impl(param : ModBar)
puts param.foo
puts param.bar
end
# The argument should have ModFoo AND ModBar
def need_foo_and_bar(param : ModFoo)
need_foo_and_bar_impl(param)
end |
@konovod THAT is a fun workaround 😄 Actually I've found another one: https://carc.in/#/r/6gs1 # The argument should have ModFoo AND ModBar
def need_foo_and_bar(param : T) forall T
{% unless T < ModFoo && T < ModBar %}
{% raise "nop!" %}
{% end %}
puts param.foo
puts param.bar
end But I don't have any trace now.. |
Also an option: |
What do you mean? An option for a workaround? or for a way to restrict the param? |
As a workaround. # The argument should have ModFoo AND ModBar
def need_foo_and_bar(param : ModFoo)
typeof(param_bar : ModBar = param)
param.foo
param.bar
end |
Related: #2404 |
Didn't find another issue about that except #1839 (comment)
I think there's no easy way to do that atm, I'd like to restrict my argument to have
ModFoo
ANDModBar
as in:I know it's not currently possible as is, but has it been considered at some point?
How can I restrict the
param
without 'workaround' like creating a module that include both, and include that module in all classes that include both (e.g: found using some macro-fu). Or using forall & macro checks at beginning of the method (see #7534 (comment))I know I could simply not restrict the param and rely on the compiler telling the user that the given arg doesn't have method
foo
orbar
. But what if those methods are generated, the user doesn't necessarily know where they come from and what to do (include some module) to be able to pass their object.The text was updated successfully, but these errors were encountered: