IntelliJ IDEA Scala inspection : import play.api.xxx conflict with com.company.play package - scala

I want to make a helper class at the root of my core project using play-json from typesafe, something like
package com.company
import play.api.libs.json.JsValue
object Helper {
implicit class RichJson(json: JsValue) {
def doStuff() //...
}
}
The problem is that I have somewhere else in the project a package com.company.play
package com.company.play
class Foo() { //...
}
In IntelliJ IDEA 2018.2.4 CE, the line import play.api.libs.json.JsValue is in error with telling me "cannot resolve symbol api" and when Ctrl+Click on the play it goes to the folder containing my Foo.scala file
If I compile the solution with sbt outside of IDEA, there is no problem.
If I put the Helper object in a subpackage (eg com.company.common) there is no error (which also means the dependency is correct in my build.sbt)
I don't understand why IDEA miss this, com.company.play isn't even in the dependencies of the core project. I already tried to invalidate cache, and it doesn't help.

The problem is that Intellij gives precedence to the package play into your project, instead of the package coming from the framework, when resolving the import of JsValue inside com.company scope.
If you really want to keep that name for com.company.play, there is a simple workaround using a fully-qualified import, just prefix like this:
import _root_.play.api.libs.json.JsValue

Related

Scala top level package object

Edited this question to be more clear, see comments below for explanation.
So this seems kinda obvious to me, but it doesn't seem like it works that way, but if I have a scala package object and it's in the top level of my packages. Say like com.company and it's something simple like below
package com
package object company{
val something = "Hello world."
}
Now it would seem to me that this variable would trickle down and be accessible from it's child packages, but they aren't.
// 2 Layers down instead of the direct child
package com.company.app
import com.company._
object Model extends App {
println(something)
}
This seems to only work with the import, which is fine, but I was hoping with the package object I could define top level things for the entire package and have it trickle down, but is that not the case? Is there a way for this to work? I appreciate any insight into this.
The code posted in your question works as it is without an import. If you want the definitions of all packet objects above your current package to trickle down, you will have to modify the package statement of the classes in subpackages
Package object
package com
package object company {
val something = "Hello world."
}
Class in a subpackage com.company.model
package com
package company
package model
// package com.company.model would not work here!
object Model extends App {
println(something)
}
This technique is used frequently in the scala library itself. See for example the package statement in s.c.i.HashSet:
package scala
package collection
package immutable

How do I use an unmanaged dependency in this simple Play example?

I am trying to write a Scala Play web service that returns JSON objects and am having trouble calling a function in a dependency. Can someone tell me what I'm doing wrong in this simplified example?
I have a project called SimpleJSONAPI that consists of the following object.
package com.github.wpm.SimpleJSONAPI
import play.api.libs.json.{JsValue, Json}
object SimpleJSONAPI {
def toJson(s: String): JsValue = Json.toJson(Map("value" -> s))
}
Unit tests confirm that given a string it returns a JSON object of the form {"value":"string"}.
I have a separate Play 2.2.3 Scala project that I created by typing play new PlayJSON. I added the following json action to the controller in the generated application.
package controllers
import play.api.mvc._
import com.github.wpm.SimpleJSONAPI._
object Application extends Controller {
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
def json = {
val j = SimpleJSONAPI.toJson("The JSON API")
Action {
Ok(j)
}
}
}
I also added this route.
GET /json controllers.Application.json
In the root of the PlayJSON project I have a lib directory that contains the simplejsonapi_2.11.jar built by SimpleJSONAPI. This appears to contain the correct code.
> jar tf lib/simplejsonapi_2.11.jar
META-INF/MANIFEST.MF
com/
com/github/
com/github/wpm/
com/github/wpm/SimpleJSONAPI/
com/github/wpm/SimpleJSONAPI/SimpleJSONAPI$.class
com/github/wpm/SimpleJSONAPI/SimpleJSONAPI.class
This compiles, but when I try to connect to localhost:9000/json I get the following runtime error in the line with the val j assignment.
java.lang.NoSuchMethodError: scala.Predef$.ArrowAssoc(L/java/lang/Object;)Ljava/lang/Object;
I've also seen the same error in a unit test that exercises the /json route with a FakeRequest.
If I copy the toJson function from the external dependency into the Play application everything works.
As far as I can tell from the documentation I'm doing everything right, and the error message is opaque. Can someone tell me how to get this to work?
I think your import is incorrect given how you are using the API. Either exclude the object name on the import...
import com.github.wpm.SimpleJSONAPI._
Or change your usage to drop the object name...
val j = toJson("The JSON API")
This was a problem with Scala compiler version compatibility. I compiled my SimpleJSONAPI dependency with Scala 2.11, while the Play app was being built with Scala 2.10. When I changed the SimpleJSONAPI dependency to also build with Scala 2.10, I was able to use it in my Play app.
This was confusing because it's not obvious from the project files which version of Scala a Play app is using, and the error message about ArrowAssoc gives no indication that it is a compiler version issue.

