2012-02-25 67 views
6

我試過這在斯卡拉2.9.1和斯卡拉2.10 M2。斯卡拉泛型和控制檯自動完成

這裏是我的成績單階從終端運行階命令後:

 
scala> case class Person(val name: String) 
defined class Person 

scala> val friends = List(Person("Fred")) 
friends: List[Person] = List(Person(Fred)) 

scala> friends.head.TAB 
asInstanceOf isInstanceOf toString  

scala> friends.head.name 
res0: String = Fred 

scala> :t friends 
List[Person] 

scala> :t friends.head 
Person 

斯卡拉知道朋友的類型是List [人]的那friends.head是類型的人。它不應該能夠建議名稱作爲潛在的完成嗎?

如果這不被支持,我很樂意考慮修復它。我偷看了源代碼(scala-2.9.1.final-sources/src/jline/src/main/java/scala/src/jline/console/completer),但我會很感激任何指示如何解決它。

謝謝。

Tim

回答

7

你是對的,它不被支持。前綴(friends.head)實際上並未通過類型檢查器來確定準確的類型。

如果你想玩這個,你應該使用最新版本的Scala,並與-Dscala.repl.debug運行,看看發生了什麼。

scala> ps.head. 
complete(ps.head., 8) last = (, -1), verbosity: 0 
List[$read$$iw$$iw$Person] completions ==> List(removeDuplicates, toStream, stringPrefix, reverse, span, dropWhile, takeWhile, splitAt, takeRight, slice, drop, take, toList, +:, ++, mapConserve, reverse_:::, :::, ::, companion, lastIndexWhere, indexWhere, segmentLength, isDefinedAt, lengthCompare, sameElements, dropRight, last, reduceRight, reduceLeft, foldRight, foldLeft, find, count, exists, forall, foreach, apply, length, productPrefix, productIterator, seq, corresponds, iterator, toSeq, toString, view, indices, sorted, sortBy, sortWith, padTo, :+, updated, patch, distinct, intersect, diff, union, contains, containsSlice, lastIndexOfSlice, indexOfSlice, endsWith, startsWith, reverseIterator, reverseMap, combinations, permutations, size, lastIndexOf, indexOf, prefixLength, lift, andThen, orElseFast, orElse, compose, canEqual, zipWithIndex, zipAll, zip, copyToArray, sliding, grouped, head, toIterator, toIterable, isEmpty, transpose, flatten, unzip3, unzip, genericBuilder, withFilter, toTraversable, inits, tails, init, lastOption, tail, headOption, scanRight, scanLeft, scan, groupBy, partition, collect, filterNot, filter, flatMap, map, ++:, hasDefiniteSize, repr, isTraversableAgain, par, addString, mkString, toMap, toSet, toBuffer, toIndexedSeq, toArray, copyToBuffer, minBy, maxBy, max, min, product, sum, aggregate, fold, reduceOption, reduce, reduceRightOption, reduceLeftOption, :\, /:, collectFirst, nonEmpty, /:\, asInstanceOf, isInstanceOf, productArity, productElement) 
List[$read$$iw$$iw$Person] -> 'head' ==> Some(()Object (31 members)) 
()Object completions ==> List(toString, asInstanceOf, isInstanceOf) 
package <root> completions ==> List(target, project, lib_managed, quicktime, ch, scala, apple, java, com, sunw, javax, sun, org, asInstanceOf, isInstanceOf, toString) 
package <root> -> 'ps' ==> None 
object Predef -> 'ps' ==> None 
package scala -> 'ps' ==> None 
package java.lang -> 'ps' ==> None 
tryCompletion(Parsed(ps.head./8), _) lastBuf = ps.head., lastCursor = 8, p.position = 8 

asInstanceOf isInstanceOf toString 

你可能應該徵求保羅菲利普斯,REPL作者的意見。我相信他已經考慮過了(如果不是一半的話),並且比其他任何人都更瞭解困難。

+1

對於'-Dscala.repl.debug'爲+1。它可能也可以從裏面repl設置,對吧? – 2012-02-25 14:04:44