On Oct 25, 2012, at 13:22 , Igor P. [email protected] wrote:
Yossef M. wrote in post #1081211:
Just because something doesn’t work the way you insist it must doesn’t
mean it’s broken.
exactly.
If you claim that using accessors and ‘@’ variables when reopening
Struct classes with class idiom isn’t broken, that’s fine, but my last
example I gave, clearly indicates differently!
Clearly? No. Your example is wrong, as shown by a simple -w:
9999 % ruby19 -w
S = Struct.new(:num)
class S
attr_accessor :num
def initialize(n); @num=n+5; end
def num=(n); @num = n+5; end
end
-:3: warning: method redefined; discarding old num
-:3: warning: method redefined; discarding old num=
-:5: warning: method redefined; discarding old num=
Reimplementing the accessors breaks the explicit contract of Struct.
There’s no point in doing so at all and your example is simply wrong.
Breaking the contract of initialize is also wrong, you’re not even
calling super.
If you do not need it to
be consistent in those simple circumstances you use Struct idiom, that
Struct is not an idiom. It is a class. Its internals are an
implementation detail. From the very first line of the rdoc on the
class: “A Struct is a convenient way to bundle a number of attributes
together, USING ACCESSOR METHODS” (emphasis mine).
by no means discredits a more elaborate use, which clearly is broken,
redundant and inconsistent, as I have shown in my previous sample code
snippet, where you need to annul the Struct by the {{ attr_accessor :num
}} line when you reopen the ‘S’ class, in order to be able to define
more elaborate custom initialization accessor methods.
“Be able to”? You’re blowing this WAY out of proportion:
% ruby19 -w
S = Struct.new(:num)
class S
def initialize n
super
self.num = n + 5
end
end
p S.new(10).num # => 15
Looks like it is more than able to.
Let’s not waste any more time on this issue. I think we all can tolerate
this apparent glitch. At the same time reiterating that there should be
as few exceptions as possible in the language grammar and its use
certainly is not an out of line proposition.
This has nothing to do with grammar. Again, Struct is a class, not
grammar.