754
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
\documentclass{article}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
\usepackage{../style}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
\usepackage{../langs}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
\usepackage{marvosym}
|
|
917
|
5 |
\usepackage{tcolorbox}
|
|
|
6 |
%% \usepackage[]{hvextern}
|
754
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
%cheat sheet
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
%http://worldline.github.io/scala-cheatsheet/
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
\begin{document}
|
|
1016
|
12 |
\fnote{\copyright{} Christian Urban, King's College London, 2025}
|
754
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
|
|
1016
|
14 |
\section*{Ammonite ``Quirks''}
|
|
917
|
15 |
|
|
1016
|
16 |
While Ammonite is great when it comes to additional features, such as
|
|
|
17 |
multiple main's and dynamically loading files, and in the past it was
|
|
|
18 |
a rock-solid piece of software written by one of my Scala-heroes
|
|
|
19 |
(Haoyi Li), it seems it has acquired some ``quirks'' when it
|
|
|
20 |
transitioned to Scala 3. Interestingly, the problems listed below only occur
|
|
|
21 |
inside the REPL. So running Scala-scripts on the command
|
|
|
22 |
line with for example
|
754
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
|
|
1017
|
24 |
\begin{lstlisting}[language={},numbers=none]
|
|
1016
|
25 |
$ amm cw02.sc
|
754
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
\end{lstlisting} %% $
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
\noindent
|
|
1016
|
29 |
works fine. Similarly, the original Scala REPL seems to not suffer
|
|
|
30 |
from these quirks. Unfortunately, this REPL does not allow you to
|
|
|
31 |
dynamically load files which might be relevant in later CWs.
|
754
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
|
|
1016
|
33 |
\subsection*{Problem with Implicits}
|
754
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
|
|
1016
|
35 |
In CW2 (and possibly others), if you use the template code and develop
|
|
|
36 |
your code inside the REPL, a problem arises in the line
|
754
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
\begin{lstlisting}[numbers=none,language=Scala]
|
|
1016
|
39 |
val KEYWORD : Rexp = "while" | "if" | "then"
|
|
|
40 |
\end{lstlisting}
|
754
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
|
|
1016
|
42 |
\noindent with the error
|
|
760
|
43 |
|
|
|
44 |
\begin{lstlisting}[numbers=none,language=Scala]
|
|
1016
|
45 |
-- [E008] Not Found Error: cmd17.sc:1:36 -----------------------------
|
|
|
46 |
1 |val KEYWORD : Rexp = "while" | "if" | "then"
|
|
|
47 |
| ^^^^^^^^^^^^^^^^
|
|
|
48 |
|value | is not a member of ammonite.$sess.cmd4.ALT.
|
|
|
49 |
|An extension method was tried, but could not be fully constructed:
|
|
|
50 |
|
|
|
|
51 |
| ammonite.$sess.cmd16.|()
|
|
|
52 |
|
|
|
|
53 |
| failed with:
|
|
|
54 |
|
|
|
|
55 |
| value |: <overloaded ammonite.$sess.cmd16.|> does not take
|
|
|
56 |
| parameters
|
|
|
57 |
|Compilation Failed
|
|
|
58 |
\end{lstlisting}%$\medskip
|
754
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
59 |
|
|
994
|
60 |
|
|
1016
|
61 |
\noindent\textbf{Fix:} The code runs absolutely fine when called on the
|
|
|
62 |
commandline. It also runs absolutely fine inside the standard Scala REPL.
|
|
|
63 |
If you prefer to use to use the Ammonite REPL, then you need to make the
|
|
|
64 |
following changes: (1) comment out the implicits below and change the
|
|
|
65 |
\code{$} to for example \code{&}%$:
|
|
994
|
66 |
|
|
1016
|
67 |
\begin{lstlisting}[numbers=none,language=Scala]
|
|
|
68 |
extension (s: String) {
|
|
|
69 |
def & (r: Rexp) = RECD(s, r)
|
|
|
70 |
//def | (r: Rexp) = ALT(s, r)
|
|
|
71 |
//def | (r: String) = ALT(s, r)
|
|
|
72 |
//def % = STAR(s)
|
|
|
73 |
//def ~ (r: Rexp) = SEQ(s, r)
|
|
|
74 |
//def ~ (r: String) = SEQ(s, r)
|
|
994
|
75 |
}
|
|
1016
|
76 |
\end{lstlisting}
|
|
994
|
77 |
|
|
1016
|
78 |
\noindent You need to then use \code{&} in the code for the
|
|
1017
|
79 |
\code{WHILE_REGS}.
|
|
|
80 |
|
|
|
81 |
\subsection*{Problem with Loading Dynamically Files}
|
|
|
82 |
|
|
|
83 |
There seems to be cache problems with loading dynamically files in the REPL.
|
|
|
84 |
For example in the file \code{thompson.sc}, the code for DFAs, NFAs etc is
|
|
|
85 |
loaded using:
|
|
|
86 |
|
|
|
87 |
\begin{lstlisting}[numbers=none,language=Scala]
|
|
|
88 |
import $file.dfa, dfa._
|
|
|
89 |
import $file.nfa, nfa._
|
|
|
90 |
import $file.enfa, enfa._
|
|
|
91 |
\end{lstlisting}%$
|
|
|
92 |
|
|
|
93 |
\noindent The menace is that all works perfectly in the first REPL session,
|
|
|
94 |
but if you close it and then re-open it for example the next day, you
|
|
|
95 |
receive the following error when you try to re-evaluate the same lines:
|
|
|
96 |
|
|
|
97 |
\begin{lstlisting}[numbers=none,language=Scala]
|
|
|
98 |
-- [E008] Not Found Error: ~/cfl-material/progs/automata/nfa.sc:50:2
|
|
|
99 |
50 | dfa
|
|
|
100 |
| ^^^
|
|
|
101 |
| value dfa is not a member of ammonite.$sess - did you mean $sess.nfa?
|
|
|
102 |
-- [E006] Not Found Error: ~/cfl-material/progs/automata/nfa.sc:6:18
|
|
|
103 |
6 |import $file.$ , dfa._
|
|
|
104 |
| ^^^
|
|
|
105 |
| Not found: dfa
|
|
|
106 |
|
|
|
|
107 |
| longer explanation available when compiling with `-explain`
|
|
|
108 |
Compilation Failed
|
|
|
109 |
\end{lstlisting}%$\medskip
|
|
|
110 |
|
|
|
111 |
\noindent\textbf{Fix:} The code again runs absolutely fine when called on the
|
|
|
112 |
commandline. But the REPL trips over. In this case, the trick with using the
|
|
|
113 |
Scala REPL does not work, because it does not support dynamically loading
|
|
|
114 |
files. However, one ugly fix is to cut-and-paste all the code into a
|
|
|
115 |
single file and then develop this one gigantic file. Or a slightly
|
|
|
116 |
less ugly solution is toi clear out the cache of ammonite. The cache
|
|
|
117 |
on my system is located in
|
|
|
118 |
|
|
|
119 |
\begin{lstlisting}[language={},numbers=none]
|
|
|
120 |
~/.ammonite/cache/3.0.3/
|
|
|
121 |
\end{lstlisting}
|
|
|
122 |
|
|
|
123 |
\noindent
|
|
|
124 |
I usually delete all files inside this directory and in this way force
|
|
|
125 |
ammonite to be in a consistent state.
|
|
994
|
126 |
|
754
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
127 |
\end{document}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
128 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
129 |
%%% Local Variables:
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
130 |
%%% mode: latex
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
131 |
%%% TeX-master: t
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
132 |
%%% End:
|