0

I have three files, one is hi.scala and the second is hic.scala and third is hello.scala. The codes of both are as follows:

hi.scala

package testpackage.src

package object hi {
    def abs(x: Double) = if (x>=0) x else -x

    def sqrt(x: Double) = {
        def sqrtIter(guess: Double, x: Double): Double =
            if(isGoodGuess(guess,x)) guess
            else sqrtIter(improve(guess,x),x)
        def isGoodGuess(guess: Double, x: Double) =
            abs(guess * guess - x)/x < .0001
        def improve(guess: Double, x: Double) = 
            (guess + x / guess)/2
        sqrtIter(1.0, x)
    }
}

hic.scala

package testpackage.src{

class hic {
    def abs(x: Double) = if (x>=0) x else -x

    def sqrt(x: Double) = {
        def sqrtIter(guess: Double, x: Double): Double =
            if(isGoodGuess(guess,x)) guess
            else sqrtIter(improve(guess,x),x)
        def isGoodGuess(guess: Double, x: Double) =
            abs(guess * guess - x)/x < .0001
        def improve(guess: Double, x: Double) = 
            (guess + x / guess)/2
        sqrtIter(1.0, x)
    }
}

}

hello.scala

import testpackage.src._

object hello {
    def main(args: Array[String]): Unit = {
        println(hi.sqrt(2)) //works fine
        println(hi.abs(-2)) //works fine
        println(new hic) //  error: not found: type hic
        println(new testpackage.src.hic) // error: type hic is not a member of package testpackage.src
    }
}

I am able to access sqrt and abs methods of hi.scala file's hi object, but I am not able to instantiate an object of hic.scala file's hic class. I am not able to understand why I am not able to instantiate the object of a class within a package.

Update: As it turned out, there was not an error in the code, but in the method of execution. The code is giving and error with the following commonds (@ println(new hic) and println(new testpackage.src.hic)) -

scalac *.scala and scala hello.scala

however, the code runs fine with the following commands -

scalac *.scala and scala hello
Kshitij Mittal
  • 2,698
  • 3
  • 25
  • 40
  • Something doesn't seem right here. You certainly shouldn't be able to call those methods from the class without instantiating it first. Do you also have a `hi` *object* as well as the class? You mentioned `new hi` gives an error - what's the error? – mikej Dec 20 '16 at 11:40
  • Look here http://stackoverflow.com/questions/17270003/why-are-classes-inside-scala-package-objects-dispreferred – mgosk Dec 20 '16 at 11:47
  • I have updated the question, there was a compiled .class file which had a package object hi, using which I was able to call sqrt and abs methods. It was my mistake (I am new to scala). Now, I am just not able to understand why I am not able to instantiate a class within a package. – Kshitij Mittal Dec 20 '16 at 12:03
  • do you have any package called "hi" ? – Balaji Reddy Dec 20 '16 at 12:20
  • No, I have a package called testpackage.src. hi is a package object of package testpackage.src in hi.scala file. – Kshitij Mittal Dec 20 '16 at 12:26

2 Answers2

0

There is an extra pair of {} in hic.scala.

package testpackage.src{

......

}

I've tested that every thing is fine after removing that, except

println(new platify.src.hic) // error: type hic is not a member of package platify.src

as platify never happened before.

Mo Tao
  • 1,225
  • 1
  • 8
  • 17
  • 1
    http://www.scala-lang.org/old/node/119.html - Packages in scala are also created with this format (with curly braces). – Kshitij Mittal Dec 20 '16 at 12:15
  • @Mo Tao extra pair of {} is not the problem. – Balaji Reddy Dec 20 '16 at 12:17
  • platify is actually a typo, I have fixed it in the question and replaced with testpackage. I am testing without braces now. – Kshitij Mittal Dec 20 '16 at 12:20
  • For me, it is still the same. Both with or without the braces, there isn't any change in the error. – Kshitij Mittal Dec 20 '16 at 12:24
  • @KshitijMittal Yes, the curly braces are not the problem. How did you run these code? I just created three .scala files as you presented in your problem, and typed `scalac *.scala` and `scala hello`. Then the hic object is successfully instantiated and everything looks ok. – Mo Tao Dec 20 '16 at 12:33
  • so are println(new hic) and println(new testpackage.src.hic) statements running correctly on your system?. – Kshitij Mittal Dec 20 '16 at 12:36
  • @KshitijMittal Sure. Something like `testpackage.src.hic@233795b6` and `testpackage.src.hic@3eb738bb`. – Mo Tao Dec 20 '16 at 12:37
  • @KshitijMittal You could check 1) is . in your CLASSPATH? and 2) does ./testpackage/src/hic.class exist? – Mo Tao Dec 20 '16 at 12:39
  • Oh, I was doing one thing different. Rather than scala hello, I was using the following command - scala hello.scala. This command is working in case of scala object but not in case of class. Is there any reason for the same? – Kshitij Mittal Dec 20 '16 at 12:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131048/discussion-between-kshitij-mittal-and-mo-tao). – Kshitij Mittal Dec 20 '16 at 12:40
0

I think the problem is with package object hi.scala. Package object name should be your package name and file name should be package.scala .For example, if I want to create package object for "com.mine" then my package object name should be "mine".

In your case, if testpackage.src is to be considered as your package , then your package object should be

package testpackage

package object src {
    def abs(x: Double) = if (x>=0) x else -x

    def sqrt(x: Double) = {
        def sqrtIter(guess: Double, x: Double): Double =
            if(isGoodGuess(guess,x)) guess
            else sqrtIter(improve(guess,x),x)
        def isGoodGuess(guess: Double, x: Double) =
            abs(guess * guess - x)/x < .0001
        def improve(guess: Double, x: Double) = 
            (guess + x / guess)/2
        sqrtIter(1.0, x)
    }
}

Additional Info

Community
  • 1
  • 1
Balaji Reddy
  • 5,576
  • 3
  • 36
  • 47
  • No, actually in hi.scala, everything is working fine. I am able to access methods sqrt and abs of hi.scala's object (as mentioned at the end of the question). But I am not able to access a class ( and instantiate an object of that class) from hic.scala. So, my question is just this - why I am not able to access a class from the package, just like I am able to access an object? – Kshitij Mittal Dec 20 '16 at 12:33
  • I'm still wondering , how could your package object will work with file name called hi.scala.. its thumb rule to have package object as package.scala unless your are using nested package syntax but that is quite unusual. – Balaji Reddy Dec 20 '16 at 12:37