Intellij Scala worksheet Run type difference explain - scala

In Intellij Scala Worksheet support, what is the difference between the Run types i.e PLAIN vs REPL ?

Plain evaluation model compiles the whole worksheet in one go before evaluating expressions, whilst REPL evaluation model evaluates each expression on the go before moving to the next one.
Adding an expression in REPL mode evaluates incrementally just that new expression, whilst in Plain mode it would re-interpret the whole worksheet from the beginning.
An example where the difference matters is when defining companion objects. Similarly to how in Scala REPL proper we have to use :paste command to define companion, in IntelliJ Scala Worksheet we have to use Plain run type.

REPL mode as it says READ EVALUATE PRINT LOOP is kind of interpreter i.e. each expression will be evaluated after to move to next line.. It is generally used to make quick logic checks.
while in worksheet mode you need to make an object or class.. worksheet is the traditional OOPS way like we do in java and whole file is compiled in one go.

Related

Error: value is not a member of object using Scala on the shell

Hello I have this scala object and I want to run the code in sample function using the shell with scala:
object SampleObject{
def sample(){
val data = Array(1, 2, 3, 4, 5)
data.foreach(println(_))
}
}
What I do is this
scala> :load /Users/username/Desktop/Cli.scala
Loading /Users/username/Desktop/Cli.scala...
defined object SampleObject
But if I run this
scala> SampleObject.sample
This is what I ge this
<console>:92: error: value sample is not a member of object SampleObject
SampleObject.sample
Why does this happen? There's a problem with the commands :load and -i, it's like you can't access to the objects members. Any suggestion to make it work?
UPDATE1:
If I run the code above neither doing load or -i but by writing it directly on the shell everything works properly.
UPDATE2:
Still not working...
The problem is that line break before the brace. You can fix this either of two ways:
Use :paste instead of :load
Put the brace on the same line as the object name
Your original question said object SampleObject{ but it turns out that is not what your actual file contains, as your update 2 shows. Since :load works line-by-line, just as if you were typing at the REPL, when the interpreter sees object Cli it's done: that is a perfectly valid, complete Scala declaration of a (not terribly interesting) object. The opening brace on the next line will start a new block that is unrelated to the object you've just defined.
This is an area where the interactive REPL behaves differently from the compiler, which will allow a single newline before the brace (see the Scala language specification for specifics).

Folding of Scala imports in Vim

I'm trying to have my Vim setup automatically fold import statements when I open a Scala file. This is similar to what Intellij does when you open a file. I currently have the following lines in my .vimrc file from the wiki.
set foldmethod=syntax
set foldenable
syn region foldImports start=/(^\s*^import\) .\+/ end=/^\s*$/ transparent fold keepend
However when I open a .scala file it doesn't fold the imports but the body of objects. I am also using the vim-scala plugin. Thanks for the help!
You were pretty close to getting this to work. There are a few funky factors at play that we should consider.
setting foldmethod to syntax (btw this is not documented on learn Vimscript the Hardway..so :help foldmethod was key to figure this out)
SYNTAX fold-syntax
A fold is defined by syntax items that have the "fold" argument.
|:syn-fold|
The fold level is defined by nesting folds. The nesting of folds is
limited with 'foldnestmax'.
Be careful to specify proper syntax syncing. If this is not done
right, folds may differ from the displayed highlighting. This is
especially relevant when using patterns that match more than one line.
In case of doubt, try using brute-force syncing:
:syn sync fromstart
The main thing to note is the sync fromstart this is a useful helper if you have regex that would match throughout the file and only want to catch the header. In your case you should be able to ignore this but just something to be aware of.
top down regex scanning
Since the import block is fairly predictable we can simplify the start and end to look something like this:
syn region foldImports start="import" end=/import.*\n^$/ fold keepend
Since the region is just looking for some string to start the matching on we can just use "import"(or /import/) and then for the end value we want to use a little bit more carefully crafted statement. The key is that we want have the end be the last line of the import with a blank line following it (/import.*\n^$/)
Hopefully that does the trick for you (I do not work with scala so you may have to adjust the regex a bit as needed)

How to get jdb-like features (setting breakpoints or displaying vars) in sbt or REPL for Scala?

Rather than opening up jdb is there a way to get similar functionality within the repl or sbt session?
The features I am seeking:
ability to define breakpoints:
:bp mySource.scala:79 // stop at line 79 mySource.scala
:bp org.mycompany.MyClass:14 // stop at line 14 of myClass (no idea if anyone supports such a thing..)
print out vars
:p myList
evaluate expressions including case statements and bonus points for closures
You can't keep a good idea down -- see the thread A non-runaway-REPL?:
Maybe it could also use a "safe" mode where it forks a JVM. While
we're at it, it's time for a debug command.
I don't know if the command should be called :forked, :borked, or simply :wtf.

Scala REPL fails to autocomplete methods that comes from implicit conversion

if I write in scala 2.10 REPL (interactive Scala shell):
"""\w""".
And press TAB it gives me:
+ asInstanceOf charAt
codePointAt codePointBefore
codePointCount compareTo compareToIgnoreCase
concat contains ....
However, .r is missing. When I put the same string into eclipse, it offers me .r as well. The same is true if I insert import scala.util.matching._ before.
Why REPL is not offering all possibilities?
Even bigger problem REPL has if i try to work with unicode, e.g. I write:
"""\p{L}""".
and press TAB
it gives me error:
scala> """\p{L}""".<console>:1: error: unclosed multi-line string literal
"""
^
Again, it works fine in Eclipse.
Is REPL so buggy, or am I missing something?
Yes, r is missing, but if you write """\w""".r and press enter it nevertheless works res0: scala.util.matching.Regex = \w. Having tab autocompletion for r seems not really neccessary.
The unicode issue is probably caused by java. You can explicitly request UTF-8 if you pass -Dfile.encoding=UTF-8 to java. Here is a post which describes how to do it.
If you use Eclipse, I can reccommend the Scala worksheet plugin which is a very good repl replacement.
The REPL only displays fields and methods of the object, while .r is only available through an implicit conversion (augmentString in scala.Predef) which turns it into a StringOps. There is probably no reason for this besides the fact that it would need to be implemented and nobody got around to doing it. You can still call .r on this, of course.
The Scala IDE is smart enough to resolve implicits, which is why you can see it there.

How to run an external file from within the scala interactive interpreter (REPL)?

I have a file with several lines of scala code -- imports, list value assignments, etc. that I often use to initialize some things when using the REPL.
Currently I just open up the file in a text editor and copy-and-paste it into the REPL, but is there a way to do it by calling the external file in a more direct manner so I don't have to copy-and-paste every time?
In some interactive database tools like SQL Plus, it is done by typing #filename at the prompt. Is there something similar in the Scala REPL? Preceding the filename with # doesn't work, eval doesn't work either.
Type:
:help
and you see, that
:load <path> load and interpret a Scala file
solves your problem.
In some circumstances, pasting the code might be preferable though, but then
:paste
might be your friend then. It helps inserting a whole block without feedback, line by line, until you hit Ctrl + D. In some cases this is significant for the code interpretation.