gpt4 book ai didi

generics - Scala 泛型和控制台自动完成

转载 作者:行者123 更新时间:2023-12-04 14:49:25 26 4
gpt4 key购买 nike

我在 Scala 2.9.1 和 Scala 2.10 M2 中尝试过这个。

这是我从终端运行 scala 命令后的 scala 成绩单:

Scala> case class Person(val name: String)
定义类 Person

Scala> val Friends = List(Person("Fred"))
friend :列表[人] = 列表(人(弗雷德))

Scala>friends.head.TAB
asInstanceOf isInstanceOf toString

Scala>friends.head.name
res0:字符串 = 弗雷德

Scala> :t friend
列表[人]

Scala> :tfriends.head


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

如果这不受支持,我很乐意考虑修复它。我查看了源代码 (scala-2.9.1.final-sources/src/jline/src/main/java/scala/tools/jline/console/completer) 但我很感激任何关于如何修复它的指针.

谢谢。

蒂姆

最佳答案

你是对的,它不受支持。前缀( 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 的建议。我敢肯定他已经考虑过(如果还没有实现一半),并且比其他任何人都更了解其中的困难。

关于generics - Scala 泛型和控制台自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9441844/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com