Sponsored Link •
|
Summary
The next Heron release will be replacing the interface construct with the concept construct.
Advertisement
|
The next version of Heron will be introducing the "concept" construct which will supercede the interface construct. A Heron concept is very much like the C++ standard library notion of a concept, except that in Heron it will be an actual type which can be used as a variable declaration or function arguments.
A concept is a set of function declations, contract clauses, and local type declarations which describe a class. A concept variable or parameter is compatible with any class which models the concept. In order for a class to model a concept it is only has to provide public functions which match the signatures of the functions in the contract, and provide public types which have the appropriate name and match the requirements.
Concepts can be used to describe template parameters, but can also be used simply as function parameters. For example a simple concept is the Array concept.
concept Array[A : type] { contract { GetAt(int n) : A { pre { n >= 0; n < Count(); } } SetAt(int n, A x) { pre { n >= 0; n < Count(); } post { GetAt(n) == x; } } Count() : uint; } types { value_type : type; } }
If you want to write a function which can accept any object which models the Array concept you can write:
output_array(Array a) { for (int i=0; i<a.Count(); ++i) { println(a.GetAt(i)); } }
A concept also provides support for Programming with Contracts by providing a mechanism for expressing contractual clauses. The pre section delineates the set of contractual preconditions, while the post section delineates the contractual postconditions.
For more information on concepts visit I strongly recommend The SGI STL Introduction. For more on the shortcomings of C++ concepts you can visit the Boost Concept Checking library.
I'd like your feedback, do you approve or disapprove of the the concept construct? Does it make sense what I am trying to do? Are the advantages of the approach obvious?
Have an opinion? Readers have already posted 14 comments about this weblog entry. Why not add yours?
If you'd like to be notified whenever Christopher Diggins adds a new entry to his weblog, subscribe to his RSS feed.
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. |
Sponsored Links
|