Confusion regarding working of protected[some_scope] and inheritance in scala - scala

This question is about protected scope of scala. Following code runs perfectly.
//In firstfile.scala
package A{
class test{
protected[test] var a=0
}
}
package B{
class test1 extends A.test{
println(a)
}
}
But when I put package B in some other file,then compilation fails saying not found value a
// In secondfile.scala
import A.test
package B{
class test1 extends test{
println(a)
}
}
I am running this code in intellij.
what can be the reason of this? Are not the two codes same?

I think you've found a bug. I can reproduce it in Eclipse with Scala IDE 2.0.0-beta9 with Scala 2.9.0-1. First, let's create Foo.scala:
package a
class Foo {
protected[Foo] var x = 0
}
and then Bar.scala:
package b
class Bar extends a.Foo {
println(x) // Error: Not found: value x
}
Eclipse telling us, there is an error. But after cleaning a project this error disappears. And I checked, with scalac everything compiles with first attempt (who would doubt).
Update:
I've opened a ticket #1000567 on Scala IDE bugtracker regarding this issue.

Related

Dead Code encountered with x fatal warnings on

I have a piece of code:
case class ColumnChange private(field: String, old: Option[JsValue], `new`: Option[JsValue])
#typeclass
trait CreateColumnChanges[-New] {
def fieldChanges(newValue: New): Vector[ColumnChange]
}
trait LowLevelFieldChangesImplicits {
import com.github.ghik.silencer.silent
#silent
implicit object NothingColumnChanges extends CreateColumnChanges[Nothing] {
override def columnChanges(newValue: Nothing): Vector[ColumnChange] = Vector.empty
}
}
I have enabled "-Xfatal-warnings" in my build.sbt.
When I am compiling my project its throwing this error:
dead code following this construct
[error] implicit object NothingCreateFieldChanges extends CreateFieldChanges[Nothing] {
I tried changing type to CreateColumnChanges[Any], but somehow many tests are failing, which over-complicates my problem, maybe because -New is in contravariant position. I tried putting this scalacOptions "-Ywarn-dead-code" in build file too, but its not having any effect and again I am getting the same exception. I am compiling with scala 2.12.10.Also #silent annotation is not having effect on it. How can I change my code to avoid this error.
Finally this worked,
I changed type from Nothing to Unit

VS Code and Metals: Run 'Build import' to enable code navigation

I have a Scala- / Mill-Project that I wan't to import into VSCode.
The Metals Doctor gives me the following warnings:
Is there something missing in my project?
Here is my Mill configuration (build.sc):
import mill._
import mill.define.Target
import scalalib._
trait MyModule extends ScalaModule {
def scalaVersion = "2.13.1"
object version {
val cats = "2.0.0"
..
}
object libs {
val cats = ivy"org.typelevel::cats-core:${version.cats}"
..
}
object test extends Tests {
...
}
}
object entity extends MyModule {
override def ivyDeps = {
Agg(
libs.cats,
..
)
}
}
object macros extends MyModule {
..
}
Here is the whole project: https://github.com/pme123/zio-examples
The console output shows no warnings, here is the end:
...
time: connected to build server in 0.33s
time: imported build in 0.41s
time: indexed workspace in 3.85s
no build target: /Users/mpa/dev/Github/pme123/zio-examples/build.sc
This error/help message is misleading, as it presumes you are using Bloop, in which case re-importing solves the isssue (from experience). When you are using Mill's built-in BSP server, this won't change much.
Mill's recent 0.10.0-M4 release got a major BSP revamp. Also Metals improved it's Mill support. So you should have a better overall experience.
Here is some documentation: https://com-lihaoyi.github.io/mill/mill/Intro_to_Mill.html#_build_server_protocol_bsp

Scalatest with eclipse shows errors while using Matchers

I have an eclipse scala project which uses maven. Eclipse plugins for ScalaIDE and Scalatest are installed. I have tests like:
import org.scalatest._
class ExampleSpec extends FlatSpec with Matchers {
feature("Feature A Test") {
scenario("Foo scenario 1") {
val a = FooClass().getResult()
a.count shouldBe 1 // IDE shows error: value shouldBe is not a member of Long
a(0).getString(0) shouldBe "FOO" // IDE shows error: value shouldBe is not a member of String
}
}
}
The maven compilation and the tests run fine, but in eclipse when I open this file, I see an error in eclipse wherever I am using a Matcher as mentioned in the comments above. Eg.
value shouldBe is not a member of Long
What am I missing? A scala test file shows hundreds of problems.
After adding the following dummy code:
case class Bar() {
def count = Array(Bar())
def getString(x: Int) = Array("aqq")
def apply[T](x: Int) = this
}
case class FooClass() {
def getResult() = Bar()
}
and changing FlatSpec to FeatureSpec as this is the syntax you are using in your ExampleSpec, the code compiles without any issues.
If it's still not the case for you I can suggest creating simple build.sbt and generating project with Eclipse sbt plugin.
I know this is old, but I had the same issue with eclipse (late 2018), and I was able to fix this by making sure the test was NOT in the default package. That is, add "package org.scalatest.examples.flatspec" to the beginning of your test, as an example, and move the test into that package.

using inherited method from Java class in Scala [duplicate]

This question already has an answer here:
Calling a protected static Java method from Scala
(1 answer)
Closed 8 years ago.
I'm trying to compile a Scala class file that extends a Java class. Here is the class definition, it's as basic as it gets. On load, in the host application, the object should write "Hello from Scala" to the host's output window, using the superclass's method 'post'.
import com.cycling74.max.MaxObject;
public class FirstClass extends MaxObject{
public FirstClass{
post("Hello From Java");
}
}
This compiles and runs fine in the application.
Here is my translation to Scala (to be honest I'm not 100% sure I completely understand constructors in Scala):
import com.cycling74.max._
class FirstClass() extends MaxObject {
super.post("Hello from Scala")
}
But, when I try to compile with scalac, I receive the following error:
dm$ scalac -cp max.jar FirstClass.scala
FirstClass.scala:3: error: value post is not a member of com.cycling74.max.MaxObject with ScalaObject
super.post("Hello from Scala")
^
one error found
I'm not sure why the compiler is telling me that 'post' is not a member function, I'm certain that I've botched the Scala syntax, but cannot determine what is wrong.
Thanks!
EDIT
here's the output from the Max window, in addition to changing the code as prescribed below, i just added the Scala libs to Max's dynamic loadpath. This is exciting
MXJ System CLASSPATH:
/Applications/Max 6.1/Cycling '74/java/lib/jitter.jar
/Applications/Max 6.1/Cycling '74/java/lib/jode-1.1.2-pre-embedded.jar
/Applications/Max 6.1/Cycling '74/java/lib/max.jar
MXJClassloader CLASSPATH:
/Applications/Max 6.1/Cycling '74/java/classes/
/Users/dm/maxmsp/classes
/Users/dm/maxmsp/jars/jline.jar
/Users/dm/maxmsp/jars/scala-compiler.jar
/Users/dm/maxmsp/jars/scala-dbc.jar
/Users/dm/maxmsp/jars/scala-library.jar
/Users/dm/maxmsp/jars/scala-partest.jar
/Users/dm/maxmsp/jars/scala-swing.jar
/Users/dm/maxmsp/jars/scalacheck.jar
/Users/dm/maxmsp/jars/scalap.jar
Jitter initialized
Jitter Java support installed
Hello from Scala
Assuming the definitioin of post method in MaxObject as..
public class MaxObject {
public static void post(java.lang.String message){
System.out.println("printing from MaxObject.post :: " + message);
}
}
you can directly call the post method in scala as -
class FirstClass extends MaxObject {
MaxObject.post("Hello from Scala")
}
Infact, if you are not compelled to extend the MaxObject.. you can also use it as..
class FirstClass{
MaxObject.post("Hello from Scala")
}
Consuming it as :
val fc = new FirstClass //> printing from MaxObject.post :: Hello from Scala

#Test method in Scala trait not found

does somebody know why #Test annotated methods which are inherited from a Scala trait are not found by the JUnit 4 test runner? It gives me "No JUnit tests found".
class FooTests extends BarTesting
But the following modification works?
import junit.framework.TestCase
class FooTests extends TestCase with BarTesting
In the Scala trait BarTesting I defined methods with the following signature.
#Test
final def testBar() {
// ...
}
This is indeed a bug in Eclipse. You can raise as such if you like. http://www.assembla.com/spaces/scala-ide/tickets.
When you extend TestCase the test is being run because it starts with test, not because of the annotation. There was a problem with recognition of annotations, which is how the junit stuff works, and I haven't looked to see if it is fixed yet to make the junit stuff work.
Your best bet is to:
Raise the bug against scala-ide
Add #RunWith[classOf[JUnit]) to your class
The following works:
trait BarTesting {
#Test final def testBar() {
println("Hello world")
}
}
#RunWith(classOf[JUnit4])
class FooTesting extends BarTesting {
}
And I'll try and fix the bug.
EDIT: In the latest versions of scala-ide (as of 9 November 2011), this now works.