4

This function is provided

def foo( a: String = "bar", b: Int = 1, c: String = "default" ): String

Is there a way to create a partial function String => String without specifying a and b? My approach foo( c = _: String ) does unfortunately not compile. Are there any alternatives?

soulcheck
  • 36,297
  • 6
  • 91
  • 90
Taig
  • 6,718
  • 4
  • 44
  • 65
  • By `a` dou you mean the string "a"??. i.e. you want to give the default value as the name of the variable? – Jatin Aug 24 '13 at 13:27
  • By `a` I mean the parameter a, I'll modify the example bit to reduce the confusion (: – Taig Aug 24 '13 at 13:32
  • Just on a side note, you mean _partially applied function_. A _partial function_ is a function which is not defined for all inputs ([see the scala doc](http://www.scala-lang.org/api/current/index.html#scala.PartialFunction)) – Jens Hoffmann Aug 24 '13 at 15:20
  • 1
    And `(c: String) => foo(c = c)` isn't what you're looking for? – Travis Brown Aug 24 '13 at 15:57
  • @TravisBrown That's how I'm currently doing it. I've been hoping I could throw in a little more syntactic sugar to get rid of the boilerplate code. Looks like I'm out of luck. – Taig Aug 24 '13 at 17:08
  • 1
    This answer is popular http://stackoverflow.com/a/5259946/1296806 I think because of the phrase "tightest non-degenerate scope" which sounds kind of kinky. – som-snytt Aug 25 '13 at 14:58

1 Answers1

3

As noted by Travis this works:

def foo( a: String = "bar", b: Int = 1, c: String = "default" ): String = s"$a$b$c"                                                
val fooc = (c: String) => foo(c = c)            
fooc("myc")
//> res0: String = bar1myc        
huynhjl
  • 41,520
  • 14
  • 105
  • 158
  • Accepted as it is a valid alternative to my approach although it is not the answer I've been hoping for. – Taig Aug 24 '13 at 17:10
  • This helped me figure something out, but I really do wish it was possible to do something like `model.copy(field:T = _)` and get back a `T => model` – EdgeCaseBerg Oct 28 '16 at 15:33