Monday, September 24, 2007

Script# Gottchas [1] - Thine Code Shall Compile

I've been toying with Nikhil's Script# for the past week.  There's a learning curve involved.  While the documentation is very helpful and covers almost all scenarios, it does have few issues.  For one, the screenshots in the documentation pertain to an old version.  And the other, it doesn't spoon-feed you!  [How we miss getting spoon-fed, uh?  Thank you, MSDN]...

Nikhil does a very good job of warning you that the Csharp code must compile.  But he does not, of course, tell you want would happen otherwise.

Look at the seemingly innocuous code below:

    1 using System;
    2 
    3 namespace NewNameSpace
    4 {
    5     public class Reverser
    6     {
    7         public static void Main(string[] args)
    8         {
    9             Console.WriteLine(Reverse(args[0]));
   10         }
   11 
   12         public static string Reverse(string str)
   13         {
   14             char[] chars = str.ToCharArray();
   15             Array.Reverse(chars);
   16             return new string(chars);
   17         }
   18 
   19     }
   20 }

The above code is valid C# [Of course ;)].   What is more interesting is that even the Script# compiler doesn't complain.

reverse

Here's the Javascript generated:

    1 Type.createNamespace('NewNameSpace');
    2 
    3 ////////////////////////////////////////////////////////////////////////////////
    4 // NewNameSpace.Reverser
    5 
    6 NewNameSpace.Reverser = function() {
    7 }
    8 NewNameSpace.Reverser.main = function(args) {
    9 

 

What the heck?  Where's the rest of the file!!!

My initial reaction is that Nikhil has forgotten to write a fileBuffer.Flush() somewhere ;) [from the guy who wrote a compiler?  Forget it :)]

Hidden in the recesses somewhere [I don't remember where] there was an ominous line warning that your code MUST compile before you can turn Script# compiler on it.

To cut the long story short, you need to compile your C# code with:

reverse2

Oops...

Technorati Tags:

1 comment:

Anonymous said...

Interesting to know.