dart import and part of directives in same file

I'm writing a dart file:
import 'something.dart'
part of my_lib;
class A{
//...
}
I have tried this with the import and part of directives reversed and it still won't work, can you not have a class file as part of a library and have imports?
All your imports should go in the file that defines the library.
Library:
library my_lib;
import 'something.dart';
part 'a.dart';
class MyLib {
//...
}
a.dart
part of my_lib;
class A {
//...
}
Since a.dart is part of my_lib it will have access to any files that my_lib imports.
The Pixel Elephanr's answer is correct, but I suggest the alternative syntax for the part-of directive:
my_file.dart
(the library main file):
//This now is optional:
//library my_lib;
import 'something.dart';
part 'a.dart';
class MyLib {
//...
}
a.dart
(part of the same library; so in it you can reference the elements imported in my_file.dart)
//Instead of this (whitout quotes, and referencing the library name):
//part of my_lib;
//use this (whit quotes, and referencing the library file path):
part of 'my_file.dart'
class A {
//...
}
In the Doc you can found both the syntax, but only using the part-of's syntax with quotes (pointing to the file path), you can omit the library directive in the library main file; or, if the library directive is still needed for other reasons (to put doc and annotations to library level), at least you won't be forced to keep in sync the library name in multiple files, which is boring in case of refactoring.
If you are facing this in IntelliJ IDEA or Android Studio while moving the files via drag and drop, then switch to 'Project Source' in project pane at left and then move(drag and drop). When I faced this problem while working with flutter, this worked for me.

Using IntelliJ for a Scala Project

I am missing something simple here.
Scala downloaded
Scala home set
IntelliJ plugin downloaded
When new module is added, scala is chosen:
When new class is created, however, when trying to run it, i get
Looking as module properties, i see
What am i missing please?
Two things:
The file you're working with must be a Scala class.
You should have an object declared somewhere (preferably outside of the class).
object runnableObject {
def main(args: Array[String]) {
println("Hello world!")
}
}
You can then use this object to run your Scala code in IntelliJ.

How to refer to protected inner class in Scala when inheriting from Java (with byte code only)

I am writing a Scala class to inherit from a Java class, and I must override a method that takes a protected Java inner class as a parameter. The Java dependency comes as a jar without source code.
I have the exact same setup as found in https://issues.scala-lang.org/browse/SI-3120 except that I do not have the Java source code available, so scalac only knows about the Java dependency by looking at the byte code (in jar or class files).
This is basically what I'm trying to do:
// javapkg/JavaSuperClass.java
package javapkg;
public class JavaSuperClass {
protected class JavaInnerClass {
}
public void method(JavaInnerClass javaInnerclass) {
System.out.println("hello");
}
}
// scalapkg/ScalaSubClass.scala
package scalapkg
import javapkg.JavaSuperClass
class ScalaSubClass extends JavaSuperClass {
override def method(javaInnerClass: JavaSuperClass#JavaInnerClass) {
println("world")
}
}
I have Java Sun JDK Hotspot 1.6.0_24 and Scala 2.9.0.1 on Linux. This is what happens:
$ cd javapkg
$ javac JavaSuperClass.java
$ cd ../scalapkg
$ scalac -cp .. ScalaSubClass.scala
ScalaSubClass.scala:6: error: class JavaInnerClass in class JavaSuperClass cannot be accessed in javapkg.JavaSuperClass
Access to protected class JavaInnerClass not permitted because
prefix type javapkg.JavaSuperClass does not conform to
class ScalaSubClass in package scalapkg where the access take place
override def method(javaInnerclass: JavaSuperClass#JavaInnerClass) {
^
one error found
Note, if I change JavaSuperClass#JavaInnerClass to simply JavaInnerClass, I get this:
ScalaSubClass.scala:6: error: method method overrides nothing
override def method(javaInnerClass: JavaInnerClass) {
^
one error found
Note: I know this sounds very similar to the common "protected static inner class" Java-compatibility issue in Scala, but I believe this is unrelated because there are no statics anywhere in my example.
I feel like something is wrong, because when I put the same code into a mixed java/scala project in Eclipse, it seemed to compile fine (with the latter JavaInnerClass syntax); it's only when I compile the Scala code with only the Java byte code (and no Java source code) that I cannot get it to work. Am I just completely missing the correct syntax to refer to a Java inner class, is this a known defect, or should I file a compiler bug? I couldn't find anything about this exact use case in my searching.
This is an excellent article that discuss the topic.
EDIT-1
My bad, I answered to quickly. This actually may be a bug Mike, I'm trying to see if I can find a hack around. I'll let you know if I find one.
EDIT-2
I've tried different things but I can't find a way to make it work. Mike I'd suggest you to file a bug report.