Trait, FunctionN, or trait-inheriting-FunctionN in Scala?

Posted by Willis Blackburn on Stack Overflow See other posts from Stack Overflow or by Willis Blackburn
Published on 2010-06-18T10:13:45Z Indexed on 2010/06/18 10:33 UTC
Read the original article Hit count: 233

Filed under:

I have a trait in Scala that has a single method. Call it Computable and the single method is compute(input: Int): Int. I can't figure out whether I should

  • Leave it as a standalone trait with a single method.
  • Inherit from (Int => Int) and rename "compute" to "apply."
  • Just get rid of Computable and use (Int => Int).

A factor in favor of it being a trait is that I could usefully add some additional methods. But of course if they were all implemented in terms of the compute method then I could just break them out into a separate object.

A factor in favor of just using the function type is simplicity and the fact that the syntax for an anonymous function is more concise than that for an anonymous Computable instance. But then I've no way to distinguish objects that are actually Computable instances from other functions that map Int to Int but aren't meant to be used in the same context as Computable.

How do other people approach this type of problem? No right or wrong answers here; I'm just looking for advice.

© Stack Overflow or respective owner

Related posts about scala