Search code examples
genie

Immutable compact class's copy function for Genie


This code I translate from Vala's Memory Management Explained

section: Immutable compact classes with a copy function

I have try to run that vala's code. It work as aspected.

But I have some trouble now.

1: compile

error: syntax error, expected declaration but got `[' with previous end of line [Immutable]

2: I remove all attributes

error: Return with value in void function

return new Foo ()

3: give it a return value

construct copy (): Foo

error: syntax error, expected identifier

construct copy (): Foo

my code:

[indent = 4]

[Compact]
[Immutable]
[CCode (copy_function = "foo_copy")]
class Foo
    def method ()
        print "METHOD"

    construct copy ()
        return new Foo ()


init
    var foo = new Foo ()
    foo.method ()
    var bar = foo

How can I translate that to Genie correctly?


Solution

  • You have confused the return value, Foo, in Vala with a constructor. In Genie the code would be:

    [indent = 4]
    
    [Compact, Immutable, CCode (copy_function = "foo_copy")]
    class Foo
        def method ()
            pass
    
        def copy ():Foo
            return new Foo ()
    
    init
        var foo = new Foo ()
        foo.method ()
        var bar = foo
    

    Multiple attributes should be comma separated.