scala泛型和控制台自动完成

时间:2012-02-25 06:24:28

标签: generics scala autocomplete console

我在scala 2.9.1和scala 2.10 M2中尝试了这个。

这是从终端运行scala命令后的scala脚本:

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

Scala知道朋友属于List [Person],而friends.head属于Person类型。它不应该建议将名称作为潜在的完成吗?

如果不支持,我会很乐意考虑修复它。我在源代码中浏览过(scala-2.9.1.final-sources / src / jline / src / main / java / scala / tools / jline / console / completer)但我很欣赏任何有关如何解决它的提示

感谢。

1 个答案:

答案 0 :(得分: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作者Paul Phillips的建议。我确信他已经考虑过(如果不是已经实施了一半),并且比其他任何人都更了解难度。