Type definition and overriding - scala

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.

Related

How to return subtype of a generic trait in scala?

I am trying to create a factory pattern, and the return type is a parametrized trait, and the actual return type will be a subtype of that trait, but I do not know the generic type beforehand, so I don't know how to specify the return type. A simplified version is like this:
trait Mapper[T] {
def createMap(a: T, b: T): T
}
class MapperA extends Mapper[String]{
override def createMap(a: String, b: String): String = {
a + b
}
}
class MapperB extends Mapper[Int]{
override def createMap(a: Int, b: Int): Int = {
a + b + b + b
}
}
def factory(typeOfMapper: String): Mapper = {
typeOfMapper match {
case "mapperA" => new MapperA()
case "mapperB" => new MapperB()
}
}
val mapper = factory("mapperA")
This will give me and error of
trait Mapper takes type parameters
but I do not know the generic type beforehand. What should the return type be for the factory method here?
Well, you could return Mapper[_] ... that will compile, but isn't really very useful, as pointed out in the comments: you won't be able to use the returned value in any meaningful way, because actual type of create will be unknown.
If different instances of your mapper will always have different type parameters (at least, within the same scope), then a neat solution would be using implicits:
object Mapper {
implicit def strMapper: Mapper[String] = new MapperA
implicit def intMapper: Mapper[Int] = new MapperB
}
Then everywhere you have these visible, you can just do:
val m1: Mapper[String] = implicitly[Mapper[String]]
val m2: Mapper[Int] = implicitly[Mapper[Int]]
You could also write your factory function (though, I am not sure why you'd want to) like this:
def mapperFactory[T: Mapper] = implicitly[Mapper[T]]
and use it like this:
val m: Mapper[String] = mapperFactory
or like this
def intMapper = mapperFactory[Int]
If you want different mappers for the same type parameter, it's basically the same idea, except it does't look as neat without implicits. The key is different factories for different types:
class Mapper {
def str(`type`: String): Mapper[String] = `type` match {
case "foo" => FooMapper()
case "bar" => BarMapper()
}
def int(`type`: String): Mapper[Int] = `type` match {
case "baz" => BazMapper()
case "bak" => BakMapper()
}
}
val fooMapper = Mapper.str("foo")
val bakMapper = Mapper.int("bak")

Overriding a trait method with a function value

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 _

Why does the use of typed variable make this code work

This works
trait SomeTrait {
type T
def write2( s: String): T
}
case class C() extends SomeTrait {
type T = String
override def write2(s:String): T = s }
But this does not
trait SomeTrait {
def write2[T]( s: String): T
}
case class C() extends SomeTrait {
override def write2(s: String): String =s }
To my reasoning, they ..seem similar. Why specifically does the compiler give a "method does not override anything" error? IS there a way of making this work?
If you use this definition:
trait T {
def write2[T]( s: String): T
}
Consider some client using this trait. For example:
def doSomething(t: T): Unit = {
val s: String = t.write2[String]("hello")
val n: Int = t.write2[Int]("world")
val d: Double = t.write2[Double]("!")
println(s + n.toString + d.toString)
}
I don't know what the values of s, n, and d would be, but in theory that would be a perfectly valid usage of the trait, from the compiler's perspective. So in order to truly override that write2[T] method, you would have to provide valid behavior for all possible types T.
Compare that to:
trait T2 {
type T
def write2( s: String): T
}
Or even:
trait T3[T] {
def write2(s: String): T
}
Then when callers use it:
def doSomething(t: T2): Unit = {
val x = t.write2("hello") // always returns type t.T
}
def doSomething[T](t: T3[T]): Unit = {
val x = t.write2("hello") // always returns type T
}
There's only one possible type that can be returned, once you have a specific instance of that trait. So to override that method, you need only override the behavior for that one type.
Your trait definition in the second snippet does not mean that implementations of it must have a method write2 for some type T, but that it must have a method write2 which takes a type parameter T.
That means that, if you have a value v: SomeTrait, you should be able to do
val stringed: String = v.method2[String]("foo")
val inted: Int = v.method2[Int]("foo")
This compiles:
trait T {
def write2[T]( s: String): T
}
case class C() extends T {
override def write2[T](s: String): T =s.asInstanceOf[T]
}
write2 returns a T, not a String (hence the reason why your second override does not work and the necessity in the code above for the ugly cast)

How to specify a shapeless singleton type ahead of time

I'd like to construct a type like LimitedString[Limit] where Limit is a type representation of the maximum length of the string.
It would work along the lines of
class LimitedString[Limit] [private](val s: String)
object LimitedString {
private def getLimit[Limit]: Int = ??? // turn `Limit` type into a value
def truncate[Limit](s: String) = new LimitedString[Limit](s take getLimit)
def get[Limit](s: String) =
if(s.length < getLimit) Some(new LimitedString[Limit](s))
else None
}
type Str100 = LimitedString[100] // obviously this won't work
def someLibraryMethod(s: Str100) = { ... }
What I can't figure out is how to actually type (as in keyboard) the type (as in compilation) for Limit.
I started looking into Shapeless's singleton types and found that you can say
100.narrow
// res1: Int(100) = 100
But if I try to use Int(100) as the type, I get errors.
val x: Int(100) = 100
// error: ';' expected but '(' found.
Additionally, how would I implement something like def getLimit[Limit]: Int?
I took #TravisBrown's suggestion to look into Shapless's Witness, and came up with this:
class LimitedString[Limit <: Int] private[util](val s: String) extends AnyVal {
override def toString = s
}
class LimitedStringCompanion[Limit <: Int : Witness.Aux]{
def limit: Int = implicitly[Witness.Aux[Limit]].value
def unapply(s: String): Option[LimitedString[Limit]] = {
if(s.length > limit) None else Some(new LimitedString(s))
}
def truncate(s: String): LimitedString[Limit] = new LimitedString(s take limit)
}
Usage:
import shapeless._
object MyLibraryThing {
type Name = LimitedString[Witness.`50`.T]
object Name extends LimitedStringCompanion[Witness.`50`.T]
def rename(id: Int, name: Name) = { ... }
}
The key things that make it work:
Witness.Aux is a typeclass which you can use to get the singleton value back out of the type
The singleton type Witness.`50`.T is actually a subtype of Int
Type aliases make it more convenient to interact with

Overriding curried functions in Scala

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.