Re: [Vala] Vala 0.22.0 - requiring instance of Class to access Class class-members.



Hello,

This is the second time I've tried to post to the list, but I haven't 
seen my first attempt show up in the vala-list archive yet, so I thought 
I would resend, just in case.

I'm still learning my way around vala so I may be doing something wrong, 
but according to the docs at https://wiki.gnome.org/Vala/Manual/Classes:

  * Class members are shared between all instances of a class. They can
    be accessed without an instance of the class, and class methods will
    execute in the scope of the class.

I'll mark some points:

Class members are sharing ACCESS between all instances of a class.
They
 can     be accessed without an instance of the class(BUT HAVE TO 
SPECIFY ANOTHER INSTANCE TO GET TO), and class methods will     execute 
in the scope of the class(WITH A GIVEN INSTANCE).

There's an only one exception to my marking - static members.
Static
 members belong to the class, not to the instance, and therefore its 
value is "shared" between all instances and the class.

More about static vs non-static members(in OOP):
http://www.daniweb.com/software-development/csharp/threads/96978/static-vs-non-static-methods


This does not appear to be the case.  In the following example I cannot 
access the prop method of the Parent or Child classes without 
instantiating an instance of those classes.

public class Parent : Object {
     protected class int _prop;
     public class int prop() { return _prop; }
     class construct {
         _prop = 0;
     }
}

public class Child : Parent {
     class construct {
         _prop = 1;
     }
}

Parent p = new Parent();
print ("Parent.prop: %d\n", p.prop());         // Works as expected
print ("Parent.prop: %d\n", Parent.prop());    // error: Access to 
instance member `Parent.prop' denied

Child c = new Child();
print ("Child.prop: %d\n", c.prop());        // Works as expected
print ("Child.prop: %d\n", Child.prop());    // error: The name `prop' 
does not exist in the context of `Child'

Is this the desired behavior or are class methods, suppose to only be 
accessible from class instances?

Kind Regards,

Tom


This is desirable behavior.
Notice that it works fine when calling method "prop" by instance.
When you call this method by a class name, it fails, since ONLY static method are allowed to be called by a 
class name.
Child.prop() would fail even if prop() is static, since the method belongs to Parent, and not to Child.

I believe it's a bug in the compiler. It's non-sense to require an instance
when those are class-level instances.

Sorry, but I don't understand you. Why it's a bug in compiler?

However as a workaround you can use static fields. You can still initialize
them with class construct { } or static construct { }.

So why don't just do this?

Also if you were thinking of overriding a protected class field, I hardly
believe that's implemented (there's a bug somewhere about that)

No I checked this in the past, Vala doesn't support this.
It could theoretically can be possible by register this with GObject class struct, but it's a source for ABI 
breaks.
Better to implicate this with a virtual static get method.

I do accept that this could be a nice feature.

Tal
                                          


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]