2

Say I have something looking like this:

def curriedHelper[T](foo: String)(f: String => T) = f(foo)

This is just something simplified. Point being that I have some work to be done and later apply another transformation where the resulting type is yet to be determined. Maybe foo here is a db-collection name and f is a function from a DB-cursor to different record types.

Here Scala's currying falls short for me since T needs to be fixed already on partial application. Its simply not useful since Scala fixes T to Nothing:

scala> val partiallyAppliedToNothing = curriedHelper("bar") _
partiallyAppliedToNothing: (String => Nothing) => Nothing = <function1>

So then I have typically done something like

def curriedHelper2(foo: String) = new {
  def apply[T](f: String => T) = f(foo)
}

val better = curriedHelper2("bar")

However, this I just noticed uses reflection

val usesReflection = better{ x =>
    try {
    throw new NullPointerException
  } catch {
    case e => e.printStackTrace
  }
  x
}

Stacktrace show reflection being used to invoke apply. I can't understand why this needs to be reflection like structural typing in general. Could just be an inner class, right?

So I guess the only thing remaining is to name this to avoid reflection:

trait WhatDoICallThis[F] {
  def apply[T](f: (F => T)): T
}

def curriedHelper3(foo: String) = new WhatDoICallThis[String] {
  def apply[T](f: String => T) = f(foo)
}

Are there any standard classes for this?

Viktor Hedefalk
  • 3,572
  • 3
  • 33
  • 48
  • Possible duplicate of http://stackoverflow.com/questions/21827014/typed-function-and-currying-in-scala/21834046#21834046 – yǝsʞǝla Feb 21 '14 at 16:26
  • For the Nth time, multiple parameter lists *are not currying*! – Randall Schulz Feb 21 '14 at 16:47
  • @RandallSchulz: The [Scala language specification disagrees](http://stackoverflow.com/a/19916445/334519). – Travis Brown Feb 21 '14 at 17:11
  • If you're going to make a claim about the spec, then give a reference to the spec. They quite simply are not at all the same thing, they simply cause people confusion, for some reason. – Randall Schulz Feb 21 '14 at 17:22
  • It's difficult to link to specific passages in the spec, and I quote the relevant example (1.2.4, for what it's worth) in the linked answer (which also explains why the word may not be 100% appropriate in this context). – Travis Brown Feb 21 '14 at 17:37
  • I plan to learn me more haskell for great good and to read @TravisBrown answers. I can't google the html version of the SLS someone cooked up. Does the fault lie with me, my stars, or the way google presents search results? – som-snytt Feb 22 '14 at 07:32
  • @AlekseyIzmailov, I actually read that possible duplicate and Kevin's answer was to use a case class. The reflection part was maybe just an unnecessary sidetrack in posing my question. Case class or just closing over the data needed with a trait, I'm asking if there is a standard way to do this to avoid naming my own class. Maybe scalaz has something? – Viktor Hedefalk Feb 22 '14 at 08:05

0 Answers0