Scriptcs and multiline REPL!

A while ago, we added a killer REPL feature to the scriptcs project. Recently, we followed it up with adding support for one of the most popular requested features – multiline constructs for the REPL.

It’s not yet available in the stable version available via Chocolatey or on the master branch on Github, but you can get a taste of how it looks and feels by installing the nightly build or building from dev branch directly.

Let’s have a quick look together.

Installing scriptcs nightlies

To get scriptcs nightlies you simply need to install scriptcs from the nightly build feed, hosted at Myget.

You install the same way as you’d do normally do it – with Chocolatey, except you point the source to Myget.

And if you haven’t used scriptcs before – make sure to read the readme at Github.

Understanding scriptcs multiline REPL

There are three types of characters that allow you to write mutliline construct:

  • ( – as it’s missing a matching )
  • [ – as it’s missing a matching ]
  • { – as it’s missing a matching }

If you try to execute a line with a missing bracket, we will keep buffering the code (as under the hood we will detect a compilation exception) until the code is correct.

As mentioned, multiline REPL in scriptcs is based on the idea of compilation exception – this is a great idea suggested by Tim Erickson, and is in line with the behavior of C# interactive window.

One thing that needs to be added though, is a way to flush the buffer with some command – sometimes you may open a multiline construct by accident, and then you will probably want to have a #cls or similar command to allow you to exit.

Putting it to work

You will probably find this functionality very handy when initializing objects and defining classes.

Notice we keep buffering, since the first bracket is still missing it’s closing partner. The input character at the beginning of the line, indicates whether we are still within the context of the previous line (even though we are physically on the next line) or not.

Now, after the closing bracket, everything is fine! You can continue working with the REPL and you have the declared classes at your disposal now.

You can use the multiline REPL in many other ways too – for example to create method definitions.

And even method invocations (after all, round brackets are also supported).

Summary

I’m convinced that scriptcs REPL is a very useful tool for C# – if you haven’t tried it yet, you definitely have to! As such, I hope you will enjoy our new little functionality for the REPL!

And keep your eyes out for more, there are a still few gems in the pipeline for REPL! :)

Share the post!