多次Scala导入函数的范围

时间:2015-10-31 03:12:52

标签: python scala

我正在学习Scala并使用Scala REPL遇到此问题。我正在玩immutable.Mapmutable.Map

  Welcome to 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 to Map is ambiguous;
  it is imported twice in the same scope by
  import scala.collection.immutable.Map
  and import scala.collection.mutable.Map
   var mymap2 = Map[Int, String](1->"one", 2->"two", 3->"three")
                ^
  scala>

这是否意味着我只能导入一次相同的函数名称?我有一个Python背景,似乎我可以导入任意多次。是这样的吗?如果是这样,这里的设计理念是什么:

  # I created two files in the working directory module1 and module2 and they
  # both have a function called myfunc() to its 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" for more information.
  >>> from module1 import myfunc
  >>> print myfunc()
  module1
  >>> from module2 import myfunc
  >>> print myfunc()
  module2
  >>> from module1 import myfunc
  >>> print myfunc()
  module1
  >>> 

2 个答案:

答案 0 :(得分:3)

您可以导入其中一个名称更改的类型,这通常可以是您正在处理的实体类型的有用信号。例子:

import Cocoa

class MyApplication: NSApplication {
    override func sendEvent(event: NSEvent) {
        if event.type == NSEventType.KeyDown {

            if ((event.modifierFlags.rawValue) & (NSEventModifierFlags.CommandKeyMask.rawValue) > 0) {
                switch event.charactersIgnoringModifiers!.lowercaseString {
                case "x":
                    if NSApp.sendAction(Selector("cut:"), to:nil, from:self) { return }
                case "c":
                    if NSApp.sendAction(Selector("copy:"), to:nil, from:self) { return }
                case "v":
                    if NSApp.sendAction(Selector("paste:"), to:nil, from:self) { return }
                case "z":
                    if NSApp.sendAction(Selector("undo:"), to:nil, from:self) { return }
                case "a":
                    if NSApp.sendAction(Selector("selectAll:"), to:nil, from:self) { return }
                default:
                    break
                }
            }
        }
        return super.sendEvent(event)
    }
}

然后通过新的(本地)名称使用导入的类型:

scala> import scala.collection.mutable.{Map => MMap} // or MutableMap, or whatever label you find useful
scala> import java.util.{Map => JMap} // or JavaMap, or whatever

答案 1 :(得分:0)

Scala的REPL在编译常规代码时可能保持scala的相同行为(而不是来自repl):

#!/bin/sh
exec scala -nc -save "$0" "$@"
!#

object Hello {
  def main(args:Array[String]):Unit = {
    println("Hello, Scala !! ")
    var mymap = Map[Int, String](1->"one", 2->"two", 3->"three")

    import scala.collection.mutable.Map

    var mymap1 = Map[Int, String](1->"one", 2->"two", 3->"three")

    import scala.collection.immutable.Map

    var mymap2 = Map[Int, String](1->"one", 2->"two", 3->"three")
  }
}

返回编译错误:

error: reference to Map is ambiguous;
it is imported twice in the same scope by
import scala.collection.immutable.Map
and import scala.collection.mutable.Map
    var mymap2 = Map[Int, String](1->"one", 2->"two", 3->"three")

在python中,这不会给出任何错误:

#!/usr/bin/python

from datetime import datetime
print(datetime.now())

from datetime import datetime
print(datetime.now())

from datetime import datetime
print(datetime.now())

打印

2015-10-31 01:47:25.492501
2015-10-31 01:47:25.492589
2015-10-31 01:47:25.492609

如果你真的想在scala的REPL中这样做,你可以做Shadowlands所建议的。

或者您可以使用scala ipython - 替代方案:

Ammonite - A Modernized Scala REPL

我用我的菊石进行局部测试,重复你的步骤,问题没有发生。可能amite会更聪明地避免REPL中的导入冲突(但是仍会在常规scala代码中给你一个编译错误)