1

Ich habe einen Beispielcode:Fehler bei SPARK REPL (Verweis auf A ist mehrdeutig) und funktioniert in Intellij und Scala REPL?

class A(str: String) { 
    println(s"InsideCase:::$str") 
} 

object A { 
    def apply(str: String) = { 
    println("foobar::") 
    new A(str) 
    } 
} 

object b extends App { 
    A("kool") 
} 

Dieser Code funktioniert in IntelliJ in Ordnung. Und gibt mir Ausgabe:

foobar:: 
InsideCAse:::kool 

aber wenn ich versuche dies auf REPL zu tun:

scala> :paste 
// Entering paste mode (ctrl-D to finish) 

class A(str: String) { 
    println(s"InsideCAse:::$str") 
} 

object A { 
    def apply(str: String) = { 
    println("foobar::") 
    new A(str) 
    } 
} 


// Exiting paste mode, now interpreting. 

defined class A 
defined object A 

scala> A("kool") 

Es gibt mir die folgende Fehlermeldung:

<console>:27: error: reference to A is ambiguous; 
it is imported twice in the same scope by 
import $line31$read.A 
and import INSTANCE.A 
     A("kool") 

Was ich hier fehlt? Wenn Sie bitte ausführlich erklären können, wäre das hilfreich.

+0

Es funktioniert gut für mich. –

+0

Es funktioniert auf Scala REPL dann Warum funktioniert es nicht auf SPARK REPL? –

Antwort

2

Dies ist eine Spark REPL-Einschränkung. Sie können entweder tun es eine alte Schule Art und Weise mit einem Objekt-Wrapper:

object awrapper { 

    class A(str: String) { 
    println(s"InsideCAse:::$str") 
    } 

    object A { 
    def apply(str: String) = { 
     println("foobar::") 
     new A(str) 
    } 
    } 
} 

import awrapper._ 

oder ein Paket mit paste -raw (Scala 2.11+) definieren:

scala> :paste -raw 
// Entering paste mode (ctrl-D to finish) 

package apacakage 

class A(str: String) { 
    println(s"InsideCAse:::$str") 
} 

object A { 
    def apply(str: String) = { 
    println("foobar::") 
    new A(str) 
    } 
} 


// Exiting paste mode, now interpreting. 


scala> import apacakage._ 
import apacakage._ 

scala> A("kool") 
foobar:: 
InsideCAse:::kool 
res1: apacakage.A = apacakage[email protected]