Hi guys, I''m trying to override a sealed method in IR and I succeed. For example: C#: public class Shape { public virtual int GetNumberOfSizes() { return 0; } } public class Square : Shape { public override sealed int GetNumberOfSizes() { return 4; } } IR: class NewSquare < Square def get_number_of_sizes return 99 end end puts NewSquare.new.get_number_of_sizes # => 99 Bug or by design? Thanks, Shay. ---------------------------- Shay Friedman http://www.ironshay.com Follow me: http://twitter.com/ironshay -- Posted via http://www.ruby-forum.com/.
Is there something special that needs to be done when overriding CLR methods (like C#''s override keyword)? -- Posted via http://www.ruby-forum.com/.
And also - with regular methods (not abstract, virtual sealed or whatever), when I inherit them in IR - are the just hidden like the static methods? Thanks! Shay. ---------------------------- Shay Friedman http://www.ironshay.com Follow me: http://twitter.com/ironshay -- Posted via http://www.ruby-forum.com/.
No. We override the method if there is a non-sealed virtual in the base class that has the same or un-mangled name. Tomas -----Original Message----- From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Shay Friedman Sent: Saturday, June 13, 2009 9:33 AM To: ironruby-core at rubyforge.org Subject: Re: [Ironruby-core] Sealed method can be overriden Is there something special that needs to be done when overriding CLR methods (like C#''s override keyword)? -- Posted via http://www.ruby-forum.com/. _______________________________________________ Ironruby-core mailing list Ironruby-core at rubyforge.org http://rubyforge.org/mailman/listinfo/ironruby-core
Yes, sealed virtual and "regular" methods have the same behavior. Tomas -----Original Message----- From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Shay Friedman Sent: Saturday, June 13, 2009 10:10 AM To: ironruby-core at rubyforge.org Subject: Re: [Ironruby-core] Sealed method can be overriden And also - with regular methods (not abstract, virtual sealed or whatever), when I inherit them in IR - are the just hidden like the static methods? Thanks! Shay. ---------------------------- Shay Friedman http://www.ironshay.com Follow me: http://twitter.com/ironshay -- Posted via http://www.ruby-forum.com/. _______________________________________________ Ironruby-core mailing list Ironruby-core at rubyforge.org http://rubyforge.org/mailman/listinfo/ironruby-core
There is a slight difference though. In the example: puts NewSquare.new.get_number_of_sizes # => 99 puts NewSquare.new.GetNumberOfSizes # => 4 If I change the example to a regular method (not sealed), the output is slightly different: puts NewSquare.new.get_number_of_sizes # => 99 puts NewSquare.new.GetNumberOfSizes # => 99 Thanks, Shay. ---------------------------- Shay Friedman http://www.ironshay.com Follow me: http://twitter.com/ironshay -- Posted via http://www.ruby-forum.com/.
That''s expected. If you change "sealed override" to just "override" the method is virtual and Ruby subclasses will override it. In both cases NewSquare.new.GetNumberOfSizes finds CLR method Square::GetNumberOfSizes and invokes it. In the first case, the method is not overridden and therefore the implementation in Square gets executed. Without "sealed" the method is overridden by NewSquare#get_number_of_sizes and therefore the virtual dispatch executes NewSquare''s implementation. In both cases NewSquare.new.get_number_of_sizes finds Ruby method NewSquare#get_number_of_sizes and executes it. Tomas -----Original Message----- From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Shay Friedman Sent: Saturday, June 13, 2009 12:03 PM To: ironruby-core at rubyforge.org Subject: Re: [Ironruby-core] Sealed method can be overriden There is a slight difference though. In the example: puts NewSquare.new.get_number_of_sizes # => 99 puts NewSquare.new.GetNumberOfSizes # => 4 If I change the example to a regular method (not sealed), the output is slightly different: puts NewSquare.new.get_number_of_sizes # => 99 puts NewSquare.new.GetNumberOfSizes # => 99 Thanks, Shay. ---------------------------- Shay Friedman http://www.ironshay.com Follow me: http://twitter.com/ironshay -- Posted via http://www.ruby-forum.com/. _______________________________________________ Ironruby-core mailing list Ironruby-core at rubyforge.org http://rubyforge.org/mailman/listinfo/ironruby-core
The same happens with non-virtual methods as well. By the way, should you be able to call super when you override a virtual method? Thanks, Shay. ---------------------------- Shay Friedman http://www.ironshay.com Follow me: http://twitter.com/ironshay -- Posted via http://www.ruby-forum.com/.
C#: public class C { public int FooBar() { return 123; } } Ruby: require ''x.dll'' class D < C def foo_bar 48 end end p D.new.FooBar # => 123 p D.new.foo_bar # => 48 The same rules hold here: foo_bar does NOT override FooBar (since FooBar is not virtual) and therefore is an unrelated method. So the first call resolves to C::FooBar and invokes it. The second call resolves to D#foo_bar and invokes it. Only if there wasn''t foo_bar or FooBar defined in D this call would resolve to C::FooBar. Tomas -----Original Message----- From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Shay Friedman Sent: Saturday, June 13, 2009 12:36 PM To: ironruby-core at rubyforge.org Subject: Re: [Ironruby-core] Sealed method can be overriden The same happens with non-virtual methods as well. By the way, should you be able to call super when you override a virtual method? Thanks, Shay. ---------------------------- Shay Friedman http://www.ironshay.com Follow me: http://twitter.com/ironshay -- Posted via http://www.ruby-forum.com/. _______________________________________________ Ironruby-core mailing list Ironruby-core at rubyforge.org http://rubyforge.org/mailman/listinfo/ironruby-core
Yes, super should work if the calling method is in a Ruby base class. C#: public class C { public virtual int FooBar() { return 123; } } Ruby: class D < C def foo_bar 10 * super end end p D.new.FooBar p D.new.foo_bar (there is a bug in the current bits that makes it throw an exception: `get_MethodHandle'': The requested operation is invalid for DynamicMethod. (TypeError), I''ll fix it asap). Tomas -----Original Message----- From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Shay Friedman Sent: Saturday, June 13, 2009 12:36 PM To: ironruby-core at rubyforge.org Subject: Re: [Ironruby-core] Sealed method can be overriden The same happens with non-virtual methods as well. By the way, should you be able to call super when you override a virtual method? Thanks, Shay. ---------------------------- Shay Friedman http://www.ironshay.com Follow me: http://twitter.com/ironshay -- Posted via http://www.ruby-forum.com/. _______________________________________________ Ironruby-core mailing list Ironruby-core at rubyforge.org http://rubyforge.org/mailman/listinfo/ironruby-core