[vala/wip/issue548: 33/34] vala: Allow explicit interface methods to be virtual



commit dd725930c0bedb6f1d456223589aa692a7c23f0e
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sat Dec 1 15:27:08 2018 +0100

    vala: Allow explicit interface methods to be virtual

 tests/Makefile.am                              |  1 +
 tests/objects/classes-interfaces-virtuals.vala | 34 ++++++++++++++++++++++++++
 vala/valaclass.vala                            |  5 ----
 vala/valaobjecttypesymbol.vala                 |  9 ++++++-
 4 files changed, 43 insertions(+), 6 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index a806956cc..3977a70d4 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -255,6 +255,7 @@ TESTS = \
        objects/class_only.vala \
        objects/classes.vala \
        objects/classes-interfaces.vala \
+       objects/classes-interfaces-virtuals.vala \
        objects/compact-class.vala \
        objects/compact-class-destructor.vala \
        objects/constructor-variadic.test \
diff --git a/tests/objects/classes-interfaces-virtuals.vala b/tests/objects/classes-interfaces-virtuals.vala
new file mode 100644
index 000000000..cf56c62b7
--- /dev/null
+++ b/tests/objects/classes-interfaces-virtuals.vala
@@ -0,0 +1,34 @@
+interface IFoo : Object {
+       public abstract int foo ();
+}
+
+interface IBar : Object {
+       public abstract string foo ();
+}
+
+class Foo : Object, IFoo, IBar {
+       public virtual int IFoo.foo () {
+               return 42;
+       }
+       public string IBar.foo () {
+               return "foo";
+       }
+}
+
+class Bar : Foo {
+       public override int foo () {
+               return 23;
+       }
+}
+
+void main () {
+       var foo = new Foo ();
+       assert (foo.foo () == 42);
+       assert (((IBar) foo).foo () == "foo");
+
+       var bar = new Bar ();
+       assert (bar.foo () == 23);
+       assert (((Foo) bar).foo () == 23);
+       assert (((IFoo) bar).foo () == 23);
+       assert (((IBar) bar).foo () == "foo");
+}
diff --git a/vala/valaclass.vala b/vala/valaclass.vala
index a07bc11e4..7ae97bdec 100644
--- a/vala/valaclass.vala
+++ b/vala/valaclass.vala
@@ -299,11 +299,6 @@ public class Vala.Class : ObjectTypeSymbol {
                }
 
                base.add_method (m);
-               // explicit interface method implementation
-               if (m.base_interface_type != null) {
-                       scope.remove (m.name);
-                       scope.add (null, m);
-               }
        }
 
        public HashMap<Method,Method> get_implicit_implementations () {
diff --git a/vala/valaobjecttypesymbol.vala b/vala/valaobjecttypesymbol.vala
index 66e7f955f..ce17d63c1 100644
--- a/vala/valaobjecttypesymbol.vala
+++ b/vala/valaobjecttypesymbol.vala
@@ -115,7 +115,14 @@ public abstract class Vala.ObjectTypeSymbol : TypeSymbol {
        public override void add_method (Method m) {
                methods.add (m);
                members.add (m);
-               scope.add (m.name, m);
+
+               // explicit interface method implementation
+               // virtual/abstract methods needs to be scoped and overridable
+               if (this is Class && m.base_interface_type != null && !(m.is_abstract || m.is_virtual)) {
+                       scope.add (null, m);
+               } else {
+                       scope.add (m.name, m);
+               }
        }
 
        /**


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]