4 ms·
Say a function requires an object of type t or it will fail, in a dynamic language it is often fine to assume if the object is not a t it is a valid initializer
by justincormack 14y ago
Say a function requires an object of type t or it will fail, in a dynamic language it is often fine to assume if the object is not a t it is a valid initializer for a t instead. Eg a string could be used as an initializer for a date. The problem with the map example is the ambiguity.
- laumars 14y agoBut that's just poor coding practices as you're letting any old garbage in as data without first validating it (and that's assuming such data was user generated. If the garbage was generated within your code itself then you have even bigger problems). Don't get me wrong, I'm not against loosely typed languages (eg in Perl it's very handy being able to use strings and integers as booleans), but that example you've given would scare me in any language as you shouldn't be making any assumptions about data unless the program generated that data itself (and even then, I'd still run a few checks if just to catch unconsidered exceptions / security vulnerabilities)
- justincormack 14y agoNot necessarily, the initialiser should do validation, and can still throw...