Two Ways to Design A Programming Language

Method One:

Have a legend of the field think deeply and make precisely reasoned arguments for features.

Method Two:

Look at code-gen features in IntelliJ and figure out how to avoid needing them for your language:

Implicit Type Casting

IntelliJ has a macro for the common pattern of checking the type of something and then immediately downcasting/crosscasting the expression to that type:

instanceof

Java:

  Object x = aMethodThatReturnsAnObject();
  if( x instanceof List ) {
    System.out.println( "x is a List of size " + ((List) x).size() );
  } 

Gosu:

  var x : Object = aMethodThatReturnsAnObject()
  if( x typeis List ) {
    // hey, you already told us x was a list.  Why make you cast?
    print( "x is a List of size ${x.size()}" )
  } 

Delegation

IntelliJ has a wizard to assist you in delegating all the implementations of an interface to a field:

delegation

Gosu:

class MyDelegatingList implements List {

  delegate _delegateList represents List

  construct( delegateList : List ) {
    _delegateList = delegateList
  }

  override function add( Object o ) : boolean {
    print( "Called add!" )
    return _delegateList.add( o )
  }

  // all other methods on List are automatically
  // delegated to _delegateList
}

I omit the java version out of respect for your eyes.

I should note, Gosu is the new name for GScript, our internal programming language


3 Comments on “Two Ways to Design A Programming Language”

  1. Doug says:

    So can I delegate to an anonymous subclass in place there, and override behavior in the sub class? Just curious.

  2. Daniel shchyokin says:

    Isn’t generic typing supposed to cure this?

    i.e. List= new ArrayList();

    List.add(a); //will throw an exception if A is not the right type….

    • Carson Gross says:

      Daniel,

      Lists don’t throw runtime exceptions if the wrong thing is added to them, due to erasure. But I’m not sure which problem you are referring to. If you are referring to typeis inference, the idea is just to make developers lives easier in a pretty-type-safe way.

      Can you clarify what you mean?

      Cheers,
      Carson


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 38 other followers