Scala Overloading Issue - scala

If I have a java.util.Map[String,Java.util.Map] for the first call, correct overloaded method is called: toScala(java.util.Map[_,_]). But in the mapValues invocation, function toScala(other:Any) is called. How to avoid this?
I would be calling toScala using a java object like Map, List, Int, String. The map may contain another java.util.Map/java.util.List/ String/Int as one of its values. Similarly the list may also contain another java.util.List/ String/ Int as its members
private def toScala(map:java.util.Map[_,_]) : Any = {
map match {
case null => Map.empty
case _ => map.asScala.mapValues(toScala).toMap
}
}
private def toScala(list:java.util.List[_]) : Any = {
list match {
case null => List.empty
case _ => list.asScala.map(toScala).toList
}
}
private def toScala(other:Any) : Any = {
other match {
case null => None
case _ => other
}
}

Maybe this helps you:
private def toScala(v: Any): Any = vmatch {
  case null => throw new NullPointerException("null value encountered while converting Java to Scala")
  case m: java.util.Map[_, _] => m.asScala.mapValues(toScala).toMap
  case m: java.util.List[_] => m.asScala.map(toScala).toMap
  case m: java.util.Set[_] => m.asScala.map(toScala).toMap
  case m => m
}
Or:
private def toScalaOption(v: Any): Any = vmatch {
  case null => None
  case m: java.util.Map[_, _] => m.asScala.mapValues(toScala).toMap
  case m: java.util.List[_] => m.asScala.map(toScala).toMap
  case m: java.util.Set[_] => m.asScala.map(toScala).toMap
  case m => Some(m)
}
Cheers

Related

How to wrap effectful F instead of the concrete type constructor?

I have the following function definition:
private val checkSapHealth: IO[ServerHealth] =
BlazeClientBuilder[IO](global).resource.use { client =>
discovery
.senderAddr
.flatMap { addr =>
client
.get(addr.url |+| addr.health) { res =>
res.status match {
case Ok =>
IO(ServerOnline)
case _ =>
IO(ServerOffline)
}
}
.timeout(2.second)
.recover {
case _: Throwable => ServerOffline
}
}
}
I would like to replace the concrete type IO through F[_] to make it more abstract. The problem here is the line:
IO(ServerOnline)
The question is how to make it to
F(ServerOnline)
Try to use cats.effect.Sync
https://typelevel.org/cats-effect/typeclasses/sync.html
So basically using Sync[IO].delay is equivalent to using IO.apply.
private def checkSapHealth[F[_]: Sync]: F[ServerHealth] = ...
use ConcurrentEffect and add an implicit Applicative of F, thus giving you the option to lift values into the F context
private def checkSapHealth[F[_] : ConcurrentEffect](implicit A: Applicative[F]): F[ServerHealth] =
BlazeClientBuilder[F](global).resource.use { client =>
discovery
.senderAddr
.flatMap { addr =>
client
.get(addr.url |+| addr.health) { res =>
res.status match {
case Ok =>
A.pure(ServerOnline)
case _ =>
A.pure(ServerOffline)
}
}
.timeout(2.second)
.recover {
case _: Throwable => ServerOffline
}
}
}

Convert match expressions so I can combine them using combinators like orElse

I currently have something that looks like:
Class UserLocation {
def handleUser1(user: User): Unit = user.location match {
case ... => ..
case ... => ..
case _ => ..
}
}
Class UserLocation2 {
def handleUser2(user: User): Unit = user.location match {
case ... => ..
case ... => ..
case _ => ..
}
}
Class UserLocation3 {
def handleUser3(user: User): Unit = user.location match {
case ... => ..
case ... => ..
case _ => ..
}
}
How can I convert the above to a partial function so I can do something like:
def handleUser(user: User): Unit = handleUser1(user) orElse handleUser2(user) orElse handleUser3(user)
There are a few problems with the your layout and your goal. For one thing, all the handleUser methods are hidden in their own class so the goal, as stated, is impossible because they aren't all in scope at the same time. They might be pulled into scope by making the classes implicit, but for that each class needs a constructor parameter.
So here's one possible solution achieved by dropping all the individual classes.
case class User(location: String)
val handleUser1: PartialFunction[User,Unit] = {
case User("Bern") => println("there")
case User("NYC") => println("here")
}
val handleUser2: PartialFunction[User,Unit] = {
case User("Spain") => println("there")
case User("USA") => println("here")
}
val handleUser3: PartialFunction[User,Unit] = {
case User("moon") => println("far")
case User("earth") => println("near")
case User(_) => println("unknown") // the only default
}
val handleUser = handleUser1 orElse handleUser2 orElse handleUser3
handleUser(User("Bern")) // "there"
handleUser(User("moon")) // "far"
handleUser(User("Boon")) // "unknown"
You can define the functions as PartialFunctions and then compose them using orElse:
val f :PartialFunction[Int, Int] = {
case 1 => 1
}
val f2 :PartialFunction[Int, Int] = {
case 2 => 2
}
val f3 :PartialFunction[Int, Int] = {
case 3 => 3
}
def g = f orElse f2 orElse f3
g(1) // 1
g(2) // 2
g(3) // 3
g(4) // scala.MatchError
P.S
#Suma - Thanks for the input.

