1

been reading this article on Traits in Computer programming.

The opening sentence highlights two points

  • Traits both provide a set of methods that implement behaviour to a class,
  • and require that the class implement a set of methods that parameterize the provided behaviour.

The behaviour that the definition talks about seems to suggest it is duck typing.

I did read What is duck typing and the following example in C++ helps with that.

template <typename T>
void f (T x) {
    x.method ();
}

We have effectively parametrized the method 'f' with type 'T'. The fact that it done at compile time and not at runtime shouldn't make a difference.

As highlighted in the Wikipedia article on Traits It is not clear to me, how is a trait is different from duck typing ? Appreciate any pointers.

Thanks for your time.

inout
  • 65
  • 6
  • Are you asking about traits as the term is usually used in C++ metaprogramming, or in the sense of named interfaces in the Wikipedia article? – Davis Herring Jul 03 '20 at 01:12
  • @DavisHerring the question is in the context of wikipedia article. How is a trait different from duck typing ? – inout Jul 03 '20 at 01:24
  • 2
    In essence, traits rely on duck typing. Traits are a particular way, a particular design pattern, of using duck typing. – Igor Tandetnik Jul 03 '20 at 03:28

0 Answers0