Scala's Relative Package Imports - eclipse

I have a multi-project Scala workspace in eclipse. I think I'm getting hosed by my lack of understanding of the way Scala imports packages, but after spending more time than I care to admit looking for a solution, I can't figure this one out. I have recreated the problem in a simple 2 project setup.
Project 1: com.foo.mathematics contains a simple Vector class
Contains one file:
package com.foo.mathematics
class Vector2D(x : Double, y : Double) {
def length = math.sqrt(x*x + y*y)
}
Project 2: com.foo.analysis
package com.foo.analysis
import com.foo.mathematics.Vector2D
class Frame(xAxis : Vector2D, yAxis : Vector2D) {
}
Eclipse shows an error in the import line, The error message that I get is: Object mathematics is not a member of the package com.foo.
In the outline view, my import statement says this:
com.foo.analysis.<error: <none>>.Vector2D
I have tried changing the import to:
import mathematics.Vector2D
import _root_.com.foo.mathematics.Vector2D
neither one works...
What am I missing?

Both import com.foo.mathmatics.Vector2D and import _root_.com.foo.mathmatics.Vector2D should be fine. Most likely you either haven't added the first project to the build path of the second (see Build Path > Configure Build Path in the context menu), or need to clean the second project (Project > Build Clean) after making changes in the first project.
(Also, mathmatics looks like a typo for mathematics, so double check that you really have the same name in both places.)
Relative package imports don't come into it, they just mean you could write it this way:
package com.foo
package analysis
import mathmatics.Vector2D
class Frame(xAxis : Vector2D, yAxis : Vector2D) {
}

Related

Intellij Scala math functions import

New to Intellij IDEA/Scala so I'm wondering is there shortcut to auto import Scala packages.
Example:
package test
object TestClass extends App{
var i = pow(22,22)
println("Hello World" + i );
}
It wont compile until import statment is added
import scala.math._
Coming from Eclipse/Java I expected CRTL + Shift O (or auto import) would offered me math package, must I type import myself ?
Sometimes yes, sometimes no. It depends on what you're searching for.
If you write math IntelliJ doesn't know what that is. If you write Math., that's already in scope and it will offer a menu of methods on the Math object.
If you write Date, alt-enter should bring up a menu of import options. Choose one and the import statement will be inserted into your code.
No, not necessarily.
In your settings in IntelliJ you can set up auto import by following these instructions. Alternatively, when you try and use a package that you do not have imported, it will tell you that it does not recognize what you are doing and show a red error. You can then autofill from the error (typically hit alt+enter) and it should solve the issue.

Scala works poorly with package objects with Unicode (non-ASCII) file names

Using Scala 2.12.0-M3 under SBT / Windows 10, I expect this code to compile and print "1" but Scala complains about not finding a variable under a package object with a non-ASCII name.
(1)
package パッケージ
package object パッケージ {
val a = 1
}
(2)
package second
import パッケージ._
object Example extends App {
println(a)
}
Is this a bug in the compiler? Can anyone else reproduce this issue on another platform?
It looks correct that this code doesn't compile.
When you declare a package object X in a package Y, it means that X resides in Y.
println(パッケージ.a)
would work. To make it clear, a rewritten version of your second source:
package second
object Example extends App {
println(パッケージ.パッケージ.a)
}
Now suppose you have package x and we'd want to create a package object y, whose fully qualified name is x.y, in other words that object would reside inside x:
package x
package object y
Now from the "root" package if we want to refer to y we'd need to either import x or use it's fully qualified name x.y.
Your original code is slightly confusing because you have two packages with the same name, one inside the other, and the inner one happens to be a package object.

IntelliJ IDEA warns about Scala imports instead of showing type info when importing object members

I have code similar to below
import sqlCtx.implicits._
val items = sc.parallelize(List(i1, i2, i3))
items.toDF().registerTempTable("items")
When I hover over items I would like usual behaviour - displaying type information. Instead I get warnings Avoid wildcard imports and Imports should be grouped together. I can get rid of the first by importing specific function, like
import sqlCtx.implicits.rddToDataFrameHolder
but I can't put import on top of the file what IntelliJ expects of me since it imports from an object that is created with the code and not preexisting. How to workaround it?
I use IntelliJ IDEA v. 15.0.3 with the latest Scala plugin.

How do packages work in golang

