Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 01:18
    HertzDevil review_requested #11504
  • Nov 28 23:58
    HertzDevil synchronize #11504
  • Nov 28 16:33
    HertzDevil labeled #11504
  • Nov 28 16:33
    HertzDevil labeled #11504
  • Nov 28 16:33
    HertzDevil labeled #11504
  • Nov 28 16:32
    HertzDevil opened #11504
  • Nov 28 14:42
    oprypin review_requested #11496
  • Nov 28 14:41
    oprypin review_requested #11495
  • Nov 28 14:41
    oprypin synchronize #11395
  • Nov 28 14:28
    HertzDevil synchronize #11503
  • Nov 27 19:45
    mohd-akram synchronize #11439
  • Nov 27 19:14
    straight-shoota synchronize #11499
  • Nov 27 18:48
    mgomes ready_for_review #11482
  • Nov 27 18:20
    HertzDevil labeled #11482
  • Nov 27 18:20
    HertzDevil labeled #11482
  • Nov 27 18:17
    HertzDevil synchronize #11495
  • Nov 27 18:02
    HertzDevil labeled #11503
  • Nov 27 18:02
    HertzDevil labeled #11503
  • Nov 27 18:02
    HertzDevil labeled #11503
  • Nov 27 18:02
    HertzDevil opened #11503
Benjamin Wade
@RespiteSage
I feel like I constantly have this question, but is there a way to access the actual, compile-time type of a generic type within a class instantiation but not in a method?
Like
class Indigent(T)
  {% puts T %}
end

a = Indigent(Int32).new
Except it actually works.
George Dietrich
@Blacksmoke16
idt because T is only specific to an instance
Benjamin Wade
@RespiteSage
Hm... Okay.
I was wanting to set constants based on what type it is (specifically, MIN/MAX).
I bet I could do those as getters and set them in the initializer, though.
Ary Borenszweig
@asterite
Constants will be inside Indigent, not Indigent(Int32)
That is, you can't have constants have different values depending on T
1 reply
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
Benjamin Wade
@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