{"id":74,"date":"2011-07-26T15:59:55","date_gmt":"2011-07-26T15:59:55","guid":{"rendered":"http:\/\/www.benbarefield.com\/blog\/?p=74"},"modified":"2011-07-26T16:01:50","modified_gmt":"2011-07-26T16:01:50","slug":"getting-started-with-scala-net","status":"publish","type":"post","link":"https:\/\/www.benbarefield.com\/blog\/2011\/07\/26\/getting-started-with-scala-net\/","title":{"rendered":"Getting Started with Scala.Net"},"content":{"rendered":"<p>Reporting from the <a href=\"https:\/\/sites.google.com\/site\/programmingsummercamp\/\">Programming Summer Camp<\/a> in Crested Butte, Colorado. This has got to be one of the better places in the world to code. We had a great warm up bike ride this morning, then settled in for an afternoon of coding. I spent my time figuring out <a href=\"http:\/\/www.scala-lang.org\/node\/10299\">Scala.Net<\/a> with the goal of calling some C# code from Scala.<\/p>\n<p>The Scala.net compiler is in an SVN repository located at: <a title=\"http:\/\/lampsvn.epfl.ch\/svn-repos\/scala\/scala-experimental\/trunk\/bootstrap\" href=\"http:\/\/lampsvn.epfl.ch\/svn-repos\/scala\/scala-experimental\/trunk\/bootstrap\">http:\/\/lampsvn.epfl.ch\/svn-repos\/scala\/scala-experimental\/trunk\/bootstrap<\/a><\/p>\n<p>I started by writing a small C# class library. I created one class with a couple of methods. This is pretty standard \u201cgetting up to speed\u201d stuff:<\/p>\n<div id=\"codeSnippetWrapper\" style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;\">\n<div id=\"codeSnippet\" style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">\n<pre style=\"text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #0000ff;\">using<\/span> System;<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #0000ff;\">namespace<\/span> Echidna<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">{<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">    <span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">class<\/span> Speaker<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">    {<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">        <span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">void<\/span> Speak()<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">        {<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">            Console.WriteLine(<span style=\"color: #006080;\">\"Hi there.\"<\/span>);<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">        }<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">        <span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">void<\/span> Speak(<span style=\"color: #0000ff;\">string<\/span> name)<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">        {<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">            Console.WriteLine(<span style=\"color: #0000ff;\">string<\/span>.Format(<span style=\"color: #006080;\">\"Hi, {0}, glad you could join us.\"<\/span>, name));<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">        }<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">    }<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">}<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<p>Next, I dove into the Scala. For this experiment all I needed was an object whose Main created an instances of my C# class and called its methods:<\/p>\n<div id=\"codeSnippetWrapper\" style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;\">\n<div id=\"codeSnippet\" style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">\n<pre style=\"text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #0000ff;\">import<\/span> Echidna.Speaker<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">object Test {<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">    def main(args: Array[String]) {<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">        val speaker = <span style=\"color: #0000ff;\">new<\/span> Speaker()<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">        speaker.Speak<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">        speaker.Speak(<span style=\"color: #006080;\">\"Joe\"<\/span>)<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">    }<\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">}<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<p>I\u2019d like to note here that the import is a Scala import statement (not a C# include), so the class name needs to be included. I could also have written \u201cimport Echidna._\u201d to get everything from the Echidna namespace. The compiler error did not make it especially obvious that I had messed this up.<\/p>\n<p>With all my code built I could start compiling. Currently, there are two steps to create an executable, but there are plans to get rid of the second step. First, MSIL is created using the Scala.Net compiler:<\/p>\n<p>&lt;Scala.Net directory&gt;\\bin\\scalacompiler.exe<\/p>\n<p>-target:msil<\/p>\n<p>-Xassem-extdirs &lt;Scala.Net directory&gt;\\bin<\/p>\n<p>-Xshow-class &lt;Scala entry class&gt;<\/p>\n<p>-Xassem-path &lt;Dependent Assemblies&gt;<br \/>\n&lt;Scala files&gt;<\/p>\n<p>This is the bare minimum required to compile the Scala into MSIL. I inserted the new lines for ease of reading and to make it more obvious what is going on. Notes:<\/p>\n<ul>\n<li><span style=\"color: #333333;\">&lt;Scala.Net directory&gt; should be the directory that you pulled the SVN repository into<\/span><\/li>\n<li>&lt;Scala entry class&gt; is the Scala class that should serve as the entry point for your program. In my case it was \u201cTest\u201d<\/li>\n<li>&lt;Dependent Assemblies&gt; are the .Net assemblies your Scala program relies on. In my case this was \u201cEchidna.dll\u201d<\/li>\n<li>&lt;Scala files&gt; are your Scala source files. In my case it was \u201csimple.scala\u201d<\/li>\n<\/ul>\n<p>Next, the MSIL needs to be turned into an executable using ilasm. I opened a Visual Studio command prompt so that ilasm was in my Path:<\/p>\n<p>ilasm Test.msil<\/p>\n<p>The last problem I ran into was that the executable couldn\u2019t find scalalib.dll, so I copied it out of the directory where the compiler is (the same as &lt;Scala.Net directory&gt;.) After this, my executable ran perfectly.<\/p>\n<p>More information on the compiler and it\u2019s capabilities (for example, it does not currently support .Net generics) can be found at: <a title=\"http:\/\/lamp.epfl.ch\/~magarcia\/ScalaNET\/2011Q2\/PreviewScalaNET.pdf\" href=\"http:\/\/lamp.epfl.ch\/~magarcia\/ScalaNET\/2011Q2\/PreviewScalaNET.pdf\">http:\/\/lamp.epfl.ch\/~magarcia\/ScalaNET\/2011Q2\/PreviewScalaNET.pdf<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reporting from the Programming Summer Camp in Crested Butte, Colorado. This has got to be one of the better places in the world to code. We had a great warm up bike ride this morning, then settled in for an &hellip; <a href=\"https:\/\/www.benbarefield.com\/blog\/2011\/07\/26\/getting-started-with-scala-net\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,28],"tags":[],"class_list":["post-74","post","type-post","status-publish","format-standard","hentry","category-net","category-scala"],"_links":{"self":[{"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/posts\/74","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/comments?post=74"}],"version-history":[{"count":1,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/posts\/74\/revisions"}],"predecessor-version":[{"id":75,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/posts\/74\/revisions\/75"}],"wp:attachment":[{"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/media?parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/categories?post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/tags?post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}