## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Activity
• 20:43
wontruefree synchronize #11682
• 20:42
wontruefree synchronize #11697
• 20:04
beta-ziliani milestoned #11719
• 19:57
beta-ziliani milestoned #11697
• 19:53
beta-ziliani milestoned #11690
• 19:51
beta-ziliani milestoned #11689
• 19:49
beta-ziliani milestoned #11684
• 19:43
beta-ziliani milestoned #11676
• 19:40
beta-ziliani review_requested #11591
• 19:39
beta-ziliani milestoned #11570
• 19:35
beta-ziliani review_requested #11207
• 18:42
beta-ziliani milestoned #11544
• 18:34
beta-ziliani milestoned #11533
• 18:31
beta-ziliani milestoned #11524
• 18:27
beta-ziliani milestoned #11476
• 18:13
beta-ziliani milestoned #11450
• 18:12
beta-ziliani labeled #11393
• 18:10
beta-ziliani synchronize #11207
• 18:05
beta-ziliani milestoned #11201
• 17:57
beta-ziliani reopened #11190
Ary Borenszweig
@asterite
That is, you can't have constants have different values depending on T
Plus you can't do Indigent(Int32)::MAX (I think that doesn't even parse)
Andrius Chamentauskas
@andriusch

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)?

mfiano
@mjfiano:matrix.org
[m]
straight-shoota: Why does Shardbox have roughly 1/6 of the shards as some of the other databases? Yours is my favorite still though
George Dietrich
@Blacksmoke16
@mjfiano:matrix.org shardbox is manually curated, the others scrape github/gitlab and stuff
mfiano
@mjfiano:matrix.org
[m]
Ah I see.
George Dietrich
@Blacksmoke16
@andriusch because how would that work if the hash also contained a cat?
and because is typed as Animal that would be possible
@RespiteSage
@andriusch It has to do with how generic types are implemented in Crystal. Other people are more able to explain why, but the basic idea is that even if T "inherits" from U, A(T) does not inherit from A(U).
In this particular case, you could use dogs = animals[Dog].transform_values &.as(Dog) on your last line.
George Dietrich
@Blacksmoke16
whats the end goal of this? what are you wanting to do?
Andrius Chamentauskas
@andriusch
ah got it, makes sense that Hash(Int32, Dog) < Hash(Int32, Cat) is not true
I want to store objects based on their type & id in a hash
all objects would inherit from some class, so it made sense to me to use Hash(Animal.class, Hash(Int32, Animal)) as type definition
From IRC (bridge bot)
@FromIRC
<riza> if you're using ints as keys, why not an array?
<riza> Hash(Animal.class, Array(Animal))
George Dietrich
@Blacksmoke16
and do what with this hash? like whats the end goal?
Andrius Chamentauskas
@andriusch
I don't want to use array since ids are not sequential, I don't think array would solve my problem though
George Dietrich
@Blacksmoke16
are the ids not related to the instance?
like cat.id
Andrius Chamentauskas
@andriusch
not exactly, more like each id can have multiple types of objects associated to it
e.g. an object with id = 1 can have a cat & a dog associated to it
object with id = 2 only has a cat
while object with id = 3 has an opposum
George Dietrich
@Blacksmoke16
would it be easier to use a struct instead of a hash, then you could give a name to these IDs, with getters to get the related animals?
Andrius Chamentauskas
@andriusch
once the objects are stored I want to be able to retrieve:
an object based on type & id
all objects based on type
there could potentially be >100 type of objects, I don't really want to define 100 properties on struct
and same for ids
George Dietrich
@Blacksmoke16
hmm fair enough
Ary Borenszweig
@asterite
You can just make it so that a Dog and a Cat are not equal by ==, and that they also have different hash values. Then you can simply have Set(Animal)
Oh, but you need to be able to retrieve all dogs and cats, I see...
Andrius Chamentauskas
@andriusch
thanks for your help, I guess I'll go with animals[Dog].transform_values &.as(Dog) since it doesn't seem there are better alternatives
George Dietrich
@Blacksmoke16
yea prob best you're going to get. Hashes can be kinda annoying to work with but i cant really think of another way
mfiano
@mjfiano:matrix.org
[m]
So it's a long shot but figure I'll ask anyway: My biggest problem seems to be trying to come up with an ordering that compiles with regard to requireing 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 requires 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?
mfiano
@mjfiano:matrix.org
[m]
Also sometimes it blows up when I use include by itself, and I have to wrap it in macro included fairly often.
It's quite maddening, because I don't see anything wrong with a fairly simple project.
George Dietrich
@Blacksmoke16
i found what works for me is to just require everything manually within your main entrypoint file
mfiano
@mjfiano:matrix.org
[m]
That's what I first tried, and infact I'm reading that file right now lol
George Dietrich
@Blacksmoke16
with external libs at the top, then one off files, then globs
that covers most things, are still a few cases where i may need to require something in a specific file. For example the exceptions dir, most of them require 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
so i guess tl;dr is like, global things in main file, then sometimes specific requires within directories as needed

Also sometimes it blows up when I use include by itself, and I have to wrap it in macro included fairly often.

got an example of this?

mfiano
@mjfiano:matrix.org
[m]
So would you say that you recommend explicitly listing every file and very rarely use the glob except for collections of order independent submodules?
George Dietrich
@Blacksmoke16
pretty much yea
mfiano
@mjfiano:matrix.org
[m]
I have always preferred to do a serial load order myself in Lisp, manually toposorting it as I develop so that sounds like what I prefer
George Dietrich
@Blacksmoke16
however you do it, just need to ensure things are required before they're used, that's the key point
having every file require everything it needs is a bit overkill
mfiano
@mjfiano:matrix.org
[m]
Maybe doing it that way will solve a lot of my problems. The setup that has been giving me grief is a main file doing require "./origin/*, and then trying to get the requires right in that directory. That sounds like a recipe for failure now that I actually typed it out ðŸ˜†
George Dietrich
@Blacksmoke16
totally could still use the glob, but require the common things before