Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 10:19
    straight-shoota closed #11504
  • 10:17
    BlobCodes synchronize #11211
  • Nov 29 23:41
    straight-shoota closed #11447
  • Nov 29 23:41
    straight-shoota synchronize #11469
  • Nov 29 23:35
    BlobCodes synchronize #11211
  • Nov 29 22:51
    BlobCodes edited #11211
  • Nov 29 21:19
    beta-ziliani milestoned #11446
  • Nov 29 21:02
    oprypin synchronize #11446
  • Nov 29 19:53
    beta-ziliani milestoned #11395
  • Nov 29 19:52
    beta-ziliani milestoned #11440
  • Nov 29 19:51
    straight-shoota labeled #11508
  • Nov 29 19:51
    straight-shoota labeled #11508
  • Nov 29 19:51
    straight-shoota labeled #11508
  • Nov 29 19:51
    straight-shoota opened #11508
  • Nov 29 18:54
    beta-ziliani milestoned #11367
  • Nov 29 17:44
    beta-ziliani milestoned #11428
  • Nov 29 17:42
    beta-ziliani milestoned #11382
  • Nov 29 17:40
    oprypin synchronize #11446
  • Nov 29 17:34
    beta-ziliani labeled #11507
  • Nov 29 17:34
    beta-ziliani labeled #11507
mfiano
@mjfiano:matrix.org
[m]
yet, anyway
Ary Borenszweig
@asterite
I know oprypin has written some games, also I think in Windows
mfiano
@mjfiano:matrix.org
[m]
Does Crystal have concurrent hash tables?
Benjamin Wade
@RespiteSage
Someone may have implemented them, but I don't know.
mfiano
@mjfiano:matrix.org
[m]
Would like to see a lockless one with CAS or something
That has been a huge complaint with my Lisp work anyway
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.