2011-04-29 39 views
2

藉此Java類:斯卡拉的Java不兼容引用靜態字段的類具有相同的名稱作爲靜態內部類

public class Fisk { 

    public static class A { 
    } 

    public static A A = new A(); 
} 

此Java代碼的工作:

Fisk.A a = new Fisk.A(); 
    Fisk.A b = Fisk.A; 

但是,從調用它斯卡拉:

val fisk = new Fisk.A() 
    val strupp = Fisk.A 

導致編譯器錯誤:

error: ambiguous reference to overloaded definition, 
[INFO] both variable A in object Fisk of type Fisk.A 
[INFO] and object A in object Fisk of type object Fisk.A 
[INFO] match expected type ? 
[INFO]   val strupp = Fisk.A 
[INFO]         ^
[ERROR] one error found 

任何人都知道解決這個問題,或者我必須重命名我的靜態字段?

- 安德烈亞斯

+0

您是否嘗試給出明確的類型,例如'val strupp:Fisk.A = Fisk.A'? – Landei 2011-04-29 09:01:45

+0

給出一個明確的類型使它能夠使用Maven編譯,但在IDEA中給出錯誤: 錯誤:A已經定義爲對象A public static A A = new A(); 任何提示? – Andreas 2011-04-29 09:08:56

回答

1
scala> Fisk.A 
<console>:8: error: ambiguous reference to overloaded definition, 
both variable A in object Fisk of type Fisk.A 
and object A in object Fisk of type object Fisk.A 
match expected type ? 
     Fisk.A 
      ^
// this is the static field A of Fisk 
scala> Fisk.A: Fisk.A 
res1: Fisk.A = [email protected] 

// this is a new constructed instance of type Fisk.A 
scala> val fisk = new Fisk.A() 
fisk: Fisk.A = [email protected] 

// this is the static field A of Fisk (see the same hashcode) 
scala> val strupp: Fisk.A = Fisk.A 
strupp: Fisk.A = [email protected] 
+0

我向Jetbrains提交了一張票,因爲它使用顯式類型,但由於某種原因不能在IDEA中編譯:[http://youtrack.jetbrains.net/issue/SCL-3146](http://youtrack .jetbrains.net/issue/SCL-3146) 請投票支持:-) – Andreas 2011-04-29 09:25:24

相關問題