2013-03-25 8 views
8

In Scala REPL kann man Werttypen finden:Scala REPL: Wie funktioniert der Funktionstyp?

scala> val x = 1 
    x: Int = 1 

    scala> :t x 
    Int 

Doch Scala REPL zeigt nicht die Typinformationen für Funktionen:

scala> def inc(x:Int) = x + 1 
    inc: (x: Int)Int 

scala> :t inc 
<console>:9: error: missing arguments for method inc; 
follow this method with `_' if you want to treat it as a partially applied function 
     inc 
    ^
<console>:9: error: missing arguments for method inc; 
follow this method with `_' if you want to treat it as a partially applied function 
      inc 
     ^

Wie Funktion Typ in Scala REPL finden?

+0

Wie wäre es zu tun, was sagt es in der Fehlermeldung? – folone

+0

möglich duplicate von [this] (http://stackoverflow.com/questions/15583551/type-information-in-the-scala-repl) –

Antwort

21

den Vorschlag folgend wird ziemlich gut funktionieren:

:t inc _ 
Int => Int 

Um ein bisschen mehr Detail zu geben, der Grund, warum dies notwendig ist, ist, dass Scala eine Unterscheidung zwischen ‚Methoden‘ unterhält, die in der JVM native Unterstützung haben, aber die nicht die erste Klasse sind, und 'Funktionen', die als Instanzen von FunctionX behandelt und von der JVM als Objekte betrachtet werden. Die Verwendung des abschließenden Unterstrichs wandelt den ersten in den letzteren um.

+1

Danke! Warum kann ich dann nicht: ': t scala.collection.immutable.List.type.foldLeft _' und auch nicht' 't java.Lang.String.split _'? –

+2

Dies sind beide Klassenmethoden, und Sie können eine Instanzmethode nur teilweise anwenden (d. H. Zu einem 'Funktion'-Objekt konvertieren). Erstellen Sie zum Beispiel eine bestimmte Instanz einer 'List' und Sie können den Typ der 'foldLeft'-Methode überprüfen. – Impredicative

0

Sie können den Namen der Methode schreiben und Tab drücken.

Stream.fill<tab> 

gibt Ihnen:

def fill[A](n1: Int,n2: Int,n3: Int)(elem: => A): 
scala.collection.immutable.Stream[scala.collection.immutable.Stream[scala.collection.immutable.Stream[A]]] 
def fill[A](n1: Int,n2: Int,n3: Int,n4: Int)(elem: => A): scala.collection.immutable.Stream[scala.collection.immutable.Stream[scala.collection.immutable.Stream[scala.collection.immutable.Stream[A]]]] 
def fill[A](n1: Int,n2: Int)(elem: => A): scala.collection.immutable.Stream[scala.collection.immutable.Stream[A]] 
def fill[A](n1: Int,n2: Int,n3: Int,n4: Int,n5: Int)(elem: => A): scala.collection.immutable.Stream[scala.collection.immutable.Stream[scala.collection.immutable.Stream[scala.collection.immutable.Stream[scala.collection.immutable.Stream[A]]]]] 
override def fill[A](n: Int)(elem: => A): scala.collection.immutable.Stream[A] 
Verwandte Themen