Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • many functional languages can do this trivially... Scala, F#, and the other languages that play nicely with OOP, But Haskell too... basically any language that allows you to define types and their behavior supports this. Commented May 21, 2015 at 12:32
  • @AK_ I'm aware F# can do this (though IIRC it requires some minor hoop-jumping) and guessed Scala could as another cross-paradigm language. Interesting that Haskell can do it- got a link? What I'm really looking for is the functional-idiomatic answer, rather than specific languages which offer a feature. But of course things can get rather fuzzy and subjective once you start talking about what is idiomatic, which is why I left it out of the question. Commented May 21, 2015 at 12:38
  • 1
    For cases where precondition can't be checked at compile-time, it is idiomatic to check in the constructor. Consider a PrimeNumber class. It would be too expensive to perform multiple redundant checks for primality for each operation, but it is not a kind of test that can be performed at compile-time. (A lot of operations you would like to perform on prime numbers, say multiplication, do not form a closure, i.e. results are probably not guaranteed prime. (Posting as comments since I don't know functional programming myself.) Commented May 21, 2015 at 12:53
  • A seemingly unrelated question, but ... Are asserts or unit tests more important? Commented May 21, 2015 at 12:58
  • @rwong Yeah, some nice examples there. I'm actually not 100% clear what ultimate point you're driving at, though. Commented May 21, 2015 at 13:01