The Artima Developer Community
Sponsored Link

Heron-Centric: Ruminations of a Language Designer
Designing the Heron Standard Library
by Christopher Diggins
August 3, 2005
Summary
I am in the middle of implementing the Heron standard library, and I have found two techniques for implementing container concepts. One is more object-oriented and the other is more generic. Which should I use?

Advertisement

Below is an example concept from the Heron standard library. In order to understand the code you should take note that: a) the question mark represents an assertion statement, b) define is for defining a type-alias, which may be overridden

    concept Stack[T : type] {
      contract {
        is_empty() : bool;
        top() : value_type {
          before {
            ?(!is_empty());
          }
        }
        pop() {
          before {
            ?(!is_empty());
          }
        }
        push(value_type x) {
          after {
            ?(!is_empty());
          }
        }
      }
      types {
        define value_type : T;
      }
    }
The above code is pretty straightforward, but it behaves more like an interface than a concept. The following concept is more sophisticated as it doesn't require a template parameter.
    concept Stack {
      contract {
        is_empty() : bool;
        top() : value_type {
          before {
            ?(!is_empty());
          }
        }
        pop() {
          before {
            ?(!is_empty());
          }
        }
        push(value_type x) {
          after {
            ?(!is_empty());
          }
        }
      }
      requires {
        value_type : Any;
      }
    }
The second version of the Stack concept requires that a class which models (implements) the concept provides its own definition of value_type. Any is an unbounded constraint on the set of types accepted (it also could have been a concept).

My plan is to support both techniques in Heron, but I have to choose one for the standard library. I am leaning towards implementing the library using the first technique, at least for now.

Talk Back!

Have an opinion? Readers have already posted 13 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