The Artima Developer Community
Sponsored Link

Heron-Centric: Ruminations of a Language Designer
Eureka: Forget Interfaces what I really need are Traits!
by Christopher Diggins
December 29, 2005
Summary
I was working on specifying separate mixin and interface types, when I remembered Scala Traits / Ruby Modules.

Advertisement

It just dawned on me as I was working on a mixin specification, that the Ruby and Scala solutions are far more elegant than what I was moving towards. Thanks to Keith Gaughan for jogging my memory on this point.

I am less familiar with Ruby Modules ( http://www.rubycentral.com/book/tut_modules.html ) , but as far as I can tell it is pretty much the same thing as the trait in Scala ( http://scala.epfl.ch/intro/traits.html ) : essentially a stateless class which can be used as a mixin, and as an interface (I welcome any corrections).

In one fell swoop this solves several of my problems. I was heading towards several different constructs, which in the end can all be achieved using traits.

Here is a simple interface implemented using a trait:

  trait StackInterface[T : type] {
    signature {
      push(T x);
      pop() : T;
      is_empty() : bool;
    }  
  }
One thing very exciting for me about traits is that they can be used to implement contracts:
  trait StackContract {
    signature {
      push(T x) {
        override;
        assert(!is_empty()); 
      } 
      pop() : T {
        assert(!is_empty()); 
        override;
      }
    }
  }
However, with one small change to the language specification: _before and _after member macros (incidentally something similar existed in the original cfront tool by Stroustrup), you can implement an AOP style advice class.
  trait LoggingAdvice {
    macros {
      _before = {
        writeln("calling function " + _function_name);
      }
    }
  }
So now you can design an extremely powerful and flexible class using a composition of mixins.
  typedef Stack[type T] = 
    mixin(LoggingAdvice, 
      mixin(StackContract[T], heron::stack[T]));
That is only the tip of the iceberg, conditional mixin statements would make things even more exciting:
  static const bool bLogging = true; 
  static const bool bVerifyContracts = true; 

  typedef Stack[type T] = 
    mixin_if(bLogging, LoggingAdvice, 
      mixin_if(bVerifyContracts, StackContract[T], heron::stack[T]));
How do you like them apples?

Talk Back!

Have an opinion? Readers have already posted 21 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Christopher Diggins adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Christopher Diggins is a software developer and freelance writer. Christopher loves programming, but is eternally frustrated by the shortcomings of modern programming languages. As would any reasonable person in his shoes, he decided to quit his day job to write his own ( www.heron-language.com ). Christopher is the co-author of the C++ Cookbook from O'Reilly. Christopher can be reached through his home page at www.cdiggins.com.

This weblog entry is Copyright © 2005 Christopher Diggins. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use