Hi, 2009/10/28 Patrick Castle <lem...@bigpond.com>: > Just stuffing about at the moment getting my head about creating classes in > Genie. From what I gather, every variable you declare outside of a method is a > member/property of that class. > > What I'm finding a bit confusing is there isn't just one way to declare a > property it seems - but both ways don't work consistently. > > For example, I have class "Test" and I want it to have a private property "a" > not accessible to external callers. It seems like these are the options: > > class Test : Object > _a : int > > or > > class Test : Object > prop private a : int > > or > > class Test : Object > prop _a : int the first one actually defines a field, and the two others are properties > > but not > > class Test : Object > private a : int > > My first question - is there any reason why that last one shouldn't compile? I don't think so (actually I don't know the syntax for private fields, but this seems fine) > Also - why actually have so many ways to declare the same thing? Is it > deliberate or just an accident of the syntax checking? the field/property thing aside, there is only that a member starting with an underscrore is implicitly private. > > Also - does Genie have the concept of a class variable, or are they all just > instance variables? My initial temptation was to use static in the declaration > > class Test : Object > prop static a : int > > This actually starts spitting out all sorts of errors when compiling the C > source, so perhaps it gets further than it should. Is it an illegal > declaration? You can try removing prop (thus creating a field), but I don't think this should create errors. > And if so, should it be caught before it gets as far as the C source being > created and compiled? Sure. The rule of thumb here is that a C compiler error is a bug either in valac or in the vapi (in case you're using a library). > > On the Genie homepage it says: > All methods, properties and events can also take modifiers to define further > options. > > I assumed that would mean that all modifiers were legal. So I thought I should > try the other modifiers. > > abstract: > > class Test : Object > prop abstract a : int > > This one compiles OK, but if you create an instance of the class and assign a > value to the property you get a Segmentation Fault when you run it. this souldn't compile afaik (class Test should be abstract for this to work). > > I looked at the definition of abstract and it seems to say that it should not > be > defined in the class in which it's declared but only in the subclass. So I > created a subclass and defined it there instead and I still get a clean > compile > but a Segmentation Fault when running. I think it sould be : (I'm not sure about genie syntax for abstract classes)
class abstract Test : Object prop abstract a : int class Test2 : Test _a : int prop override a: int get return _a set _a = value > > I'm guessing that this is only meant for methods rather than properties - but > if > so, it would probably be a good idea to make a distinction on the web page > between method modifiers and property modifiers. Seems like the remaining > modifiers look relevant only to methods as well. abstract, virtual and override should work for properties as well, the only thing I see as method only is async. (and readonly is property only) > > Also - I think rather than getting a Segmentation Fault, improper use of these > modifiers should probably throw exceptions during the compile. absolutely. > > So to summarise, declaration of properties implies public access unless > keyword > private is used (or underscore). Your options are: _var, prop private var, > prop > _var. your options are prop _var or prop private var, but generally you wouldn't use a private property (unless you need some processing) but rather private field. (or a readonly property if you don't want the property to be modified) > > There is no such thing as a class variable in Genie. there should be both class variables and static variables (I'm not sure about the difference, but I think that class variables are specific to a class, so althought they are inherited, they don't necessarily have the same value in a subclass) HTH, Abderrahim _______________________________________________ Vala-list mailing list Vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list