I'm having difficulty understanding how curried functions (with one argument) differ from normal methods. I tried to implement the latter with the former, but wasn't able to.
I have Market trait defined as follows:
package market {
trait Market {
def getBuyRate(currency: String): Double
def getSellRate(currency: String): Double
}
}
I have another FakeMarket trait that extends Market where I wanted to use currying to implement getBuyRate and getSellRate, as follows:
package market {
trait FakeMarket extends Market with Iterable[Double] {
def getRate(factor: Double)(currency: String): Double = {
factor * this.iterator.next()
}
def getBuyRate = getRate(1) _
def getSellRate = getRate(1.02) _
}
}
Finally, I have RandomFakeMarket object that extends FakeMarket:
package market {
object RandomFakeMarket extends FakeMarket {
def iterator = new Iterator[Double] {
def hasNext = true
def next = 100.0
}
}
}
Having the types defined as such gives an error saying:
<console>:10: error: object creation impossible, since:
it has 2 unimplemented members.
/** As seen from object RandomFakeMarket, the missing signatures are as follows.
* For convenience, these are usable as stub implementations.
*/
def getBuyRate(currency: String): Double = ???
def getSellRate(currency: String): Double = ???
object RandomFakeMarket extends FakeMarket {
^
This seems odd to me because FakeMarket implements methods called getBuyRate and getSellRate of String => Double type.
I can get this to work if in Market I had done:
getBuyRate: String => Double
Why is this different from how I originally defined getBuyRate? Why should this be different when there's only one argument? Now it seems that the parent trait Market has to worry about how getBuyRate gets implemented (a normal function vs a curried function).
It's not so much related to currying. def method(p1: String, p2: String): String is not equivalent to def method: (String, String) => String in scala. The difference between methods and functions is that methods support inheritance, so they need to know the method's input and output, which is not so obvious because of ambiguity:
def get: String => Double may be both String -> Double (iterpreted as unified member) and () -> String -> Double (interpreted as function). But in scala it's Function1[String, Double] or () Function1[String, Double], which are simmilar in the meaning that there is no input. Unfortunatelly, def get(): String => Double makes no difference here (see examples below).
In other words, method may not have input parameters in scala (as it's may not be a pure function because of side effects), so it can't infer (String)Double from String => Double so your whole function becomes an output parameter. For example you could do override def method: (String, String) => String in some subclass - this will override method without parameters and with return type (String, String) => String, but not override the method with input (String, String) and return type String.
Another problem is that methods have different types than functions and may be only one-way converted by eta-expansion, so scala is a little bit incompatible with UAP :
scala> def aaa(b: String): String = "aaa"
aaa: (b: String)String
scala> aaa _
res4: String => String = <function1>
scala> def aaa: String => String = (a: String) => "aa"
aaa: String => String
scala> def aaa()(b: String): String = "aaa"
aaa: ()(b: String)String
scala> def aaa(): String => String = (a: String) => "aa"
aaa: ()String => String
A bit more about methods vs functions: Difference between method and function in Scala
Related
Say I have a trait like this
trait X {
def foo(param: String): Int
}
and I want to override it with a function value, I have to do this:
class XImpl extends X {
private val fooF: String => Int = ???
override def foo(param: String): Int = fooF(param)
}
because the following does not work
class XImpl extends X {
override val foo: String => Int = ???
}
/*
error: class XImpl needs to be abstract. Missing implementation for:
def foo(param: String): Int // inherited from trait X
^
error: value foo overrides nothing.
Note: the super classes of class XImpl contain the following, non final members named foo:
def foo(param: String): Int
*/
Is it possible to directly implement/override the trait method with the function value in some way and avoid the forwarder?
The simple answer is that you cannot override a method with a function value because they are not the same type.
A method must be called on a particular instance of a class (even if the method itself does not use any fields in that class).
A function value stands alone and can be called without any additional information.
A method can be converted a function value using eta expansion x.foo _. This creates a function value which has the class value embedded within it.
val x: X = ???
val etaFoo: String => Int = x.foo _
In this case Scala will actually do the eta expansion for you so the _ is optional, but it is required in more complex uses and is good practice to keep it there so show that eta expansion is happening.
To convert a function value to a method you need to define a method as shown in the question.
Eta expansion works with methods with multiple parameters:
trait X2 {
def foo2(p1: String, p2: Int): String
}
val x2: X2 = ???
val etaFoo2: (String, Int) => String = x2.foo2 _
I am looking for a Generic functional way to convert between Scala String to any numeric type. I need in case of failure to pass a default value.
For example, I need to convert from String to Int but in case the String to Int conversion failed. I need to pass a default value without having throws java.lang.NumberFormatException. I tried this way but doesn't get my idea as I need it generic and also with default value in case of exception
Edit: I updated the solution to parse from any type to any type. This makes the solution more generic based on the question requested. I think you can use Scala functional way to have the generic type [T] but you need to split it into two parts.
First to implement parse types which parses from any type [U] to
any type [T]. parseTypes takes a function canBuildFrom as a parameter using Scala functional way. Then based on the output of this function you will checks if it parsed correctly or it has an exception. Also, in case it failed to parse you can pass a default parameter.
def parseTypes[T,U](str: U, canBuildFrom: U ⇒ T): Either[java.lang.NumberFormatException, T] =
Try(canBuildFrom(str)).map(Right(_)).getOrElse {
Left(new java.lang.NumberFormatException(str.toString))
}
def safeParse[T,U](attributeValue: U, canBuildFrom: U ⇒ T, defaultValue: T): T = {
parseTypes(attributeValue, canBuildFrom) match {
case Right(x) ⇒ x
case Left(x) ⇒ defaultValue
case _ ⇒ defaultValue
}
}
def safeParseDoubleToBigDecimal(attributeValue: Double): BigDecimal = safeParse[BigDecimal,Double](attributeValue, toBigDecimal, 0.0)
You can use it to parse String to Int, Double, and Decimal as following:
def safeParseStringToInt(attributeValue: String): Int = safeParse[Int,String](attributeValue, _.toInt, 0)
def safeParseStringToDouble(attributeValue: String): Double = safeParse[Double ,String](attributeValue, _.toDouble, 0.0)
def safeParseStringToBigDecimal(attributeValue: String): BigDecimal = safeParse[BigDecimal ,String](attributeValue, BigDecimal(_), 0.0)
// example of usage
val x:Int = safeParseStringToInt("123",0)
val y:Int = safeParseStringToInt("aaa",0)
Update: I update this answer as I realized that #Dima's answer is somehow more functional and better than my answer I added the answer below copied from #Dima's answer as my answer marked as the correct answer.
trait ParseIt[T] {
protected def parse(s: String): T
def apply(s: String) = Try(parse(s)).toOption
}
implicit object ParseInt extends ParseIt[Int] {
protected def parse(s: String) = s.toInt
}
implicit object ParseDouble extends ParseIt[Double] {
protected def parse(s: String) = s.toDouble
}
// etc ...
def parse[T : ParseIt](s: String, orElse: => T) =
implicitly[ParseIt[T]](s).getOrElse(orElse)
val n: Int = parse("123", 0)
val d: Double = parse("123", 0.0)
This sort of thing is implemented really nice with typeclasses:
trait ParseIt[T] {
protected def parse(s: String): T
def apply(s: String) = Try(parse(s)).toOption
}
implicit object ParseInt extends ParseIt[Int] {
protected def parse(s: String) = s.toInt
}
implicit object ParseDouble extends ParseIt[Double] {
protected def parse(s: String) = s.toDouble
}
// etc ...
def parse[T : ParseIt](s: String, orElse: => T) =
implicitly[ParseIt[T]](s).getOrElse(orElse)
val n: Int = parse("123", 0)
val d: Double = parse("123", 0.0)
I'm quite new to Scala and a part of my project ended up with the following design.
trait StringService {
def length(s: String): Int
def vowels(s: String): Int
}
Those methods are used as parameters to
def processStrings(operation: String => Int) = {...}
Calls like processStrings(ss.length) work fine. Now I would like to abstract
the type of these methods
type StringFuction = String => Int
The problem is that an implementation like
override def length(s: String): Int = { ... }
is no longer valid. Of course, I can go with
override def length: StringFunction = { (s: String) => ... }
but this seems a little off.
What's the proper way? Please feel free to suggest a different design if this isn't a true Scala approach.
Well, there are different ways to do this, depending on the purpose you want to use your type alias for. This works for example:
type StringFunction = String => Int
trait Foo { def foo(s: String) = ??? }
class Bar extends Foo {
def foo(s: String) = s.length
}
def foobar(f: StringFunction)(s) = f(s)
foobar(new Bar().foo)("bar")
This works too:
type StringFunction = String => Int
trait Foo { def foo: StringFunction = ??? }
class Bar extends Foo {
override def foo = _.length
}
def foobar(f: StringFunction)(s) = f(s)
foobar(new Bar().foo)("bar")
Strictly speaking, these are not the same constructs: in the first case Foo.foo is a method, that takes an String argument and returns an Int.
In the second case, it is a method, that takes no arguments, and returns a lambda function, that take a String argument, and returns an Int.
This difference is fairly subtle though. It does matter for some specific cases, but often can be ignored.
I was under the impression that this
// short syntax
def foo(bar: Bar)(baz: Baz): Quux
was syntax sugar for this
// long syntax
def foo(bar: Bar): (Baz) => Quux
But I cannot seem to mix the two when it comes to inheritance. The whole tree has to be defined in either the short syntax or the long syntax; never both.
For example:
case class Context
case class Work
trait ContextualWorker {
def workWithContext(ctxt: Context)(work: Work): Traversable[Work]
}
class ShortConcreteWorker extends ContextualWorker {
override def workWithContext(ctxt: Context)(work: Work) = Nil
}
class LongConcreteWorker extends ContextualWorker {
// error on next line: method workWithContext overrides nothing <-------------
override def workWithContext(ctxt: Context): (Work) => Traversable[Work] = {
val setupCode = 1
{ work => Nil }
}
}
If I change the trait to use the long syntax, then ShortConcreteWorker doesn't compile.
Is there a reason why these aren't interchangeable/inheritable? How have you gotten around it?
Right now the most flexible approach appears to be to define the tree in the long syntax, perhaps delegating to an implementation class in ShortConcreteWorker like so:
case class Context
case class Work
trait ContextualWorker {
def workWithContext(ctxt: Context): (Work) => Traversable[Work]
}
class ShortConcreteWorker extends ContextualWorker {
override def workWithContext(ctxt: Context) = workWithContextImpl(ctxt)_
private def workWithContextImpl(ctxt: Context)(work: Work) = Nil
}
class LongConcreteWorker extends ContextualWorker {
override def workWithContext(ctxt: Context): (Work) => Traversable[Work] = {
val setupCode = 1
{ work => Nil }
}
}
The two methods described quite simply have different signatures. The REPL confirms this:
scala> def foo1(a: Int)(b: Int): Int = a + b
foo1: (a: Int)(b: Int)Int
scala> def foo2(a: Int): (Int => Int) = (b: Int) => a + b
foo2: (a: Int)Int => Int
The first is a function that requires two arguments, given in separate argument lists, and returns an Int. The second is a function that takes one argument and returns a function from Int to Int. While these two things are conceptually similar, they are, in fact, different constructs, and Scala treats them as such.
This is not limited to functions with multiple argument lists. It works the same way here:
scala> def foo3(a: Int): Int = a + 1
foo3: (a: Int)Int
scala> def foo4: (Int => Int) = (a: Int) => a + 1
foo4: Int => Int
Note that there are different ramifications for usage as well. With foo2, because it only accepts one argument, we can call it with just one argument. However, foo1 requires two arguments, an so we cannot simply call it with one. You can however use the _ syntax to convert it into a callable function.
foo2(2) // Int => Int = <function1>
foo1(2) // error: missing arguments for method foo1
foo1(2) _ // Int => Int = <function1>
So to answer your question directly: The reason they are not interchangeable is because they are not the same. If they were the same, we would be able to call them the same way. If you could change the signature upon extension, how would Scala know which calling syntax to allow? The way to "get around" this is to simply make the signatures consistent.
I want to do this:
abstract class Context {
def getInt(id: Int): Int
}
abstract class Dependency[+T]
(val name: String, val id: Int)
extends Function1[Context,T]
class IntDependency(name: String, id: Int)
extends Dependency[Int](name, id) {
def apply(implicit context: Context): Int =
context.getInt(id)
}
But then I get an error message like this:
class IntDependency needs to be abstract, since method apply in trait
Function1 of type (v1: Context)Long is not defined (Note that T1 does
not match Context)
I understand that implicits should normally be part of the second parameter list, but I can't work out how to code it so it compiles, and gives the result I want.
Explanation: I'm trying to create a framework where one can define "Function" object, which can depend on other functions to compute their value. All functions should only take a single Context parameter. The context know the "result" of the other functions. The function instances should be immutable, with the state residing in the context. I want the functions to create "dependency" fields at creation time, which take the context implicitly, and return the value of the dependency within that context, so that accessing the dependency inside of the apply method "feels like" accessing a parameter or field, that is without explicitly giving the context as parameter to the dependency.
Are you sure you need your Dependency to extend a Function? Because if you don't, just leave the extends Function1[Context,T] part out and your code will work.
If you really need to extend a Function than I don't know of a solution in your case. But there are cases where you could try to overload the apply method. Like here:
scala> val sum = new Function1[Int, Function1[Int, Int]] {
| def apply(a: Int) = (b: Int) => a + b
| def apply(a: Int)(implicit b: Int) = a + b
|}
sum: java.lang.Object with (Int) => (Int) => Int{def apply(a:Int)(implicit b: Int): Int} = <function1>
scala> sum(2)(3)
res0: Int = 5
scala> implicit val b = 10
b: Int = 10
scala> sum(2)
res1: Int = 12
A method may have its final parameter section marked implicit; it need not be the second section, although that is most commonly seen.
But it seems that when a subclass marks a parameter section implicit, it is no longer considered to override the method in the superclass.
scala> new (Int => Int) { def apply(implicit i: Int) = i }
<console>:8: error: object creation impossible, since method apply in trait Function1 of type (v1: Int)Int is not defined
(Note that T1 does not match Int)
new (Int => Int) { def apply(implicit i: Int) = i }
^
scala> trait F1 { def f(a: Any) }; new F1 { def f(implicit a: Any) = () }
<console>:8: error: object creation impossible, since method f in trait F1 of type (a: Any)Unit is not defined
trait F1 { def f(a: Any) }; new F1 { def f(implicit a: Any) = () }
^
The spec does not specifically mention this (§5.1.4 Overriding), so it may be an implementation restriction, or an bug.
Its sure, that your apply method signature with implicit doesn´t conform with the signature of Function1.apply.
Hopefully I get your problem right, so what about (assuming that your context is mutable and perhaps singleton) having the implicit context injected at creation time? Is that possible in your case?
class IntDependency(id: Int)(implicit context: Context) extends Dependency[Int](id)
But then I wonder (and still was wondering before) what to do with the context argument at the apply method.
Here is the working solution:
abstract class Context {
def getInt(id: Int): Int
}
abstract class Dependency[+T]
(val name: String, val id: Int) {
def get(context: Context): T
}
class IntDependency(name: String, id: Int)
extends Dependency[Int](name, id) {
def get(context: Context): Int =
context.getInt(id)
}
implicit def intDep2Int(dep: IntDependency)
(implicit context: Context): Int =
dep.get(context)