I was trying to understand how packages work in go better in terms of what golang actually enforces rather than what is usually done or considered good practice (we can talk about good practice later too, but I wish to understand go first).
From effective go it says:
"Another convention is that the package name is the base name of its source directory..."
However, the description above doesn't seem to be forced by go or required. Thus, I was wondering, if I was allowed to have multiple files with different package declarations at the top in the same directory base. If I am allowed to have multiple package declaration in the same directory, how do I then import them and use each one separately in the same file? Basically, I guess one of the problem I have is due to the wording of some of the go tutorial/documentation. If it is by convention, then for me, its implied that it is NOT enforced by the language. Because for example, go programmers do NOT write the keyword func for a function by convention. We write func because otherwise go will yell at you and it will not compile. Thus, I wish to clarify this with the example bellow (and if possible change the documentation for go, because this is a big deal in my opinion, how can this be done?).
For example say I have three file A.go, B.go, C.go that print a print function Print() that simply prints a,b,c respectively. They are all on the same base directory called maybe base. Then each has a different package declaration package Apkg, package Bpkg, package Cpkg.
how would you then go and import them? Would something as follow work?
package main
import(
nameA "github.com/user_me/base/Apkg"
nameB "github.com/user_me/base/Bpkg"
nameC "github.com/user_me/base/Cpkg"
)
func main() {
nameA.Print() \\prints a
nameB.Print() \\prints b
nameC.Print() \\prints c
}
or maybe we don't even need to specify the name if the package statments are the top are already different:
package main
import(
"github.com/user_me/base"
)
func main() {
Apkg.Print() \\prints a
Bpkg.Print() \\prints b
Cpkg.Print() \\prints c
}
the printing file are:
A.go:
//file at github.com.user_me/base and name A.go
package Apkg
import "fmt"
func Print(){
fmt.Println("A")
}
B.go:
//file at github.com.user_me/base and name B.go
package Bpkg
import "fmt"
func Print(){
fmt.Println("B")
}
C.go:
//file at github.com.user_me/base and name C.go
package Cpkg
import "fmt"
func Print(){
fmt.Println("C")
}
Also, if you can have a name different from the base, could someone clarify to me how the import is actually done? If the package name is package Apkg in base does the import have to be import github.com/user_me/base or import github.com/user_me/base/Apkg or github.com/user_me/Apkg.
I have not yet tested this but I will do so soon. The importing deal in go has been a little confusing for me and would love to get an answer and share it with the world.
No, it's 1 package per folder, so you would have to have them :
$GOPATH/src/github.com/user_me/base/Apkg/a.go
$GOPATH/src/github.com/user_me/base/Bpkg/b.go
$GOPATH/src/github.com/user_me/base/Cpkg/c.go
You can't even build it otherwise:
┌─ oneofone#Oa [/t/blah]
└──➜ go build
can't load package: package .: found packages pkgA (blah1.go) and pkgB (blah2.go) in /tmp/blah
Your your package name doesn't need to have the same name as the directory they are in, however all files in one directory must have the same package name.
Also, you can rename packages on import, for example:
import (
cr "crypto/rand"
mr "math/rand"
)
Or your library is called main (bad idea btw) :
import m "github.com/user_me/base/main"

eclipse does not treat a scalatest flatspec as junit test

here is my test case , while i right click the file eclipse doest not show any run as junit test option. I try to manual create run configuration but does not take any sense.
scala version:2.8.1 scalatest:1.3 eclipse:3.6.2
package org.jilen.cache.segment
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
#RunWith(classOf[JUnitRunner])
class RandomSegmentSpec extends FlatSpec with ShouldMatchers {
val option = SegmentOptions()
"A Simple Segment" should "contains (douglas,lea) after put into" in {
val segment = RandomSegment.newSegment(option)
segment.put("douglas", "lea")
segment("douglas") should be("lea")
}
it should "return null after (douglas,lea) is remove" in {
val segment = RandomSegment.newSegment(option)
segment.put("douglas", "lea")
segment -= ("douglas")
segment("douglas") should equal(null)
}
it should "contains nothing after clear" in {
val segment = RandomSegment.newSegment(option)
segment.put("jilen", "zhang")
segment.put(10, "ten")
segment += ("douglas" -> "lea")
segment += ("20" -> 20)
segment.clear()
segment.isEmpty should be(true)
}
}
I've encountered this seemingly randomly, and I think I've finally figured out why.
Unfortunately the plugin doesn't yet change package declarations when you move files, nor the class names when you rename files. (Given you can put multiple classes in one file, the latter will likely never be done.) If you are used to the renamings being done automagically in Eclipse, like I am, you're bound to get caught on this.
So... check carefully the following:
the package declaration in your Scala file matches the Eclipse package name
the name of the test class in the Scala file matches the name of the Scala file
I just ran into this, fixed both, and now my test runs!
This is a known problem with the Eclipse IDE for Scala. I'm currently working on the plugin for this. Watch this space.
I found Scalatest to be very bad at integrating with Eclipse (running the tests from eclipse showed that it ran them - but they would not pass or fail, but simply show up as passive blank boxes).
For some reason I could NOT get it to work after 3 hours of trying things!
Finally I tried specs2 - and it worked (Scala 2.9, Junit4 and Eclipse 3.6)!
They have a great doc here:
http://etorreborre.github.com/specs2/guide/org.specs2.guide.Runners.html#Runners+guide
Since I don't care which testing framework to use, I will try Specs2 purely from the convenience point of view.