Properties and inheritance

Monkey Forums/Monkey Bug Reports/Properties and inheritance

michaelcontento(Posted 2012) [#1]
Hi,

Strict

Class A
    Private

    Field _value:Int

    Public

    Method value:Void(newValue:Int) Property
        _value = newValue
    End

    Method value:Int() Property
        Return _value
    End
End

Class B Extends A
    Method value:Void(newValue:Int) Property
        Print "Changed!"
        Super.value(newValue)
    End
End

Function Main:Int()
    Local b:B = New B()
    b.value = 42
    Print b.value
    Return 0
End


leads to

properties.monkey<29> : Error : Unable to find overload for value().


The error disappears when I a) remove the method from B or b) add this code to B

    Method value:Int() Property
        Return Super.value()
    End


It would be great if property methods can be used like normal methods for inheritance :)


Rone(Posted 2012) [#2]
the behavior is the same with methods...


michaelcontento(Posted 2012) [#3]
Hi,

no, IMHO, it's not - because the following example works without any problems:

Strict                                
    
Class A    
    Private    
    
    Field _value:Int    
    
    Public    
    
    Method SetValue:Void(newValue:Int)
        _value = newValue    
    End    
    
    Method GetValue:Int()    
        Return _value    
    End    
End    
    
Class B Extends A    
    Method SetValue:Void(newValue:Int)
        Print "Changed!"    
        Super.SetValue(newValue)    
    End    
End    
    
Function Main:Int()    
    Local b:B = New B()    
    b.SetValue(42)    
    Print b.GetValue()    
    Return 0    
End    


The problem seems to be a special case with properties. The version with manual setter and getter methods, second example, works as expected and only the version with properties, first example but same approach, not.

It seems that both property methods (set and get) are expected to be in the same class, if one was found.

All the best,
Michael


Rone(Posted 2012) [#4]

It seems that both property methods (set and get) are expected to be in the same class, if one was found.


Yes, but it is the same with methods, if they have the same name...So, when overriding a method/property in a derived class, all the overloadings, which the derived class implements, must be overridden.
Strict

Class A
    Private

    Field _value:Int

    Public

    Method value:Void(newValue:Int) 
        _value = newValue
    End

    Method value:Int() 
        Return _value
    End
End

Class B Extends A
    Method value:Void(newValue:Int) 
        Print "Changed!"
        Super.value(newValue)
    End
End

Function Main:Int()
    Local b:B = New B()
    b.value(42)
    Print b.value
    Return 0
End


while it works in C# as expected.

class Class1
    {
        private int _value;

        public virtual void Value(int value)
        {
             _value = value;
        }

        public virtual int Value()
        {
            return _value;
        }
    };

    class Class2 : Class1
    {
        public override void Value(int value)
        {
            System.Console.WriteLine("Changed");
            base.Value(value);
        }
    }