Skip to content

Commit 43868b8

Browse files
committed
Merge pull request scala#2759 from sschaef/gh-pages
Add explanations about new REPL commands to feature list of 2.11
2 parents 622327a + 60835e6 commit 43868b8

File tree

1 file changed

+233
-3
lines changed

1 file changed

+233
-3
lines changed

2.11/index.markdown

Lines changed: 233 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,237 @@ Improve performance of reflection SI-6638
3838

3939
## REPL
4040

41-
- @som-snytt added and improved several commands (:javap, :paste, :edit,...)
42-
- @folone and @eed3si9n contributed the `:kind` command to help to tell ground types from type constructors (#2340)
43-
- @rjolly made it possible to embed the repl as a JSR-223 Scripting Engine (#2206).
41+
The REPL is improved with several new commands that ease its usage. An overview of the commands can be seen by typing `:help` into the REPL.
4442

43+
### `:paste` ([#2725](https://github.com/scala/scala/pull/2725))
44+
45+
`:load` can only interpret a file from top to bottom which is for example a problem when there is a reference to a definition that is defined later. `:paste` is overworked to solve this limitation. It now can not only load a file but also handle it as a single unit.
46+
47+
Contents of file `test.scala`:
48+
```scala
49+
// Foo has a companion object
50+
class Foo { private val foo = 7 }
51+
object Foo { def apply(f: Foo) = f.foo }
52+
Foo(new Foo)
53+
```
54+
55+
REPL session:
56+
```scala
57+
scala> :paste test.scala
58+
Pasting file y.scala...
59+
defined class Foo
60+
defined object Foo
61+
res4: Int = 7
62+
```
63+
64+
Theres is also the `-raw` option available, which denotes that the following is not a script anymore, but a normal Scala file:
65+
66+
```scala
67+
scala> :paste -raw
68+
// Entering paste mode (ctrl-D to finish)
69+
70+
package abc
71+
case class Foo(bar: Int)
72+
73+
// Exiting paste mode, now interpreting.
74+
75+
76+
scala> abc.Foo(5)
77+
res5: abc.Foo = Foo(5)
78+
```
79+
80+
Furthermore, there is a bug fixed that led to the problem that no error message is reported when the read code was incomplete ([#2672](https://github.com/scala/scala/pull/2672)).
81+
82+
### `:save` ([#2697](https://github.com/scala/scala/pull/2697))
83+
84+
With `:save` it is now possible to store the current REPL session to a file which allows one to extend the current state of the REPL at a later time:
85+
86+
```scala
87+
scala> val i = 7
88+
i: Int = 7
89+
90+
scala> val j = 8
91+
j: Int = 8
92+
93+
scala> i*j
94+
res0: Int = 56
95+
96+
scala> :save session.scala
97+
98+
scala> :load session.scala
99+
Loading session.repl...
100+
i: Int = 7
101+
j: Int = 8
102+
res1: Int = 56
103+
```
104+
105+
The `:save` command does only save the Scala commands to file not the full REPL output:
106+
107+
```
108+
$ cat session.scala
109+
val i = 7
110+
val j = 8
111+
i*j
112+
```
113+
114+
### `:settings` ([#2701](https://github.com/scala/scala/pull/2701))
115+
116+
Sometimes one get warnings from the compiler about some code snippets. Previously it was needed to switch to `:power` mode in order to enable a full output of the warnings. Changing some other variables required a switch to the `:power` mode too. With this release it is possible to do this directly with the `:settings` command. By prefixing an property with a `+` sign the setting is enabled, an `-` disables it:
117+
118+
```scala
119+
scala> new BigInt(java.math.BigInteger.TEN)
120+
res16: scala.math.BigInt = 10
121+
122+
scala> new BigInt(java.math.BigInteger.TEN) {}
123+
warning: there were 1 deprecation warning(s); re-run with -deprecation for details
124+
res17: BigInt = 10
125+
126+
scala> :settings +deprecation
127+
128+
scala> new BigInt(java.math.BigInteger.TEN) {}
129+
<console>:11: warning: inheritance from class BigInt in package math is deprecated: This class will be made final.
130+
new BigInt(java.math.BigInteger.TEN) {}
131+
^
132+
res18: BigInt = 10
133+
134+
scala> :settings -deprecation
135+
136+
scala> new BigInt(java.math.BigInteger.TEN) {}
137+
res19: BigInt = 10
138+
```
139+
140+
### `:edit` ([#2706](https://github.com/scala/scala/pull/2706))
141+
142+
This command allows a user to change already inserted commands. It is possible to change a range of lines. The syntax is:
143+
144+
- line number: 123 (only the line 123)
145+
- range: 123-130 (the lines 123 to 130)
146+
- offset: 123+7 (the lines 127 to 130)
147+
- remaining: 123- (all lines up from 123)
148+
- previous: -10 (the last ten lines)
149+
150+
The environment variable EDITOR is used to specify the editor to invoke. If EDITOR is not set or if the `:line` command is used instead of `:edit`, the selected text is added to the end of the history in order to allow fast editing by navigating through the history with the arrow keys.
151+
152+
```scala
153+
scala> :history
154+
<snip>
155+
2505 val i = 5
156+
2506 val j = 7
157+
2507 :history
158+
159+
scala> :edit 2505+2
160+
+val i = 5
161+
+val j = 12
162+
i: Int = 5
163+
j: Int = 12
164+
165+
scala> :history
166+
<snip>
167+
2505 val i = 5
168+
2506 val j = 7
169+
2507 :history
170+
2508 :edit 2505+2
171+
2509 val i = 5
172+
2510 val j = 12
173+
2511 :history
174+
```
175+
176+
### `:javap` ([#1880](https://github.com/scala/scala/pull/1880))
177+
178+
The command to dissasemble Java output got some bugfixes and new features. It is now possible to filter out members of classes:
179+
180+
- `Bar#foo` filters out all `foo` members
181+
- `Bar#` filters out all `apply` members
182+
- `-fun Bar#foo` filters out all anonfuns of `foo` members
183+
- `-fun Bar#` filters out all anonfuns of `apply` members
184+
- `-app Bar` filters out `Bar.delayedInit`
185+
186+
Example usage:
187+
188+
```scala
189+
scala> object Foo { def apply = 10; def bar = 5 }
190+
defined object Foo
191+
192+
scala> :javap Foo#bar
193+
public int bar();
194+
flags: ACC_PUBLIC
195+
Code:
196+
stack=1, locals=1, args_size=1
197+
0: iconst_5
198+
1: ireturn
199+
LocalVariableTable:
200+
Start Length Slot Name Signature
201+
0 2 0 this LFoo$;
202+
LineNumberTable:
203+
line 7: 0
204+
205+
scala> :javap Foo#
206+
public int apply();
207+
flags: ACC_PUBLIC
208+
Code:
209+
stack=1, locals=1, args_size=1
210+
0: bipush 10
211+
2: ireturn
212+
LocalVariableTable:
213+
Start Length Slot Name Signature
214+
0 3 0 this LFoo$;
215+
LineNumberTable:
216+
line 7: 0
217+
```
218+
219+
### `:kind` ([#2340](https://github.com/scala/scala/pull/2340))
220+
221+
Because Scala supports working with higher kinded types, we might want to be able to inspects kinds as well as types:
222+
223+
```scala
224+
scala> :kind Either
225+
scala.util.Either's kind is F[+A1,+A2]
226+
227+
scala> :kind -v Either
228+
scala.util.Either's kind is F[+A1,+A2]
229+
* -(+)-> * -(+)-> *
230+
This is a type constructor: a 1st-order-kinded type.
231+
232+
scala> :k -v Int
233+
scala.Int's kind is A
234+
*
235+
This is a proper type.
236+
237+
scala> :k -v scala.Function1
238+
scala.Function1's kind is F[-A1,+A2]
239+
* -(-)-> * -(+)-> *
240+
This is a type constructor: a 1st-order-kinded type.
241+
```
242+
243+
For a more detailful explanation on the notation see the discussion of the PR.
244+
245+
### JSR-223 Scripting Engine support ([#2206](https://github.com/scala/scala/pull/2206))
246+
247+
It is now possible to use the REPL as a Scripting Engine:
248+
249+
```scala
250+
scala> import javax.script.ScriptEngineManager
251+
import javax.script.ScriptEngineManager
252+
253+
scala> import scala.tools.nsc.interpreter.IMain
254+
import scala.tools.nsc.interpreter.IMain
255+
256+
scala> val e = new ScriptEngineManager().getEngineByName("scala")
257+
e: javax.script.ScriptEngine = scala.tools.nsc.interpreter.IMain@7debe95d
258+
259+
scala> e.put("n", 10)
260+
n: Object = 10
261+
262+
scala> e.eval("1 to n.asInstanceOf[Int] foreach println")
263+
1
264+
2
265+
3
266+
4
267+
5
268+
6
269+
7
270+
8
271+
9
272+
10
273+
res4: Object = null
274+
```

0 commit comments

Comments
 (0)