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/
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
require
ing the proper files where they are needed. It seems to get really confused sometimes when I include
or extend
a module. I sort of wish I could just require "./project/*
in a project.cr file in src/, but a real solution seems to be repeating the same require
s in different files. I have no idea what I'm doing wrong, if anything, because I'm really not used to programming in such an OOP way, and mixins even less so. Has anyone ran into these sorts of problems before, and how did you get used to it/find something that works? Can anyone recommend some tutorials for Crystal or Ruby or similar for structuring a project in a nice way?
include
by itself, and I have to wrap it in macro included
fairly often.
http_exception
i could have prob added that as a one off require before the directory but :shrug: didn't think it was worth it
Also sometimes it blows up when I use
include
by itself, and I have to wrap it inmacro included
fairly often.
got an example of this?