The Artima Developer Community
Sponsored Link

Heron-Centric: Ruminations of a Language Designer
Functions as Classes: Where did I steal it from?
by Christopher Diggins
June 26, 2005
Summary
I have been thinking about making functions and classes equivalent in Heron, but I can't figure out where I stole the idea from.

Advertisement

I have been playing with the Heron specification lately and I am looking at an interesting possibility: making functions and classes equivalent. I know this can't be an original idea, but I don't know which languages do this kind of thing. Hopefully someone out there can point me in the right direction? Take note that while this code resembles Scala, the big difference is that functions are classes, as opposed to objects.

In this hypothetical Heron a function becomes a class with an implicit public result field.

Here is what it might look like if Heron functions became classes:

unit heron.collections;

interface Sequence[T : type]
{
  // deferred function classes
  define Foreach[F[T]]();
  define Concat(x : self);

  // public type ( alias )
  define Element = T;

  // extension class
  define Count() : int {
    define counter[T](T x) {
      enclosing.result++;
    }
    Foreach[counter]();
  }
}

// implicitly implements Sequence
define List[T : type](h : T, t : self)
{
  T head;
  self tail;
  head = h;
  tail = t;

  define Foreach[F[T]]() {
    F[T](Head);
    if (tail != null) {
      Tail.Foreach[F[T]]();
    }
  }
  define Last() : self {
    self curr = this;
    while (curr.tail != null) {
      curr = curr.tail;
    }
  }
  define Concat(x : self) {
    Last().tail = x.Clone();
  }
}

// type aliasing
define List[T : type](h : T)
  = List[T](h, null);

// class as function
define Pair[T : type](T x0, T x1) : List[T] {
  result = List[T](x0, List[T](x1));
}
So far this seems like a very reasonable thing for a language to do. Are there any possible drawbacks to this kind of design for a language that I haven't thought of?

By the way, some of my more devoted readers may notice that there are a few syntactic changes in this example. This is part of another idea I am playing with for making Heron more palatable. I am considering giving Heron more Java-like semantics as opposed to the C++ approach I had been previously championing.

Talk Back!

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