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

a partially applied function and a type class

4 replies
ethul
Joined: 2009-09-23,
User offline. Last seen 2 years 21 weeks ago.
hi, i was just reading up on type classes and wanted to give it a try.
so i wrote an example type class
abstract class LogFormatter[A] {  def format(a: A): String }
object LogFormatter {  implicit object StringLogFormatter extends LogFormatter[String] {    def format(s: String) = ...  }    implicit object ExceptionLogFormatter extends LogFormatter[Exception] {     def format(e: Exception) = ...  }}
i wanted to use this like so
object Logger {  def log[A: LogFormatter](level: Int)(a: A) {     val formatted = implicitly[LogFormatter[A]].format(a)    ...  }}
and then do
val x = Logger.log(0) _x("s") x(new RuntimeException)
but it seems i am unable to, because with the partially applied function, the implicit LogFormatter type is Nothing and i don't want to define this in my LogFormatter object.
any suggestions for how this might be accomplished, or for a better approach, are most welcome.
thanks! -Eric
Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: a partially applied function and a type class

http://gist.github.com/502572

-jason

On Sat, Jul 31, 2010 at 10:14 PM, Eric Thul wrote:
> hi, i was just reading up on type classes and wanted to give it a try.
> so i wrote an example type class
>
> any suggestions for how this might be accomplished, or for a better
> approach, are most welcome.

Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: a partially applied function and a type class

Would it make sense to allow currying to delay type parameters, or to allow curried type parameters without explicitly encoding it like jason's example?

On Jul 31, 2010 4:27 PM, "Jason Zaugg" <jzaugg [at] gmail [dot] com> wrote:

http://gist.github.com/502572

-jason


On Sat, Jul 31, 2010 at 10:14 PM, Eric Thul <eric [dot] thul [at] gmail [dot] com> wrote:
> hi, i was just reading u...

> any suggestions for how this might be accomplished, or for a better
> approach, are most welcome.

ethul
Joined: 2009-09-23,
User offline. Last seen 2 years 21 weeks ago.
Re: a partially applied function and a type class
thanks for the replies, this is perfect!

On Sat, Jul 31, 2010 at 4:55 PM, Josh Suereth <joshua [dot] suereth [at] gmail [dot] com> wrote:

Would it make sense to allow currying to delay type parameters, or to allow curried type parameters without explicitly encoding it like jason's example?

On Jul 31, 2010 4:27 PM, "Jason Zaugg" <jzaugg [at] gmail [dot] com> wrote:

http://gist.github.com/502572

-jason


On Sat, Jul 31, 2010 at 10:14 PM, Eric Thul <eric [dot] thul [at] gmail [dot] com> wrote:
> hi, i was just reading u...

> any suggestions for how this might be accomplished, or for a better
> approach, are most welcome.


Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: a partially applied function and a type class

Partial application of method type parameters implies that
FunctionN#apply must be type parametric (to accept the remaining type
params.) In addition, any view or context bounds of these type params
would need map to extra implicit parameters of this apply method. Not
entirely straightforward...

Runar's recent post, "Higher Rank Polymorphism in Scala" [1] shows how
to encode one specific case of this sort of thing.

The toolbox for abstraction in this area is far from complete!

-jason

[1] http://apocalisp.wordpress.com/2010/07/02/higher-rank-polymorphism-in-sc...

On Sat, Jul 31, 2010 at 10:55 PM, Josh Suereth wrote:
> Would it make sense to allow currying to delay type parameters, or to allow
> curried type parameters without explicitly encoding it like jason's example?
>
> On Jul 31, 2010 4:27 PM, "Jason Zaugg" wrote:
>
> http://gist.github.com/502572
>
>> any suggestions for how this might be accomplished, or for a better
>> approach, are most welcome.

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