Is it possible to call an interface method?

I’m just starting to learn Vala using the tutorial. Is it possible to call an interface’s method? Here are two snippets based on code from the tutorial, the first works:

// This works
public interface Callable : GLib.Object {
   public abstract bool answering { get; protected set; }
   public virtual bool hang ()
   {
      answering = false;
      return true;
   }
}

public class TechPhone : GLib.Object, Callable
{
   public bool answering { get; protected set; }
   public bool hang ()
   {
      stdout.printf ("TechPhone.hang () implementation!");
      answering = false;
      return true;
   }
   public static void main ()
   {
      var f = new TechPhone ();
      if (f.hang ())
         stdout.printf("Hand done.\n");
      else
         stdout.printf("Hand Error!\n");
      stdout.printf("END\n");
   }
}

But this code won’t compile:

// Doesn't work: note that Callable is the same as above so omitted
public class TechPhone : GLib.Object, Callable
{
   public bool answering { get; protected set; }
   public bool hang ()
   {
      stdout.printf ("TechPhone.hang () implementation!");
      return Callable.hang();
   }
   public static void main ()
   {
      var f = new TechPhone ();
      if (f.hang ())
         stdout.printf("Hand done.\n");
      else
         stdout.printf("Hand Error!\n");
      stdout.printf("END\n");
   }
}

The error is:

test.vala:16.14-16.28: error: Access to instance member `Callable.hang' denied
      return Callable.hang();
             ^^^^^^^^^^^^^^^
Compilation failed: 1 error(s), 0 warning(s)

PS I hope this is the right category: I couldn’t find one for ‘learn’ or ‘help’.

1 Like

class methods need to be static. The same holds for interfaces. https://wiki.gnome.org/Projects/Vala/Manual/Classes#Class_methods

The question is a bit what you are trying to semantically achieve with a method tied to the interface itself and not to implementing instances.

I’m not trying to use a class method, but I tried base.hang() and that didn’t work and Callable.hang(this) etc. I want to know how to call an interface instance method from a subclass (if that is possible).

Ah sorry. IIRC I think this is not possible with GObject interfaces, but I might remember wrong.