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.
> for_all (test: FUNCTION [ANY, TUPLE [G], BOOLEAN]): > BOOLEAN
In this case test is a variable of type FUNCTION[...]. This kind of first-class function construct is common in many langauges and is roughly equivalent to a function pointer.
On the other hand what I am referring to is parameterization with the function as a type.
The ideas for ETL3 stretches the whole idea further, and provides a type-safe and simplified alternative to reflection. Unfortunately none of the Eiffel compilers fully implement it yet.
> Agents are full blown objects, providing access to featues > such as the operands, the target (instance being acted > ed upon) and conditions (pre and post). > > The object hierarchy has classes FUNCTION and PROCEDURE, > both descendants of ROUTINE.
Yes but my point is that I want to use a function as a type. An object is an instance of a type, not in of itself a type.
Sorry, I thought you were talking about objects that represent functions.
Does this mean that within Heron there would be no distinction between a Function and a Class. There would just be functions, and Classes would be built by using nested functions?
Sorry if I'm struggling, but I haven't looked at Heron for a few months. Is the define keyword new? I don't remember it from last time I looked at Heron.
> Chris, > > Sorry, I thought you were talking about objects that > represent functions.
Sorry for the confusion, it is my fault.
> Does this mean that within Heron there would be no > distinction between a Function and a Class. There would > just be functions, and Classes would be built by using > nested functions?
Actually I was playing with the idea that all functions could simply be replaced by class constructors.
> Sorry if I'm struggling, but I haven't looked at Heron for > a few months. Is the define keyword new? I don't > remember it from last time I looked at Heron.
I don't know if any of these ideas will make it in. The define keyword does exist in the current Heron implementation currently and is equivalent to C++ typedef. It creates a type alias. In the example I posted however I intended it to mean to define a new type.
I only skimmed over this, but it sounds a little like classes in JavaScript. Or maybe it is the reverse? In JavaScript you can use any arbitrary function as the constructor of a class. Actually, it is quite a confusing mess and makes object oriented programming in JavaScript unpalatable, IMO.
> I only skimmed over this, but it sounds a little like classes in JavaScript.
JavaScript doesn't have classes. It has objects and objects alone, which are cloned and modified. That's the magic of prototype-based programming.
> In JavaScript you can use any arbitrary function as the constructor of a class.
Yes and no. The "constructor" is just a function that gets invoked when you clone it using the new operator.
> Actually, it is quite a > confusing mess and makes object oriented programming in > JavaScript unpalatable, IMO.
If you're dumping all that stuff in the constructor, you're doing something very, very wrong. It wastes memory through creating unnecessary closures, and it's confusing to boot.
Once I understood it, I think I like this idea. It feels to me more like the class is a function (the constructor?).
You can then read the whole class definition as a function, which defines member variables, defines member functions, and finally returns the object isntance. <P> Then conceptually each object is built by calling any function, and objects are the same type if they have the same members defined by that function. <P> Taking this further, you could have conditional member definitions, which would let you create specialisations of the class at compile or run-time. I can't think of any earthshaking uses for this, so I'll just suggest optimizations for now, like this 'optimization' of multiplication for numbers:
Bla is a function language that was implemented to experiment with the idea of first-class environments. I have listed a code example below.
X() = self where a = 1 b() = a + 1
I believe Wouter did an excellent job in combining the ideas of first-class functions and objects... although it seems to be pretty easy once you see Javascript (prototypical) and then Bla (class-based).