The Artima Developer Community
Sponsored Link

Heron-Centric: Ruminations of a Language Designer
Metaprogramming on Steroids: Overloaded Type Operators
by Christopher Diggins
September 24, 2005
Summary
Every once in a while I come up with a good idea for Heron, and then inevitably Vesa Karvonen tells me that it existed in ML since the dawn of time. Maybe this time will be different?

Advertisement

Writing type operators is as natural as breathing for physicists and engineers.

newton = kilogram * (meters / seconds**2);
So it seems to me to be perfectly reasonable to allow programmers to overload operators for types as well.

Another application is when emulating Backus-Naur Form (BNF) as I do in the YARD library. YARD is a C++ metaprogramming parsing engine I use in both HeronFront and HeronScript. YARD constructs a R-D parser at compile time by using metaprogramming techniques. In YARD emulation of a BNF grammar such as :

A ::== B* C
B ::== C | D
would look like:
struct A : 
  bnf_and<
    bnf_star<B>,
    C
  >
{ };

struct B :
  bnf_or<
    C,
    D
  >
{ };
As you can see this is not very pretty but in C++ it is probably as good as I can do, while still constructing the parser at compile-time. In Heron if I enable operator overloading for types, then I could emulate the BNF grammar as below:
alias A : *B & C; 
alias B : C | D;
which is quite a bit more natural.

This is actually only scratching the surface of possibilities of operator overloading on types. Eventually constant values like 42, 'q', "hello", 3.14 will all be valid types along with code blocks { ... }. I'll leave it to you to dream up the possibilities that this implies for now (hint: this makes macros obsolete).

Talk Back!

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