I'm having trouble importing packages into package objects. It didn't seem to work in Eclipse and so I switched to intellij. At one point the feature seemed to be working and so I created package objects for most packages. Now it doesn't seem to be working at all. Here's a package object in file package.scala, the package file itself compiles fine:
package rStrat.rSwing
package testSw //Edited for clarity
object testSw
{
import rStrat._
import rSwing.topUI._
}
and here's a class file from the same module and package.
package rStrat.rSwing.testSw
object MainTest {
def main(args: Array[String])
{
val testApp = new AppWindow //Appwindow is a member of topUI
testApp.open
}
}
It works fine if I import the topUI package straight into the MainTest file. it makes no difference whether I try and import the whole package or a particular class. is this legal scala? Is the problem with the IDEs?
I'm using Scala 2.92 Final, Intellij 11.1.1, JDK 1.6.0_31, Eclipse 3.7.2
Scala does not have first class imports. Where a package can only contain declarations of classes and traits, a package object can contain any other valid Scala declaration like var, val, def, type (type alias), implicit stuff. And although as in any object you can import things, they don't transitively propagate to the rest of the package and are thus not visible to the rest of the world.
This creates the object rStrat.rSwing.testSw.testSw:
package rStrat.rSwing
package testSw //Edited for clarity
object testSw
This creates the package object rStrat.rSwing.testSw.testSw:
package rStrat.rSwing
package testSw //Edited for clarity
package object testSw
This creates the package object rStrat.rSwing.testSw:
package rStrat.rSwing
package object testSw
It's the last one you want.
Related
In my scala worksheet test.sc I define :
import com.tradedata.VolatilityInstance
test.sc exists at src/main/scala
In location src/main/scala I define :
package com.tradedata
class VolatilityInstance(intervalLength: Int, volatility: Double, beginDate: String, endDate: String) {
override def toString: String = {
this.beginDate
}
}
The filename for this class is VolatilityData.scala
VolatilityData.scala exists at level src/main/scala/com/tradedata
When I attempt to run the worksheet I receive error :
Error:(1, 16) object VolatilityInstance is not a member of package com.tradedata
import com.tradedata.VolatilityInstance
If i define a new Scala object :
import com.tradedata.VolatilityInstance
object Tester extends App {
}
Tester object runs without issue.
I've tried re-building the project, invalidating the caches and restarting IntelliJ but cannot fix.
Is this a bug with the Scala worksheet ? How to import other classes into the Scala worksheet ?
I've tested your case in IntelliJ IDEA 2019.2.3 (Ultimate Edition) with Scala plugin 2019.2.28
Open test.sc, click Scala worksheet settings and change Run type from REPL to Plain.
In Worksheet Settings check Build project before run
It helps me
Swift newbie here, using swift 3 on Linux with the package manager.
I have a package Regulate, executable, and a sibling package Utils, intended to be a library. Utils/Sources has a TextReader.swift file, with class TextReader and its init function both public. The Utils directory is a git repo, described in Regulate/Package.swift:
dependencies: [.Package(url: "../Utils", "1.0.0")]
I've tried 3 ways to instantiate a TextReader object in the Regulate program and gotten 3 error messages:
import Utils
...
let reader = TextReader(filename: name)
error: use of unresolved identifier 'TextReader'
import Utils
...
let reader = Utils.TextReader(filename: name)
error: module 'Utils' has no member named 'TextReader'
import class Utils.TextReader
error: no such decl in module
It looks like the library module needs some additional structure to declare its exports, perhaps.
What do I need to do here? Thanks!
D'oh! This looks like a name conflict.
When I use Utils2 instead of Utils, it works fine. With Utils it cloned and built the other module, but apparently went to some system module instead when I referenced it.
I have the following packages hierarchy:
rootpackage
---firstpackage
---secondpackage
rootpackage contains a package object.
I know that if a file from firstpackage has package declaration in the form:
package rootpackage.firstpackage
the content of rootpackage package object will not be in the file scope, but with the following declaration it will:
package rootpackage
package firstpackage
How this strange difference is explained? And is there more transparent way to expose the package object's content to subpackages, like importing rootpackage package object content to firstpackage one?
Package object members can be imported in the following way:
import rootpackage.SomeMember
I am learning Scala so bear with me if this is a stupid question.
I have this package and a class (teared it down to most simplistic version):
package Foo {
class Bar {}
}
then in main.scala file I have:
import Foo.Bar
object test {
def main() {
val b = new Bar()
}
}
Why am I getting this:
test.scala:1: error: Bar is not a member of Foo
It points at the import statement.
scalac is the scala compiler. Foo.bar needs to have been compiled for you to use it, so you can't just run your main.scala as a script.
The other mistake in your code is that the main method needs to be
def main(args: Array[String]) { ...
(or you could have test extends App instead and do away with the main method).
I can confirm if you put the two files above in an empty directory (with the correction on the main method signature) and run scalac * followed by scala test it runs correctly.
The most likely explanation is that you did not compile the first file, or you are doing something wrong when compiling. Let's say both files are in the current directory, then this should work:
scalac *.scala
It should generate some class files in the current directory, as well as a Bar.class file in the Foo directory, which it will create.
To quickly test a scala code in IntelliJ (with the Scala plugin), you can simply type Ctrl+Shift+F10:
Note that for testing a Scala class, you have other choices, also supported in IntelliJ:
JUnit or TestNG
ScalaTest
I have a library which has root package "scala", and now I have a project using this library, and I have a sub package named "com.zjffdu.scala". And the class file in this package needs to import classes from the library. So I have the following import statement.
import scala._
But because this class is also in package "scala", the scala compiler will look for files in current directory rather than the library.
So how can I explicit to tell scala to import classes from the library.
Thanks
Use this:
import _root_.scala._
As you can see it's not very pretty — the best option is probably to avoid naming one of your packages scala.
And by the way — the root scala package is always preimported (though subpackages, of course, are not).