How define the result type of this method?

How do I define the method return type in the following case:
working code
def deleteInstance(model: String, uid: Long) = model match {
case "menu" => Model.all(classOf[Menu]).filter("uid", uid).get().delete()
case "articles" => Model.all(classOf[Articles]).filter("uid", uid).get().delete()
case "news" => Model.all(classOf[News]).filter("uid", uid).get().delete()
case "image" =>Model.all(classOf[Image]).filter("uid", uid).get().delete()
case "files" =>Model.all(classOf[Files]).filter("uid", uid).get().delete()
case _ => false
}
non-working code:
class ModelManager{
def getModel(model: String) = {
model match{
case "menu" => classOf[Menu]
case "articles" => classOf[Articles]
case _ => false
}
def deleteInstance(model:String, uid: Long) = {
Model.all(getModel(model)).filter("uid", uid).get().delete()
}
}
}
Error raised is:
recursive method getModel needs result
type
It looks like you need an Option:
class ModelManager{
def getModel(model: String) = model match {
case "menu" => Some(classOf[Menu])
case "articles" => Some(classOf[Articles])
case _ => None
}
def deleteInstance(model:String, uid: Long) =
getModel(model) map { m =>
Model.all(m).filter("uid", uid).get().delete()
} getOrElse false
}
You can think of an Option as a container that can hold at most one element. The Option that holds an element x is Some(x). The empty Option is None. Option has several useful methods, including the map and getOrElse methods used above.
The map method applies a function to each element of the "container". Of course, if the container is None, it does nothing (except perhaps to change the static type of the Option). In your case (assuming delete returns a Boolean), the map method will change the Option[Class] into an Option[Boolean].
The getOrElse method returns the element of the option, if there is one, and otherwise returns a default value (false in this case).
Note that you can also simplify your implementation by using the condOpt method defined in PartialFunction:
class ModelManager{
def getModel(model: String) = condOpt(model) {
case "menu" => classOf[Menu]
case "articles" => classOf[Articles]
}
def deleteInstance(model:String, uid: Long) =
getModel(model) map { m =>
Model.all(m).filter("uid", uid).get().delete()
} getOrElse false
}
It looks like getModel will return a Class sometimes, a Boolean others. In Scala, this would typically be modeled using the Either class:
def getModel(model: String) = {
model match{
case "menu" => Left(classOf[Menu])
case "articles" => Left(classOf[Articles])
case _ => Right(false)
}
Left and Right represent the two possible choices of an Either. Callers of this method will need to inspect the return value (probably by using pattern matching as well) to decide if the method returned a Class or Boolean.
It seems you didn't close with parens in the right place. Did you mean this?
class ModelManager{
def getModel(model: String) = {
model match{
// snip
}
} // end method here
def deleteInstance(model:String, uid: Long) = {
Model.all(getModel(model)).filter("uid", uid).get().delete()
}
}
It does not look like you're trying to define a recursive method... Then you're likely to have other issues to resolve as you need a method that returns Class[_] not a combination of Boolean and Class[_] (which would be Any). So may be this would work better?
def getModel(model: String): Class[_] = {
model match{
case "menu" => classOf[Menu]
case "articles" => classOf[Articles]
} // end method here

How to pattern-match Class[X] for different X?

I want to check the type of the parameters of a method, but I don't know the best way to do this. See my code:
class X {
def x(a: Int, b: String) {}
}
val methods = classOf[X].getDeclaredMethods
methods map { m =>
m.getParameterTypes.toList map { t =>
println(t.getName)
// I don't know how to write the following
if ( the type of t is Int) { do something}
else if( the type of t is String ) { do something}
else { }
}
}
Please note the comment in the code. I don't know how to check the types in scala way.
I've tried:
t match {
case _:String => println("### is a string")
case _:Int => println("### is an int")
case _ => println("### ?")
}
But it can't be compiled.
I can use java-way to check:
if (t.isAssignableFrom(classOf[String])) // do something
else if(t.isAssignableFrom(classOf[Int])) // do something
else {}
It seems we should use it in scala, right?
UPDATE:
If I want to use match, I should write like this:
t match {
case i if i.isAssignableFrom(classOf[Int]) => println("### is an Int")
case s if s.isAssignableFrom(classOf[String]) => println("### is a String")
case _ => println("###?")
}
Is it the best answer?
I could make it work with t as a type by defining the cases as constants. It wouldn't compile with the class literals as the case expression. Try:
val S = classOf[String]
val I = classOf[Int]
t match {
case S => println("### is a string")
case I => println("### is an int")
case _ => println("### ?")
}
You can use ClassManifest.fromClass to correctly handle the coercion of primitives to AnyVals, and any other such troubles you might have encountering boxed vs unboxed types when getting funky with reflection.
Like this:
import reflect.ClassManifest
class Wibble { def method(a:Int, b: String) = () }
for(method <- classOf[Wibble].getDeclaredMethods; paramType <- method.getParameterTypes) {
ClassManifest.fromClass(paramType) match {
case m if m <:< ClassManifest.Int => println("Interiffic")
case m if m <:< ClassManifest.Float => println("Floaty, like butterflies")
case m if m <:< ClassManifest.Double => println("Or Quits...")
//todo: all the other AnyVal types...
case m if m <:< classManifest[String] => println("bleeding edge physics, yeah baby!")
//...and a default condition
}
}

Can I use a class variable in a Scala match statement?

Say I have something like this:
obj match {
case objTypeOne : TypeOne => Some(objTypeOne)
case objTypeTwo : TypeTwo => Some(objTypeTwo)
case _ => None
}
Now I want to generalise, to pass in one of the types to match:
obj match {
case objTypeOne : clazz => Some(objTypeOne)
case objTypeTwo : TypeTwo => Some(objTypeTwo)
case _ => None
}
But this isn't allowed, I think for syntactic rather than semantic reasons (although I guess also that even though the clazz is a Class[C] the type is erased and so the type of the Option will be lost).
I ended up with:
if(clazzOne.isAssignableFrom(obj.getClass)) Some(clazz.cast(obj))
if(obj.isInstanceOf[TypeTwo]) Some(obj.asInstanceOf[TypeTwo])
None
I just wondered if there was a nicer way.
You could define an extractor to match your object:
class IsClass[T: Manifest] {
def unapply(any: Any): Option[T] = {
if (implicitly[Manifest[T]].erasure.isInstance(any)) {
Some(any.asInstanceOf[T])
} else {
None
}
}
}
So let's test it:
class Base { def baseMethod = () }
class Derived extends Base
val IsBase = new IsClass[Base]
def test(a:Any) = a match {
case IsBase(b) =>
println("base")
b.baseMethod
case _ => println("?")
}
test(new Base)
test(1)
You will have to define a val for your extractor, you can't inline IsBase, for example. Otherwise it would be interpreted as an extractor.
You could use pattern guards to achieve that. Try something like this:
obj match {
case objTypeTwo : TypeTwo => Some(objTypeTwo)
case objTypeOne if clazz.isAssignableFrom(objTypeOne.getClass) => Some(clazz.cast(objTypeOne))
case _ => None
}
You can use a local type alias for that:
def matcher[T](obj: Any)(implicit man: Manifest[T]) = {
val instance = man.erasure.newInstance.asInstanceOf[AnyRef]
type T = instance.type // type alias
obj match {
case objTypeOne : T => "a"
case objTypeTwo : TypeTwo => "b"
case _ => "c"
}
}
scala> matcher[TypeOne](TypeOne())
res108: java.lang.String = a
scala> matcher[TypeTwo](TypeOne())
res109: java.lang.String = c
UPDATE: Aaron Novstrup has pointed out that singleton type will only work if man.erasure.newInstance==obj (see §3.2.1 of the spec)