Static duck-typing
Jul. 23rd, 2012 10:06 amWhenever I see duck typing described, it almost always begins "a style of dynamic typing where..." I agree that duck typing as a philosophy is common amongst dynamic language philosophies, but it seems duck typing and C++ templates are exactly the same concept with different names, which would naturally be called "dynamic duck typing" and "static duck typing".
So I was eternally confused why duck typing was always presented as a special dynamic-typing only concept, until I eventually wondered "is it actually conceptually unrelated to dynamic typing, it's just that they're commonly used together?" Is that correct, or is there a conceptual difference between static duck typing and dynamic duck typing, other than the differences between dynamic and static typing in general?
For that matter, the biggest difference seems to be that when in Python you can write "def func(a,b)" to mean "a function which accepts two arguments of any type" and there's no concise way of specifying that it should only accept some types, in C++ you ought to be able to write "auto func(auto a, auto b)" to mean "a function which accepts two arguments of any type", but in fact, you have to write "<template class A, class B, class C> C func(A a, B b)", so no-one ever does unless they have to.
So I was eternally confused why duck typing was always presented as a special dynamic-typing only concept, until I eventually wondered "is it actually conceptually unrelated to dynamic typing, it's just that they're commonly used together?" Is that correct, or is there a conceptual difference between static duck typing and dynamic duck typing, other than the differences between dynamic and static typing in general?
For that matter, the biggest difference seems to be that when in Python you can write "def func(a,b)" to mean "a function which accepts two arguments of any type" and there's no concise way of specifying that it should only accept some types, in C++ you ought to be able to write "auto func(auto a, auto b)" to mean "a function which accepts two arguments of any type", but in fact, you have to write "<template class A, class B, class C> C func(A a, B b)", so no-one ever does unless they have to.