I have created empty instance of my object and then initialise it using run time values. Implementation was based on scala.concurrent.util.Unsafe in Scala 2.11 and it worked fine.
I understand Unsafe is bad and hence has been deprecated in Scala 2.12.
If it's deprecated then what's equivalent of Unsafe in Scala 2.12?
Assuming you're running on a JVM where sun.misc.Unsafe is still available (this will limit which JVMs you can run on, but so did using scala.concurrent.util.Unsafe so no immediate loss):
val unsafeInstance = // use in place of Scala 2.11 usages of scala.concurrent.util.Unsafe.instance
classOf[sun.misc.Unsafe]
.getDeclaredFields
.filter(_.getType == classOf[sun.misc.Unsafe])
.headOption
.map { field =>
field.setAccessible(true)
field.get(null).asInstanceOf[sun.misc.Unsafe]
}
.getOrElse { throw new IllegalStateException("Can't find instance of sun.misc.Unsafe") }
Code is very slightly adapted from the Scala 2.11 source.
It's possible that this is an instance of spending so much time thinking about "could" that one didn't think about "should".
Related
I was upgrading my project from scala 2.11 to scala 2.12.
For DB Interaction slick-extentions was used, but I found out that slick-extension has been merged with Slick itself since Slick-3.2.0.
While I was going through the docs I found about JdbcProfiles and discontinuation of Drivers etc.
Now, I have a lot of code where withSession method from scala.slick.jdbc.JdbcBackend has been used - like -
db.withSession { implicit session =>
rmobVersionControl.foreach(e =>
elements += new RMOBVersionControlElement(e._1, e._2, e._3))
}
In the docs I see that withSession() method is Deprecated (Since version 3.0).
But I was wondering if there's a way to keep this code in slick 3.2.0 because changing all this code and using Action-Based Api would be a lot of pain.
I recently revived an old library that was written in scala 2.9, and I created a new scala project using scala 2.13.2
I am getting errors like the following:
type mismatch;
found : scala.collection.mutable.Buffer[Any]
[error] required: Seq[Any]
Was there a specific change between 2.9 to 2.13.2 that involved not implicitly casting sequences or something that might solve many of these types of compile errors?
I had to add .toSeq to many of my function return statements that were vals of Buffer[Any] that needed to be passed as an arguement to a function expected a Sequence.
Quite a lot things happened in the last 7+ years (including rewrite of the collections library).
If adding .toSeq solves your problem - just go for it.
If you want to know what exactly has changed - try upgrading version-by version: first upgrade to scala-2.10., then to 2.11., then 2.12.*, then, finally, to 2.13.2.
At each upgrade you'll probably see deprecation warnings. Fix them before upgrading to the next version.
Brave, but perhaps bad form, to disturb the dead. Nevertheless, maybe pass mutable.Buffer as mutable.Seq instead of Seq which is by default immutable.Seq. Consider
val mb = mutable.Buffer(11, Some(42))
val ms: mutable.Seq[Any] = mb // OK
val is: Seq[Any] = mb // NOK
I'm still a bit confused about the scala Shapeless library after reading many articles. It seems that Shapeless uses scala compiling features? So does it use reflection and is it safe for production code?
Shapeless doesn't use reflection, it uses macros to inspect the structure of classes. With Shapeless this inspection happens at compilation time and not runtime (reflection happens at runtime). As a result of this Shapeless can be considered safer than reflection because it will be able to make many checks at compilation time.
Let's try to get a field by name using shapeless
case class MyClass(field: String)
import shapeless._
val myClassLens = lens[MyClass] >> 'field
val res = myClassLens.get(MyClass("value")) // res == "value"
if we use an invalid field name the compiler will complain with a compilation error
On the other hand if we tried to achieve this same thing using reflection the field name would be checked at runtime (maybe in production), that's why reflection is not considered as safe as Shapeless. It will also be way faster with Shapeless than reflection
Transitioning from Play Framework 2.1 to 2.2 (Scala) I was restructuring some code and found some lines of code to totally freeze the SBT build until the process was killed due to java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: GC overhead limit exceeded. Also Eclipse (tried with Juno and Kepler but I doubt it has anything to do with this) froze and it didn't even load the workbench anymore.
So, here's the code. I would love to know what makes the compiler to freeze and not just give an error here.
def foo = Action { implicit request =>
someForm.bindFromRequest.fold(
formWithErrors => Ok,
form => Async { Future.successful(Ok) }
)
}
I solved the issue already, but I'm curious why this just freezes everything. I'm on a Mac running java (1.7.0_40).
Update: Also, I'm using Scala 2.10.2. A coworker of mine can compile this on his PC, but with deprecation warnings on Async.
There are certain expressions within Scala that when you ask the compiler to evaluate them, it will instantiate a TON of type instances trying to figure out the unified difference between two types. Most likely, the type you're returning is NOT what you expect.
I would explicitly annotate the result type:
def foo = Action { implicit request =>
someForm.bindFromRequest.fold[Result](
formWithErrors => Ok,
form => Async { Future.successful(Ok) }
)
}
This should help the type inferencer KNOW what the types are and only check to see if they match, rather than expand infinitely. Also, sounds like it may have been a scala compiler bug.
I am working on a library which depends on Scala 2.9 but only for a minor feature. I would like to propose version compatible with 2.8, but I don't want to maintain two code branch. Since I'm using SBT, I would like to benefits from it cross-compilation features.
However I don't know is there is a way to provide an equivalent of conditional compilation, to include a piece of code only if Scala 2.9 is used. Reflexivity could be an option (but how?).
Edit: The features I am using in 2.9 are the new sys package object.
I got it with reflection. So if I want to get the sys.SystemProperties, I can do:
try {
val k = java.lang.Class.forName("scala.sys.package$")
val m = k.getMethod( "props" )
// etc.
} catch {
case _ => throw new UnsupportedOperationException("Only available with Scala 2.9")
}
But it is so boring and ugly that I think I will drop those features...
Read this blog post, which describes how to do it with metaprogramming:
http://michid.wordpress.com/2008/10/29/meta-programming-with-scala-conditional-compilation-and-loop-unrolling/