Are there any static duck-typed languages?

前端 未结 15 1019
我寻月下人不归
我寻月下人不归 2020-12-14 10:12

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

15条回答
  •  天涯浪人
    2020-12-14 10:25

    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.

提交回复
热议问题