Named result variables

Cay Horstmann cay at horstmann.com
Fri Oct 30 00:24:32 UTC 2015


When I teach, I like to use jshell for quick demos. I might predefine

<T> Set<T> setOf(T... values) { return new 
TreeSet<>(Arrays.asList(values)); }

and then construct two sets:

Set<Integer> set1 = setOf(1, 2, 3);
Set<Integer> set2 = setOf(2, 3, 5);
set1.removeAll(set2)

Except, that's a lot of typing, so I don't do that. I do

setOf(1, 2, 3);
setOf(2, 3, 5);
$1.removeAll($2)

Note the absence of types. The $n are automatically typed.

But I lose track of those $n prettty soon. It would be nice if i could 
name them.

I brought this up at the Java One presentation and suggested

/var set1 $1

or

/var set2

to name the last $n.

Robert thought that was a bit too much like inventing a new langauge 
feature and suggested that one might be able to solve this with tab 
completion instead.

I thought about this a bit and couldn't come up with a compelling way. 
Right now, when one hits Tab on a blank line, one is offered a 
collection of well over 400 possible completions (types, packages, 
classes, $ variables). Using that to get to something like Set<Integer> 
or TreeMap<String, TreeSet<Integer>> isn't satisfactory.

Does anyone else think that an easy way of naming results is worthwhile? 
Any ideas on what the user interface should be?

Cheers,

Cay


-- 

Cay S. Horstmann | http://horstmann.com | mailto:cay at horstmann.com


More information about the kulla-dev mailing list