-
-
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
Recursive alias, cast #4885
Comments
|
Actually you should do |
Thank you, I see what you mean, but if the problem was to cast a simple Array, I would not have open an issue. The alias Type = Int32 | Array(Type)
def foo(a : Type) : Type
a.as(Type)
end
foo 1
foo [1]
foo [1, [2]]
Of course, you can remove the cast, but the problem becomes The context is the following: "I want to choose some functions to call at runtime. These functions can return several types of objects (its very generic)." def foo
"foo"
end
def bar
0xba5
end
runtime = Hash(String, Proc(String | Int32)).new
runtime["foo"] = -> foo
runtime["bar"] = -> bar But foo returns a String and bar a Int32, and they don't match (String | Int32). So I could just cast the result of my functions. alias Type = Int32 | String | Array(Int32)
def foo
"foo".as(Type)
end
def bar
0xba5.as(Type)
end
runtime = Hash(String, Proc(Type)).new
runtime["foo"] = -> foo
runtime["bar"] = -> bar It also works with Generics but if there are recursive generics... alias Type = Int32 | Array(Type)
def foo
[0xf00].as(Type)
end
def bar
0xba5.as(Type)
end
runtime = Hash(String, Proc(Type)).new
runtime["foo"] = -> foo
runtime["bar"] = -> bar
Maybe I could workarround and declare macro runtime_init()
RUNTIME = [] of String
end
macro runtime_def(name, &block)
def _runtime_{{name.id}}
{{yield}}
end
{{RUNTIME << name}}
end
macro runtime_end()
CALLS = {
{% for name in RUNTIME %}
{{name}} => -> _runtime_{{name.id}},
{% end %}
}
end
macro call(name)
CALLS[{{name}}].call()
end
runtime_init
runtime_def "foo" do
[0xf00]
end
runtime_def "bar" do
0xba5
end
runtime_end
pp call STDIN.gets.to_s.chomp tell me there is another way |
[1] of Type |
That's nice! def foo(param1, param2) : Type
param1.operate(param2) # well... can't define the type of "operate".
end |
I usually do this with a helper function: alias Type = String | Int32 | Array(Type) | Hash(String, Type)
def cast_to_type(x : Array)
return x.map { |e| cast_to_type(e).as(Type) }.as(Type)
end
def cast_to_type(x : Hash)
h = Hash(String, Type).new
x.each do |(k, v)|
h[k] = cast_to_type(v).as(Type)
end
h.as(Type)
end
def cast_to_type(x)
x.as(Type)
end
p cast_to_type(10)
p cast_to_type([10])
p cast_to_type({"hm" => [10, ["", 2, {"a" => "b"}]]}) |
That would work yes! But isn't it expensive to reallocate every Array/Hash just to do a cast ? |
You have to. Because the memory layout of a |
Yes, I should have learned my lessons :) Sometimes I forget that we care about memory in this langage. |
Hello,
cf: #3803 , #2665
Is there a way to cast something using a recursive type ?
Error:
Note: my aim is no to be able to cast it, it is to have a defined return type (
def XXX : Type
)The text was updated successfully, but these errors were encountered: