Search code examples
c#vala

vala interface with get property - compilation error


I am relatively new to Vala and I'm trying to port some C# code into vala by hand and I have started out with a trivial example to test it out.

C# coders will recognize the snippet below as being 1-1 mapping to C#.

Here is the test vala file Universe.vala:

namespace Universe
{
    public interface Planet
    {
        string Name { get; }
    }
}

I get the compilation error:

Universe.vala: error: property getter must have a body

It would appear that Vala does not support get(set)ter properties in interfaces - or am I missing something?. How do I fix this?


Solution

  • In Vala interfaces can still contain non-abstract properties and methods (but no variables). This means if you want to define an abstract property or method, you still need to use the abstract keyword, just like you would in an abstract class.

    Since you did not use the abstract keyword here, the compiler thinks you're trying to define a non-abstract property. However non-abstract properties in interfaces need an explicit body (because the default implementation would need to use variables, which are not allowed). So that's why the error message is complaining about the missing body.

    Just add the abstract keyword and it will work fine.