Tuesday, September 13, 2011

Scala and Random musings

I've always been a programming language geek. I love learning new programming languages, though C++ and C# (and to a lesser extent Java and Ada) have paid my mortgage over the years.

I'm more of a fan of statically typed languages, but when I have to, I can feel my way around Python. Windows Power Shell is also a fantastic dynamic language that doesn't get the credit it deserves. I'll admit the syntax can take a little getting used to, and if you don't use it frequently, you will forget a lot of it.

For a reason I can't remember now, I recently read about Scala. (Wikipedia) I'd heard its name thrown around for a few years, but never really looked much into it. If a language is not available for production use for me, I have a little less incentive to check it out. Scala's primary platform is the JVM, and it can also run on Android's Dalvik without too much fuss. There is a .Net variant that uses the Java-.Net bridge/abstraction framework IKVM, but it is apparently not quite ready for prime time.

If you are at all interested in programming languages, I suggest you at least take Scala for a test drive, it's heavily influenced by Java and C# with functional programming elements in there.

I've kicked off development of two new Android apps, small utilities and I intend to publish for free, but I'm not getting specific yet because I could easily can them this early. I'm writing them in Scala, so it's an educational experiance for me.

Progress has been moving at glacial pace. You can see Java's roots in the language, and you can access Java's(or Android's) libraries, but some of the changes make it very awkward. When you are very accustomed to a particular language and write code in a new one, it is hard to not "code with an accent". (I didn't come up with that metaphor, I don't remember where I read it, maybe Coding Horror). For example, one of the features unique to Scala from others in the curly brace family is the "punctuation inference". Semicolons and sometimes even the curly braces are optional if the compiler can figure out the intent. That's akin to blasphemy to a seasoned C++ programmer, I've had to replace the ';' key on my favorite keyboard twice over the years. Ok that was a lie.

Here's an example how different the optional punctuation and qualifiers can make the code:

//Java  (and happens to be legal C# too)
public class Foo
  public int bar(int input) 
    return input + 42;

class Foo //note that this type is implicitly public.
  def bar(input : Int) : Int = input + 42

//also legal Scala, but more verbose with optional punctuation added.
class Foo //in fact, you can't explicitly declare it public.
  def bar(input : Int) : Int =
    return input + 42;

So, Scala is close enough to C++/C#/Java that I can pick up a lot quickly, but the "Zen" or conventions of Scala are to use the more terse style, which transforms the prose to something less familiar.

That example was very superficial, and highlights mostly stylistic differences, the real differences are Scala's ability to build domain specific languages, which can be overkill in a small or medium sized application. The thing about Scala that brings a smile to you is the way that you can use the "Java" way of doing things for small apps with little "archtecture" and switch to a domain model and language for larger architectures.

Some additional highlights of Scala are full operator overloading (beyond what C++ and C# can do!), message passing mutlithreading mechanisms., tuples, implicit static typing.

I've always wanted to design my own programming language, though who knows if it will ever happen. Like most languages it would be largely ideas that came before it packaged up in a new way, with some new ideas.

Here's what my language would have:

  • probably very much C# baseline (It *is* my favorite language)
  • Add value parameters to generics, like C++ has for templates. Allow explicit and/or partial specialization.
  • Add some of Ada's strong typing, type constraints(eg range types), Local functions,
  • Add precondition, post-condition, and invariant contracts from Spec# (I implemented something like invariant in a unit testing framework using C++ macros at a prior job, man that was fun!)
  • Add functional programming elements a la F# or Scala, tuples(Wait, doesn't C# 4.0 have that?), and it's awesome operator overloading. 
  • The compilation pipeline would have an intermediate stage (possibly representing all the compilation unit as XML or a dom in memory) that you could define aspects against or using with some sort of reflection or introspection. This is vague, but some ideas for this are, Aspect oriented programming, or applying an overloaded set of operators to code constructs, in much the way that C#'s LINQ works on vastly different types of data. (though LINQ does that via function overloading and namespace resolution)
  • More Cowbell. Actually,  "Cowbell" would be the perfect name for it (please don't steal that, unless you implement my dream language exactly as I've described it here, in which case, thanks a million!)


No comments:

Post a Comment

I welcome you're thoughts. Keep it classy, think of the children.