This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Better typeclass support: Infix function invocation and Parameter imports

2 replies
Ben Hutchison 3
Joined: 2009-11-02,
User offline. Last seen 42 years 45 weeks ago.

Typeclasses seem on the rise in Scala. The use of typeclasses (with or
simply instead of object-orientation) is/has becoming an increasingly
popular and respected method for building Scala software.

(Some background for the less typeclass-familiar reader: the typeclass
style separates functions from the data they act over. Scala's Equiv
[http://www.scala-lang.org/api/current/scala/math/Equiv.html] is an
easy example, Java's Comparator is another.)

There's one thing I find awkward about using a typeclass style in
Scala code. There's no straightforward way to invoke functions and
operators in infix syntax, without resorting to implicit conversions
merely to shuffle syntax around.

val myEquivalence: Equiv[T]
import myEquivalence._
val v1:T
val v2:T

//what's currently possible
assert(equiv(v1, v2))

//not currently possible without pimping, but would prefer these styles
assert(v1 equiv v2)
assert(v1 ≡ v2)

The way, for example, Scala's Numeric and Scalaz get around this is to
dress-up the first argument to the typeclass function with the infix
operator via an implicit conversion (see eg
[http://www.scala-lang.org/api/current/scala/math/Numeric$Ops.html]
and [http://scalaz.googlecode.com/svn/continuous/latest/browse.sxr/scalaz/Identity.scala.html]).
I feel this indirection brings a cost in performance and added
complexity.

If we had a direct means to tell Scala "this function identifier can
be placed between the first and second argument", it would allow
simpler, less encumbered use of typeclasses.

Of course, there's a potential ambiguity introduced by this: in the
example above, what if v1 itself has an equiv() method? I guess the
ambiguity, and backward compatibility hazards, could be resolved by
some set of precedence rules, where typeclass-style invocation is
tried after other options (ie direct invocation, implicit conversion)
have been exhausted.

Gong further, infix functions from typeclasses, as described above,
would combine nicely with another extension thats been mentioned
several times on list in the last 6 months: The ability to directly
import the members of parameters within the method parameter list
(including any context-bounds).

Some examples showing what could be possible when parameter imports
and infix functions are combined:

def aMethod[T](v1: T, v2: T)(implicit import equiv: Equiv[T]) = { v1 equiv v2 }

def aContextBoundExample[T: import Numeric](t: T) = t + t

And no implicit conversion magicks needed!

-Ben

Julien Richard-Foy
Joined: 2011-04-24,
User offline. Last seen 42 years 45 weeks ago.
Re: Better typeclass support: Infix function invocation and Para

I guess scala-virtualized [1] is a step toward your needs, but there’s
not so much documentation… It would be nice to know if scala-
virtualized aims to be merged with the standard scala compiler, what’s
the status of this project?

[1] https://github.com/TiarkRompf/scala-virtualized

Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: Re: Better typeclass support: Infix function invocation and
more here: http://www.scala-lang.org/node/8579

On 31 May 2011 09:21, Julien Richard-Foy <julien [dot] rf [at] gmail [dot] com> wrote:
I guess scala-virtualized [1] is a step toward your needs, but there’s
not so much documentation… It would be nice to know if scala-
virtualized aims to be merged with the standard scala compiler, what’s
the status of this project?

[1] https://github.com/TiarkRompf/scala-virtualized



--
Kevin Wright

gtalk / msn : kev [dot] lee [dot] wright [at] gmail [dot] com kev [dot] lee [dot] wright [at] gmail [dot] commail: kevin [dot] wright [at] scalatechnology [dot] com
vibe / skype: kev.lee.wrightquora: http://www.quora.com/Kevin-Wright
twitter: @thecoda

"My point today is that, if we wish to count lines of code, we should not regard them as "lines produced" but as "lines spent": the current conventional wisdom is so foolish as to book that count on the wrong side of the ledger" ~ Dijkstra

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland