python - Scala Import Function in Scope Multiple Times -
i learning scala , run problem using scala repl. playing immutable.map
, mutable.map
,
welcome scala version 2.11.6 (java hotspot(tm) 64-bit server vm, java 1.8.0_60). scala> map res0: scala.collection.immutable.map.type = scala.collection.immutable.map$@2e32ccc5 scala> var mymap = map[int, string](1->"one", 2->"two", 3->"three") mymap: scala.collection.immutable.map[int,string] = map(1 -> one, 2 -> two, 3 -> three) scala> import scala.collection.mutable.map import scala.collection.mutable.map scala> var mymap1 = map[int, string](1->"one", 2->"two", 3->"three") mymap1: scala.collection.mutable.map[int,string] = map(2 -> two, 1 -> one, 3 -> three) scala> import scala.collection.immutable.map import scala.collection.immutable.map scala> var mymap2 = map[int, string](1->"one", 2->"two", 3->"three") <console>:9: error: reference map ambiguous; imported twice in same scope import scala.collection.immutable.map , import scala.collection.mutable.map var mymap2 = map[int, string](1->"one", 2->"two", 3->"three") ^ scala>
does mean can import same function name once? have python background , seems can import many times want. case? if so, design philosophy here:
# created 2 files in working directory module1 , module2 , # both have function called myfunc() corresponding module name. python 2.7.6 (default, mar 22 2014, 22:59:56) [gcc 4.8.2] on linux2 type "help", "copyright", "credits" or "license" more information. >>> module1 import myfunc >>> print myfunc() module1 >>> module2 import myfunc >>> print myfunc() module2 >>> module1 import myfunc >>> print myfunc() module1 >>>
you can import 1 of types name change, can useful signal of type of entity dealing anyway. examples:
scala> import scala.collection.mutable.{map => mmap} // or mutablemap, or whatever label find useful scala> import java.util.{map => jmap} // or javamap, or whatever
then use imported types via new (local) name:
scala> var mymap1 = mmap[int, string](1->"one", 2->"two", 3->"three") mymap1: scala.collection.mutable.map[int,string] = map(2 -> two, 1 -> one, 3 -> three) scala> import scala.collection.immutable.map import scala.collection.immutable.map scala> var mymap2 = map[int, string](1->"one", 2->"two", 3->"three") mymap2: scala.collection.immutable.map[int,string] = map(1 -> one, 2 -> two, 3 -> three)
Comments
Post a Comment