Can I specify interfaces when I declare a member?
After thinking about this question for a while, it occurred to me that a static-duck-typed language might actually
In the latest version of my programming language Heron it supports something similar through a structural-subtyping coercion operator called as
. So instead of:
MyClass obj = new MyClass();
CallMyMethod(obj);
You would write:
MyClass obj = new MyClass();
CallMyMethod(obj as IMyInterface);
Just like in your example, in this case MyClass
does not have to explicitly implement IMyInterface
, but if it did the cast could happen implicitly and the as
operator could be omitted.
I wrote a bit more about the technique which I call explicit structural sub-typing in this article.