Uninitialized collection property returns nil #113

Closed
greyblake opened this Issue Sep 9, 2012 · 8 comments

Comments

Projects
None yet
3 participants
Contributor

greyblake commented Sep 9, 2012

class Poem
  include Virtus
  attribute :lines, Array[PoemLine]
end

poem = Poem.new
poem.lines  # => nil

I expect that uninitialzied property which is meant to be a collection should return an empty array. Is it a bug or part of the design?

Collaborator

dkubb commented Sep 9, 2012

I would assume that's a bug.

Owner

solnic commented Sep 9, 2012

Yup it's definitely a bug!

Owner

solnic commented Sep 9, 2012

Looks like DefaultValue is guilty here, by default it returns nil and in case of collections we need an instance of an array or a set. I'll fix that next week and push a bug-fix release.

Collaborator

dkubb commented Sep 10, 2012

@solnic wdyt about making it so the default value for the Array type is an empty Array if not already set? We should be able to do that with default proc { [] } for the Array type. People will still be able to override it if they need to.

We may want to consider something similar for the Set and Hash types too.

Collaborator

dkubb commented Sep 10, 2012

Actually I think default [] should work just fine too. Virtus will clone the empty Array by default before assigning it to an object.

Owner

solnic commented Sep 10, 2012

Yup that will be the fix. Should be trivial.

On Monday, September 10, 2012 at 2:50 AM, Dan Kubb wrote:

Actually I think default [] should work just fine too. Virtus will clone the empty Array by default before assigning it to an object.


Reply to this email directly or view it on GitHub (#113 (comment)).

dkubb closed this in 918e9d7 Sep 10, 2012

Collaborator

dkubb commented Sep 10, 2012

@greyblake ok, I've committed a fix for this. Please try it out and let me know if it solves your issue.

Contributor

greyblake commented Sep 10, 2012

@dkubb It works. Thanks guys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment