Scala general and console completion

I tried this in scala 2.9.1 and scala 2.10 M2.

Here is my scala transcript after running the scala command from the terminal:

  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 knows that friends are of type List [Person] and that friends.head is of type Person. Can't he suggest a name as a potential conclusion?

If this is not supported, I will be happy to study it. I looked into the source code (scala -2.9.1.final-sources / src / jline / src / main / java / scala / tools / jline / console / completer), but I would appreciate any pointers on how to fix this.

Thanks.

Tim

+6
source share
1 answer

You are right, this is not supported. The prefix ( friends.head ) does not actually run through type checking to determine the exact type.

If you want to play around with this, you should use the latest version of Scala and run with -Dscala.repl.debug to find out what is happening.

 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 

You should probably seek advice from Paul Phillips, author of REPL. I am sure that he has already considered (if not half), and would have known this difficulty better than anyone else.

+7
source

Source: https://habr.com/ru/post/909331/


All Articles