scalajs facade unable to access native js method - scala.js

I'm trying to use the facade for createjs https://github.com/scalawarrior/scalajs-createjs
In createjs, display objects have a 'clone' method that is used for making quick duplicates of sprites and bitmaps. But I get a compiler error when I try to call this method from scalajs, it says
Error:(30, 42) method clone in class Object cannot be accessed in
com.darkoverlordofdata.entitas.Entity
Access to protected method clone not permitted because
prefix type com.darkoverlordofdata.entitas.Entity does not conform to
class CreateAliensSystem in package systems where the access take place
val sprite = invader.clone()//.asInstanceOf[Sprite]
^
I understand that Object.clone is protected in java, but this is on a native javascript object.
So - How can I access the clone method on a native js object?

You have to redefine it in your facade type.
override def clone(): SomeType = js.native

Related

Usage of clone method in Chisel IO interface constructors

Several IO interface constructors of the Sodor processor collection implements its own clone method. I looked into the usage of clone method in Scala but still cannot figure out why exactly that is done. (I could not find any explicit usage of these methods anywhere in the design)
Sodor is still currently still on Chisel 2. clone was renamed to cloneType in Chisel 3 to distinguish it from clone in Java and Scala. cloneType is generally required by Chisel in order to instantiate new instances of parameterized Bundles. For example:
class MyBundle extends Bundle {
val foo = UInt(32.W)
}
class MyParameterizedBundle(width: Int) extends Bundle {
val bar = UInt(width.W)
}
Chisel often needs to create an instance of a given Bundle class from another instance of that class. Chisel uses Java reflection to do this. If there are no arguments to the constructor then it can just instantiate the object from the default constructor. However, it cannot determine the value of width from an instance of MyParameterizedBundle via reflection, so it cannot provide an appropriate parameter to the constructor. This is what the cloneType function is for. It tells Chisel how to create a new instance of a Bundle from a given object.
We hope to fix this wart in the future, but have not had the time to implement it. The most promising way is to automatically generate cloneType via Scala macro annotations.

Using implicit conversion instead Adapter pattern

I have a project written in MVC style. Views look like this:
trait BaseView {
def asComponent(): Component // each view can be displayed on screen
}
class ConcreteView extends Panel with BaseView {
def asComponent(): Component = this //ConcreteView is itself Component because it extends Panel
}
It is possible to change this code to use implicit conversion from ConcreteView to Component? So I can use ConcreteView as Component (due implicit conversion) without calling ConcreteView#asComponent method?
Yes, it is possible. Just define an implicit conversion from BaseView to Component that calls the asComponent method.
object BaseView {
implicit def viewIsComponent(x:BaseView) : Component = x.asComponent
}
But that does not mean that it is a good idea. An implicit conversion in scala is a very powerful feature. If a BaseView (and by inheritance each XXXView) is a Component, that means that you will get all methods of Component when you want to call a method of val myView:SomeView. That totally clutters the namespace and also can be dangerous because you are not sure if you call a method of your View or of the Component it is implicitly mapped to.
In the scala library there has been a move away from implicit conversions to more explicit and slightly more verbose way. Take for example JavaConversions: they provide implicit conversions from scala collections to java collections and back. This sounds like a good idea, but it has caused a lot of trouble in practice:
conversions happening when you don't expect them to
the namespace of the scala collections cluttered with a lot of additional methods from the java equivalent
difficult to find issues when new methods are added to the target of the implicit conversion that collide with methods in the source of the conversion
The currently recommended way to deal with java/scala collection interop is to use the more explicit JavaConverters, which add a single method asScala to java collections and asJava to scala collections.
So just leave the method as is. Maybe change the name to just .component, since you don't really convert the view to the component, but only allow somebody to access the component that each view must have.

Is it possible in Play 2.1 to convert a JsValue to a case class instance if you only know the name of the case class at the runtime?

