How is duck typing different from the old 'variant' type and/or interfaces?

后端 未结 10 999
无人及你
无人及你 2021-01-30 03:32

I keep seeing the phrase \"duck typing\" bandied about, and even ran across a code example or two. I am way too lazy busy to do my own research, can someone tel

10条回答
  •  执笔经年
    2021-01-30 03:47

    Try reading the very first paragraph of the Wikipedia article on duck typing.
    Duck typing on Wikipedia

    I can have an interface (IRunnable) that defines the method Run().
    If I have another class with a method like this:
    public void RunSomeRunnable(IRunnable rn) { ... }

    In a duck type friendly language I could pass in any class that had a Run() method into the RunSomeRunnable() method.
    In a statically typed language the class being passed into RunSomeRunnable needs to explicitly implement the IRunnable interface.

    "If it Run() like a duck"

    variant is more like object in .NET at least.

提交回复
热议问题