3 ms·
A type system can really help here as well. sendUserEmail :: FromEmail -> ToEmail -> [EmailHeader] -> EmailBody -> IO () However that also requires the di
by codygman 10y ago
A type system can really help here as well.
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.
- ubernostrum 10y agoNothing about your suggested solution requires or is exclusive to a statically-typed language. "Replace a send-to-arbitrary-number-of-addresses function with a send-to-exactly-one-address function" is possible in dynamically-typed languages, too, and as you openly admit even a statically-typed language is likely to model email sending in a way that accepts multiple recipient addresses. 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.
- Johnny_Brahms 10y agoi thought of that as well.Went and looked at my mail list programme in Racket, and the mail procedure takes many recipients, but in the form if rest arguments, like: (define (send-mail msg . recipients) ...) 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.
- codygman 10y ago> 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. 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.
- andrewstuart2 10y agoWell, using the type system, weak or strong, to force correct code either at compile time or runtime would help. I can write a function that uses Java reflection for polymorphism as easily as I can use JavaScript reflection to enforce an API contract. // 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.