jll63 / openmethods.d

Open multi-methods for the D language. OPEN! Multi is cool. Open is great.
44 stars 8 forks source link

Type modifiers are ignored (const, immutable) #15

Closed Geod24 closed 6 years ago

Geod24 commented 6 years ago
diff --git a/examples/synopsis/source/app.d b/examples/synopsis/source/app.d
index 4279ffc..c2e6ae5 100644
--- a/examples/synopsis/source/app.d
+++ b/examples/synopsis/source/app.d
@@ -8,7 +8,7 @@ class Cat : Animal {}
 class Dolphin : Animal {}

 // open method with single argument <=> virtual function "from outside"
-string kick(virtual!Animal);
+string kick(immutable virtual!Animal);

 @method // implement 'kick' for dogs
 string _kick(Dog x) // note the underscore
Running ./synopsis 
kick snoopy: bark
kick hector: bark and bite
hector meets felix: chase
hector meets snoopy: wag tail
hector meets flipper: ignore

According to this example, const and immutable are casted away. I'd expect that a non const implementation would error out. Likewise, I'd expect an immutable implementation of a const function to error. A const implementation of an immutable method should be fine though.

jll63 commented 6 years ago

You misplaced the modifier. This works:

@@ -8,16 +8,16 @@ class Cat : Animal {}
 class Dolphin : Animal {}

 // open method with single argument <=> virtual function "from outside"
-string kick(virtual!Animal);
+string kick(virtual!(const Animal));

 @method // implement 'kick' for dogs
-string _kick(Dog x) // note the underscore
+string _kick(const Dog x) // note the underscore
 {
   return "bark";
 }

 @method("kick") // use a different name for specialization
-string notGoodIdea(Pitbull x)
+string notGoodIdea(const Pitbull x)
 {
   return next!kick(x) ~ " and bite"; // aka call 'super'
 }