Message from JavaScript discussions
September 2017
— With static in C++, you define one or more types a function can return, and which code to use for which types of parameters. Very cool template metaprogramming. You can include multiple types in some languages, too
Duck typing is more like checking the structure and properties of an object to see if it matches certain criteria
— "If it looks like a duck and quacks like a duck, it's a duck"
— In JS this is commonly used to validate incoming parameter objects. I wrote something that does it:
assert.props(obj, "prop" ...);
— Very handy :D
— Non-ducktyping version of this would be: e.g. typeof parameter == 'whatever'
— Yeah
— But that's kind of bad practice in languages with duck-typing, since it means you can't easily subclass/add behaviour
— Or instanceof or isPrototypeOf
— Complex duck typing can also validate functions, I think
— But that's more unit-testy and less production
— Well, instanceof would still work in JS:
class MyError extends SyntaxError {
...
}
new TypeError() instanceof Error // true
new MyError() instanceof Error // true