The Crystal programming language | http://crystal-lang.org | Fund Crystal's development: http://is.gd/X7PRtI | Docs: http://crystal-lang.org/docs/ | API: http://crystal-lang.org/api/
class Indigent(T)
{% puts T %}
end
a = Indigent(Int32).new
Indigent(Int32)::MAX
(I think that doesn't even parse)
Hello, I have this code:
class Animal
end
class Cat < Animal
def meow
puts "meow!"
end
end
class Dog < Animal
def woof
puts "woof!"
end
end
animals = Hash(Animal.class, Hash(Int32, Animal)).new { |h, k| h[k] = Hash(Int32, Animal).new }
animals[Cat][1] = Cat.new
animals[Dog][1] = Dog.new
animals[Dog][1].as(Dog).woof
animals[Cat][1].as(Cat).meow
dogs = animals[Dog].as(Hash(Int32, Dog))
and I'm getting an error on the last line:
23 | dogs = animals[Dog].as(Hash(Int32, Dog))
^
Error: can't cast Hash(Int32, Animal) to Hash(Int32, Dog)
I don't understand why casting an element in the hash works, but casting the whole hash doesn't? Are there alternatives to what I'm trying to achieve (essentially index objects by type & by id)?
Animal
that would be possible
dogs = animals[Dog].transform_values &.as(Dog)
on your last line.
Hash(Animal.class, Hash(Int32, Animal))
as type definition
cat.id