The Artima Developer Community
Sponsored Link

Heron-Centric: Ruminations of a Language Designer
Thinking out loud about Concepts
by Christopher Diggins
August 6, 2005
Summary
Here is an interesting problem with making C++ style concepts a real type in a programming language.

Advertisement

In Heron you are supposed to be allowed to be able to write a concept as follows:

  concept Stack {
    contract {
      push(value_type x);
      pop() : value_type;
      is_empty() : bool;
    }
    requires {
      value_type : Any;
    }
  }
This should make immediate sense to most C++ programmers (hopefully). But now here is an interesting predicament:
  void MyFxn(Stack& x, Stack& y) {
    y.push(x.pop()); 
  }

  int main() {
    MyStack[int] s1;
    MyStack[string] s2;
    MyFxn(s1, s2); // compiler accepts this happily 
  }
What I don't like about this is that the compiler can't catch the error until inside of the implementation of MyFxn. The current implementation I am looking at would make things even worse, because it might end up being a runtime error ... yuck!

One solution for the programmer is to use the C++ way of doing things:

  void MyFxn[T : Stack](T& x, T& y) {
    y.push(x.pop()); 
  }
This enforces the fact that both parameters must be precisely the same, but leaves the flexibility that they are constrained to the Stack concept. I was really hoping to find a more elegant solution than this, in fact I was hoping to be able to entirely avoid template functions altogether in the languauge. Any thoughts or comments?

Talk Back!

Have an opinion? Readers have already posted 12 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-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us