I'm building a JSON RPC in Play 2.1. In order to call the proper methods the RPC dispatcher is using reflection to create and call a class method instance by name.
Right now a RPC method looks like this:
def create(obj: JsValue) = {
val menu: Menu = Json.fromJson[Menu](obj).get
collection.insert(menu).map( r => toDirectResult(r))
}
def createCustom(obj: JsValue) = {
val menu: Menu = Json.fromJson(obj)(Menu.customFormat).get
collection.insert(menu).map( r => toDirectResult(r))
}
What I would like to do is to be able to define the RPC methods like this:
def create(menu: Menu) = {
collection.insert(menu).map( r => toDirectResult(r))
}
The problem is that the RPC dispatcher only knows at the runtime that is has to call the method named "create" on the class named "Menus" and it has the value of the argument to pass to the method as a JsValue. Through reflection I can find out the number of arguments and their types for the RPC method. When the argument type is a case class, how do I transform the JsValue into a case class instance using the implicit Formatter (or Reader) defined in the companion object of the case class?
For the createCustom method I realize that there is no "magic" solution, but since I started learning Scala I discovered that few things are truly impossible with this programming language. Would it be possible to use an annotation or something similar to specify a Formatter that is not implicit?
You need to implement a PathBinder...this should help out http://www.richard-foy.fr/blog/2012/04/09/how-to-implement-a-custom-pathbindable-with-play-2/
After further careful consideration I've decided that reflection is really no the right solution for my problem. It lacks type safety and proper error reporting at compile time, it is harder to debug and has an impact on the performance as well. And I actually have all the information I need to generate the code at build time.
Unfortunately I cannot use the Play router because for the JSON RPC dispatcher the routing depends on the request body, which is not available during the Play routing. But in essence the RPC dispatcher is doing the same thing as the Play router. So for the moment I'm just going to manually code my RPC routes and then the problem in the question is solved. In the future I'm planning to write a SBT plugin that will automatically generate the dispatcher code, similar to the Play router.

Dynamic Proxy using Scalas new Dynamic Type

Is it possible to create an AOP like interceptor using Scalas new Dynamic Type feature? For example: Would it be possible to create a generic stopwatch interceptor that could be mixed in with arbitrary types to profile my code? Or would I still have to use AspectJ?
I'm pretty sure Dynamic is only used when the object you're selecting on doesn't already have what you're selecting:
From the nightly scaladoc:
Instances x of this trait allow calls x.meth(args) for arbitrary method names meth and argument lists args. If a call is not natively supported by x, it is rewritten to x.invokeDynamic("meth", args)
Note that since the documentation was written, the method has been renamed applyDynamic.
No.
In order for a dynamic object to be supplied as a parameter, it'll need to have the expected type - which means inheriting from the class you want to proxy, or from the appropriate superclass / interface.
As soon as you do this, it'll have the relevant methods statically provided, so applyDynamic would never be considered.
I think your odds are bad. Scala will call applyDynamic only if there is no static match on the method call:
class Slow {
def doStuff = //slow stuff
}
var slow = new Slow with DynamicTimer
slow.doStuff
In the example above, scalac won't call applyDynamic because it statically resolved your call to doStuff. It will only fall through to applyDynamic if the method you are calling matches none of the names of methods on the type.

Type aliasing Java classes with statics

Suppose MyClass is a class defined in Java, and has many static as well as non-static members. I tried to alias this class (and associated companion object) in a Scala object MyObject as shown below:
object MyObject {
import javastuff._
type MyAlias = MyClass
val MyAlias = MyClass
}
Scalac complains:
error: object MyClass is not a value
val MyAlias = MyClass
How do I work around this? Thanks.
Although this works in pure Scala for a class + companion object, it's not possible with Java's static methods, as these don't belong to any interface.
Scala could, in theory, create an object containing delegates to all the static methods of some class, but it doesn't do this currently. It's also possible to write a compiler plugin for this if you feel comfortable writing plugins.
Failing that, you'll either have to create an object full of delegates yourself, or just cherry-pick a few methods and pass them around as functions.
it's not possible with Java's static methods, as these don't belong to any interface.
Update 5 years later: PR 5131 mentions:
We used to disable generation of static forwarders when a object had a
trait as a companion, as one could not add methods with bodies to an
interface in JVM 6.
The JVM lifted this restriction to support default methods in interfaces,
so we can lift the restriction on static forwarders, too.
Fixes scala-dev issue 59
See commit 41c9a17 by Jason Zaugg (retronym).