Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

scala

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.

like image 846
Willis Blackburn Avatar asked Jun 18 '10 10:06

Willis Blackburn


People also ask

What is trait in Scala?

A Trait is a concept pre-dominantly used in object-oriented programming, which can extend the functionality of a class using a set of methods. Traits are similar in spirit to interfaces in Java programming language. Unlike a class, Scala traits cannot be instantiated and have no arguments or parameters.

What is a traits function?

Functional traits are morphological, biochemical, physiological, structural, phenological, or behavioral characteristics that are expressed in phenotypes of individual organisms and are considered relevant to the response of such organisms to the environment and/or their effects on ecosystem properties (Violle et al.

What is the difference between trait and interface in Scala?

Like a class, Traits can have methods(both abstract and non-abstract), and fields as its members. Traits are just like interfaces in Java. But they are more powerful than the interface in Java because in the traits we are allowed to implement the members.

Why trait is used in Scala?

Traits are used to share interfaces and fields between classes. They are similar to Java 8's interfaces. Classes and objects can extend traits, but traits cannot be instantiated and therefore have no parameters.


1 Answers

If you make it a Trait and still want to be able to use the lightweight function syntax, you could also additionally add an implicit conversion in the places where you want them:

scala> trait Computable extends (Int => Int)
defined trait Computable

scala> def computes(c: Computable) = c(5)
computes: (c: Computable)Int

scala> implicit def toComputable(f: Int => Int) = new Computable { def apply(i: Int) = f(i) }
toComputable: (f: (Int) => Int)java.lang.Object with Computable

scala> computes( (i: Int) => i * 2 )
res0: Int = 10
like image 198
Mirko Stocker Avatar answered Sep 20 '22 04:09

Mirko Stocker