0

the code will output

1
2
2

the 1 only print once. I have no idea, how to write it in genie.

below is vala code:

public static void main() {
    var a = new One();
    var b = new One();
}

class One : Object {
    class construct {
        stdout.puts("1\n");
    }
    public One () {
        stdout.puts("2\n");
    }
}

What is the equivalent code class construct method for genie?

If Genie's init? (NOW) it is diffrent from vala's class construct?

Genie's init

below can't works!

init
    var a = new One
    var b = new One

class One
    construct ()
        stdout.puts("2\n")
    init
        stdout.puts("1\n")

construct block require GLib.object

construct block => init block

but Vala's class construct dosen't.

so vala will works, but Genie dosen't,

vala code:

class One {
    class construct {
        stdout.puts("1\n");
    }
    public One () {
        stdout.puts("2\n");
    }
}

Why this functionality is useful? Actually I never use class before. but I think it is useful, very useful.

example: init some static fileds ( or class fields: that another question).

I don't know why? Why Vala implement this feature? It must be useful if Vala implement it.

Chris Kitching
  • 2,559
  • 23
  • 37
Zee
  • 78
  • 6

1 Answers1

2

Fom what I can tell from the documentation I thought it was init. Although I'm not sure why you would want to use it. It might be useful for some lazy loading of static data used in all instances of the class, but I've never done that myself.

In object oriented programming terminology a class is "instantiated" when it is created as an object. The process of instantiation can be customized with a "constructor". In Vala and Genie it is also possible to customize the process when an object is no longer needed with a "destructor".

If a method does not act upon the data in the object it is called a static method. By definition a constructor does not act upon the data in the object, but returns a new object. So constructors are always static and static construct is the wrong name. In Vala there is class construct and if you change your code to use that you get the same result. See Vala different type of constructors for a thorough treatment on this subject in Vala. The part about class construct is at the end.

My understanding was Genie's init was the equivalent of Vala's class construct. I think your question has uncovered a problem in Genie. My understanding was this code would answer your question:

[indent = 4]
init
    var a = new One()
    var b = new One()
    var c = new One.alternative_constructor()

class One:Object
    init
        print "Class 'One' is registered with GType"

    construct()
        print "Object of type 'One' created"

    construct alternative_constructor()
        print """Obect of type 'One' created with named constructor
'alternative_constructor', but the design of your
class is probably too complex if it has multiple
constructors."""

    def One()
        print "This is not a constructor"

    final
        print "Object of type 'One' destroyed"

This, however, outputs:

Class 'One' is registered with GType
Object of type 'One' created
Class 'One' is registered with GType
Object of type 'One' created
Class 'One' is registered with GType
Obect of type 'One' created with named constructor 
'alternative_constructor', but the design of your 
class is probably too complex if it has multiple 
constructors.
Object of type 'One' destroyed
Object of type 'One' destroyed
Object of type 'One' destroyed

Whereas the registration with GType only happens once so the init code is being put in the wrong place and getting called with each instantiation not with the type registration. So at present I don't think there is an equivalent of Vala's class construct in Genie.

Changing the behaviour of init may be one solution, but I may have misunderstood its original purpose and there is probably plenty of code out there now that relies on its current behaviour. The other solution is to write a patch for the Genie parser that implements this. You would need to make a good case why this functionality is useful.

Community
  • 1
  • 1
AlThomas
  • 4,169
  • 12
  • 22
  • you are right. I read it from vala's doc. **class construct** is my question. I dont't know why I type it to **static**. I'll update my question. thanks! – Zee Jan 11 '16 at 03:55
  • Hi @AlThomas! **"You would need to make a good case why this functionality is useful."** It confused me! Vala and Genie is **ONE** language, but have diffrent syntax. Why? Vala can do this? but Genie dosen't? – Zee Jan 11 '16 at 05:44
  • Vala and Genie use the same Vala compiler, so yes this functionality can be added to Genie. Genie is a free and open source software project, so you can download the source code and add the functionality to the Genie parser. This of course takes time and skill. I've not found a use for `class construct` in Genie, so I don't think I will be writing that myself. If you need to initialise static fields then this can be done without the need for `class construct`, but that is another question. StackOverflow uses a question and answer format and I think your original question was answered. – AlThomas Jan 11 '16 at 09:00