package - Confusion with a simple Scala packaging example -


i've been experiencing confusion on packaging classes in scala , importing packages. let me start pair of simple source files:

file: a/a.scala

package  // of these imports should used? both seem work. //import a.b._ import b._  class {    val fieldb = new b } 

file: a/b/b.scala

package a.b  class b 

usage

compiling scalac works without complaint either of imports above in a.scala

trying load these files in repl works differently:

$ scala welcome scala version 2.8.0.r0-b20100714201327 (java hotspot(tm) server vm, java 1.6.0_20). type in expressions have them evaluated. type :help more information.  scala> :l a/b/b.scala loading a/b/b.scala... <console>:1: error: illegal start of definition        package a.b        ^ defined class b  scala> :l a/a.scala loading a/a.scala... <console>:1: error: illegal start of definition        package        ^ <console>:5: error: not found: value b        import b._               ^ defined class  scala> 

so, have questions:

  • what correct way import in a.scala above?

  • the compiler seems able figure out if import relative package in or if absolute, without _root_. i'm seeing?

  • am doing correctly in repl? why seem unhappy seeing package statements, , why import b._ generate error?

thank you

ps know directory structure doesn't have match packaging. doing voluntarily helping me less confused now.

first off cannot define packages in repl. reason repl-statements wrapped objects. that's why :load command fails. need compile source files , add classpath if want use packages.

when trying resolve symbol imported compiler tries find in actual scope, i.e. when write

import a._ import b._ 

this import package a , package a.b. if inside package a b in scope , second import sufficient. compiler imports scala._ can use relative imports import xml._ import scala.xml._.

in addition there feature called nested packages lets write b.scala like

package package b class b { /* .... */ } 

which results in package a being imported in file.

if compiler cannot resolve import relative symbol in default package (_root_).


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -