by andrewstuart2 · 10y ago · 0 comments
getEmailBody(users[:i])
instead of getEmailBody(users[i])
I typically prefer a high level of polymorphism in my code/APIs (sensibly handling single inputs vs. arrays) but this is a great counter-example even if not the actual root cause. Every feature is also a liability. Double edged sword. Etc.Assignment is interpreted as add-to-the-list operator.
Python 2.7: https://hg.python.org/releasing/2.7.9/file/tip/Lib/email/mes...
Python 3.4: https://hg.python.org/releasing/3.4/file/tip/Lib/email/messa...
Ouch.
sendUserEmail :: FromEmail -> ToEmail -> [EmailHeader] -> EmailBody -> IO ()
However that also requires the discipline of wrapping a lower level function that is probably: sendEmails :: FromEmail -> [ToEmail] -> [EmailHeader] -> EmailBody
However even a functional language would shy away from using indexing which could be argued to be the source of this problem.So your "type system can really help" is really just an irrelevancy you've come up with to try hide your attempt to shove your preferred programming paradigm onto other people.
You should probably stop doing that.
Not exactly, but it is a weaker advantage than I thought when writing it. The advantage is that languages like Haskell encourage specializing functions in that manner which avoid that specific bug.
However so does test driven development which is just as possible in dynamic languages.
The advantage the statically typed language has over even the dynamically typed language plus tdd is that the program won't compile whereas dynamic language plus tdd relies on programmer discipline.
passing it a list makes the recipients arg a list with a list of recipients. To actually send to multiple recipients I explicitly have to use the apply procedure that passes all list elements as arguments.
as you said, not a staticly typed language. types would have made an eventual error easier to debug though.
// JavaScript
if ( Array.isArray(address) ) {
throw new Error("Must only pass a single email address.");
}
// Java
void sendEmail(Object recipientOrRecipients) { ... }
There are times when polymorphism is low-risk, and there are times when it's better safe than sorry. Best to know your risk model (and your libraries) and act accordingly. for user in users:
getEmailBody(user)
instead of for i in range(len(users)):
getEmailBody(users[i])
(for the languages that let you do so).