File format problems using Visual Studio to edit Scala code

Published by on
Tags:

Noticed a weird thing when I tried to use Visual Studio 2008 to edit some scala. First of I created a solution containing a C# class library. Yeh I know this won't compile but I am just using Visual Studio to edit the files and manage the files. So I have my solution like so:

 

So I went to add a file, so I right clicked on the project and select Add New Item and selected Text File:

So I wrote some simple code and tried to compile it (using the command prompt) and got this error:

So I tried again but this time going through the file menu to create the file, and again select Text File:

I entered the same code and ran it again and this time it worked:

So I thought there must be something weird going on with the encoding,  but when I checked both were UTF-8. So I check the file size, and strangly there is a 3 byte difference, the first file was 129 bytes and the second file is 126 bytes.

I do not know enough about file formats to shed any light on what is causing this, so for now I will just be using the file menu method to add files to my project. Maybe someone who knows more about these things could shed some light on this.

 

 

Lift Framework simple commands

Published by on
Tags:

I am starting to play around with the Lift Framework which my friend Tim P is involved in (and keeps telling me about at every pub trip!). So there are a couple of commands to get a project created and started with Lift. Once you have the everything installed (see the Lift site for details on installation prerequisites) to create a new project enter the command:

mvn archetype:generate -DarchetypeCatalog=http://scala-tools.org/

You will get asked a few questions by the installer and then you should have your project setup.

To run the new project navigate to the root directory of the project and type:

mvn jetty:run

This will run the site and start a small web server. You may notice that it does a lot of checks and downloads any components you are missing. If want to skip this check add -o to the end. This will run it in offline mode:

mvn jetty:run -o

To stop the server running hit Ctrl-c on a Windows machine (you will have to ask Tim about other systems).

To recompile to project without stopping the serve, remember to open a different console window:

mvn compile

No I have this a reference I shouldn't need to keep looking it up Smile

 

Scala Operator Magic

Published by on
Tags:

I have been looking a learning Scala and the other day I came across this code example on the Scala site. Initially this took a little time to understand so I thought I would blog what is going on so that I don’t forget and hopefully I save someone else some time.

So the code example I am using is below, it’s a slight modification of the code example found on the Scala site:

    1 object complexOps extends Application {

    2   class Complex(val re: Double, val im: Double) {

    3     def + (that: Complex) =

    4       new Complex(re + that.re, im + that.im)

    5     def - (that: Complex) =

    6       new Complex(re - that.re, im - that.im)

    7     def * (that: Complex) =

    8       new Complex(re * that.re - im * that.im,

    9                   re * that.im + im * that.re)

   10     def / (that: Complex) = {

   11       val denom = that.re * that.re + that.im * that.im   

   12       new Complex((re * that.re + im * that.im) / denom,

   13                   (im * that.re - re * that.im) / denom)

   14           }

   15     override def toString =

   16       re + " "+im

   17   }

   18 }

The code is quite simple, it just allows us to do basic operations on Complex objects. So lets add some code and start to get it working. First we need to define some Complex objects that contain some values, add this code after line 17 in the above example:

   18   var com1 = new Complex(1,2)

   19   var com2 = new Complex(3,4)

Now if we add a line to print out some data to the console:

   20   println(com1+com2)

Now save (as complexOps.scala) and compile this code:



And run it like so:



Ok so how did we get 4.0 and 6.0 as a result? Well lets start by looking at the print line:

   20   println(x+y)

What we are telling Scala to do is to use the add operator on the first Complex object, we are not concatenating the toString methods of each object;  we are calling these lines of code:

    3     def + (that: Complex) =

    4       new Complex(re + that.re, im + that.im)

This tells us that the operator can be applied when the other object is also a Complex object. The other object is stored in the variable "that" and we can now make use of it within the method body. As you can see from the line extract we create a new Complex object that use the sum of the original two objects as it's values.The new complex object is then returned, the final step is then to call the toString method. This as you can see will output the two values of our new Complex object to the screen. Pretty cool.

But thats not the end, we can overload this!!! oh yes. Lets define a second class:

   21   class MoreComplex(val re: double){

   22 

   23   }

As you can see really simple! Ok now on our original object lets add another definition for the add operator below the original add operator:

    2   class Complex(val re: Double, val im: Double) {

    3     def + (that: Complex) =

    4       new Complex(re + that.re, im + that.im)

    5     def + (that: MoreComplex) =

    6       new Complex(re+that.re, im)

Now lets create an instance of more complex and print out the result, adding it one of our original Complex objects:

   30   var com3 = new MoreComplex(5)

   31   println(com1+com3)

Ok lets compile this and see what we get as output:



Excellent the correct result! How awesome. With the example the Complex objectmust be on the left of the operator, if you swap the over you will receive and error because the MoreComplex class does not have any operators defined to handle a Complex object.

Have a play with the code and see what the other operators do!

 

All blogs tagged with 'scala'

Projects

Have you read?

Sitecore 6 XHTML Validation and the Embed HTML Element



Blogs by date