Nostalgia – what’s next?

disketterhi.jpgI had a little delayed spring cleaning of my apartment. And look what I found deep down in a drawer!

It is easy to feel nostalgic but what I learned back then back in school using Turbo Pascal has formed my whole carrier. I have always liked the Pascal language; I do not know why but I think it is because I feel productive and it is easy to get a grip of the code compared to other languages. (A very subjective, personal opinion – please do not flame me with a language war.)

It is easy when you speak one programming language fluently to look down on other languages and see what is missing in them and what makes your language more high level and effective. But what about the other way around? If you look up at a language that has something that make it more effective and help you develop on a higher level of abstraction. Will you recognise it? Probably not. I guess that most people would have a look at it and disregard it as something strange that you do not need.

I loved when the Pascal language was extended with objects in back in Turbo Pascal 5.5 but it took a while of study and thinking before I got it. We have made a lot of improvements over the years (and I like all of them except nested types which I think looks ugly, it does not feel right).

So what is the next major improvement of Object Pascal going to be? If I could decide I would have a look at one of the cool features of Lisp – closures.

How could that look like in Pascal? Procedural types can already be used to pass along a pointer to a function but passing a pointer to a local procedure to a procedure variable will not be accepted by the compiler today.

procedure doThreetimes( P: TProcedure ); 
begin 
  P; 
  P; 
  P; 
end;procedure Count; 
var 
  n: integer; 
procedure add1; 
  begin 
    n := n + 1; 
  end; 
begin 
  n := 0; 
  doThreeTimes( add1 ); 
  WriteLn( n ) // here n is 3 
end;

Another way could be to pass anonymous functions.

procedure doThreetimes( P: TProcedure ); 
begin 
  P; 
  P; 
  P; 
end;procedure Count; 
var 
  I: integer; 
begin 
  I := 0; 
  doThreeTimes( 
    procedure(); 
    begin 
      I := I + 1; 
    end; ); 
  WriteLn( I ) // here I is 3 
end;

Or maybe like this:

procedure TForm1.FormCreate(Sender: TObject); 
begin 
  Button1.OnClick := procedure(); begin 
    Self.Caption := 'New Form Title'; 
  end; 
end;

So what would you choose to improve in Object Pascal if you could decide for yourself?