Problem of duplication with my function

class ISM::AvailableSoftware
  property name : String
  property versions : Array(ISM::SoftwareInformation)

  def initialize(@name, @versions); end
end

Would work yes, but they no longer have default values. However at this point I’m not sure what problem you’re trying to solve.

Is it possible to infer manually, and if nothing is putted, affect a value of constant ?

I mean something like that:

def initialize( @name : String, @versions : Array(ISM::SoftwareInformation))

But a way to affect a value from my constant if nothing is specified when this class is initialized ?

(Because I think it’s better to initialize all variables)

def initialize(@name, @versions); end works because the types of the ivars are inferred via property macro, which is essentially creating the ivars as like @name : String and @versions : Array(ISM::SoftwareInformation).

Again to be clear the type of each instance variable is not directly related to its default value, at least in regards to whether you have type restrictions on them. The problem you ran into is that the default value of each instance references the same array because it came from a constant. One way to fix that is to just do like: @versions = Array(ISM::SoftwareInformation).new.

It also might be worth considering if these ivars should have a default value, as having a default just to avoid nilable properties isn’t really great either versus just creating the instance when you have all the required info ready.

EDIT: Community - The Crystal Programming Language might be easier than the forums.

1 Like

I done this test, it’s possible to do that:

Constante = "arr"

class Testy
    
    property name : String

    def initialize(@name : String = Constante)

    end

end

cls1 = Testy.new
puts cls1.name

cls2 = Testy.new("babouin")
puts cls2.name

But finally, because I initialize everything like this, I can slow my program ?

Probably not in a measurable amount.

Okay.

And what do you think about that ?

def initialize(@name : String = Constante)

Depends on the real use case IMO. Having a const to reduce duplication is a good idea, but if this is the only place its used, it doesn’t really save you anything. Also if the default value is just an empty string or something, need to ask yourself should the name argument be optional at all in the first place. Or if it should be optional, would it just be better to make it nil.

I see. The reason I do that it’s just to centralize all value in a same module, I found it’s easier to retrieve and modify , than retrieve each value inside the code.

If you’re modifying them often, maybe they’re not really constants :man_shrugging:. Hard to say without diving into your use case, so just do whatever makes sense I guess.

Sorry I had a lot of questions :grimacing:

I think if i use this syntax, I will don’t have a duplication anymore ?

def initialize(@name : String = Constante)

If Constante's value is an Array or other Reference type you still would. I’d go read thru the comments again as this has already been answered.

Okay.Definitely, I need to avoid that. Thanks you very much

To be honest, I fouund this behavior very strange, because I have a lot of classes initialized with constances, and I didn’t have this problem before.

What are the value’s of your other constants? Because again remember this only happens with Reference types, such as Array.

It’s my full project :

If you see inside, for example in the directory ISM/Option, you have a lot of classes initialized like that, and I don’t have any bugs

I’m actually correcting a copy of my project and I’m removing every constants to see if I can see any differences

I think this is different than Ruby, because I was programming like that in Ruby

I think I will keep module just for default texts

From briefly looking through your code, it seems like almost all of your default values are value types. You do have one default array, but I don’t think you’d run into the same issue because it’s for command line options, which I assume you’ll only have one of.

Fixing your problem depends on the differences between reference and value types. Do you understand that distinction? If not, we can focus on helping you understand that, but we’ve been assuming that level of knowledge.

No worries, I understood the problem now, I read too fast the explanations.

It’s the problem when you use a high level langage with memory manager, you can forgot the reality :grimacing:

No worries I understand that very well, I was programming in assembly x86 (FASM) and C++ in past

I corrected yet my project, thanks you so much to help me to found the problem

Special thanks to Blacksmoke16

Thanks you as well RespiteSage

1 Like