Would you put the param list in parens even if the type were not an arrow type or even if no type were specified? When I first started learning Scala, I always put the parameter list in parens, but stopped over time.
And BTW, it seems you tried to implement a builder pattern here. The usual approach would be to use a mutable collection inside a builder function like this and then call .toSet when returning it. Otherwise you create lots of immutable Sets.
edit:
def withSetCollector[A](f: (A => Unit) => Unit): Set[A] = {
val objects = collection.mutable.Set[A]()
f(objects.add)
objects.toSet
}
edit 2: Speaking of builders, you could also use
def withSetCollector[A](f: (A => Unit) => Unit): Set[A] = {
val b = Set.newBuilder[A]
f(b.addOne)
b.result()
}
Thanks, i’ve made you suggested change in my code. It is a good suggestion.
BTW, when I look at that code, I see another problem in the file which I don’t know how to convert from 2.12 to 2.13. The warning is View bounds are deprecated.
def withMaximizer[A <% Ordered[A]](init: A)(f: (A => Unit) => Unit): A = {
withReducer(init, (a:A,b:A)=>if (a < b) b else a)(f)
}
def withMinimizer[A <% Ordered[A]](init: A)(f: (A => Unit) => Unit): A = {
withReducer(init, (a:A,b:A)=>if (a > b) b else a)(f)
}
Yes, view bounds where a mechanism to say: there should be some conversion from type A to some other type B (so A could be “viewed” as a B). That’s the same as stating there should be a mapping / function from A to B implicitly available…
IntelliJ provides a quick fix for this (I’m an the latest version IntelliJ 20.03 and Scala plugin).
It would rewrite this to:
def withMaximizer[A](init: A)(f: (A => Unit) => Unit)(implicit ev$1: A => Ordered[A]): A = {
withReducer(init, (a:A,b:A)=>if (a < b) b else a)(f)
}