Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Feature Request: const function + case support.

1 view
Skip to first unread message

Skybuck Flying

unread,
Dec 12, 2009, 10:31:07 PM12/12/09
to
Hello,

I would like to write code as follows:

const function TSomeObject.Calculate( ParaA, ParaB : integer );
begin
result := ParaA shl 5 + ParaB;
end;

procedure TSomeObject.DoSomething;
begin
case SomeVariable of
Calculate( SomeConstant1, SomeConstant2 ),
Calculate( SomeConstant3, SomeConstant4 ),
Calculate( SomeConstant5, SomeConstant6 ) :
begin
// ...
end;

Calculate( SomeConstant7, SomeConstant8 ),
Calculate( SomeConstant9, SomeConstant10 ):
begin
// ...
end;

Calculate( SomeConstant11, SomeConstant12 ):
begin
// ...
end;
else
// ...
end;
end;

Currently there are a couple of problems with Delphi 2007:

1. First of all functions cannot be used in case statements... (case
statements expect constant values it says)
2. Second of all macro's do not exist in Delphi which might be a good
thing... since macro's in C cannot be debugged just like that.

So the solution above would do the following:

1. Add support for "constant functions" to the Delphi language... These
"constant functions" would replace macro functionality in a more
Delphi-like-way.

2. The constant function is compiled seperately and is then fed by the
value's from the case statement, then those value's are pre-calculated and
then used in additional compiling... with a bit of luck this could still be
considered "one-pass compiling". The constant function would need to be very
simple and be "seperate-compiling" without requiring any other source
code... if necessary the TSomeObject. could be left out.

This will allow the case statement to be optimally generated by allowing the
compiler to optimize on the constant values produced... as it currently
does.

3. The debugger would need special support to "fool" the programmer that the
debugger and application is actually executing the "constant functions"...
the debugger should call the "seperated compiled function" and execute it's
code to be able to debug these "constant functions". This just offers the
programmer an important conveniences. It prevents the programmer from having
to change the code just to be able to debug it... which is an important
productivity gain.

The benefit of all this is small... at least in the example above.

Though maybe this could offer a path to more macro-like functionality in
Delphi with debugging support to be able to do other things... like maybe
inlining assembler routines ?

I have seen code where macro-like functionality would have been usefull to
be able to combine values to produce constants or to be able to inline
assembler routines. (Like SSE instructions or so... but I think those are
dead with the coming of parallel gpu's and multi-core stuff ??)

Bye,
Skybuck.


BRoberts

unread,
Dec 13, 2009, 5:37:15 PM12/13/09
to
"Skybuck Flying" <IntoTh...@hotmail.com> wrote in message
news:db40e$4b245ff5$d53371df$27...@cache1.tilbu1.nb.home.nl...

> Hello,
>
> I would like to write code as follows:
>

Then don't use Pascal.

Skybuck Flying

unread,
Dec 14, 2009, 4:12:07 AM12/14/09
to

"BRoberts" <berdon...@caneris.ca> wrote in message
news:7a114$4b256c9f$45c49ff1$4...@TEKSAVVY.COM-Free...

I am using Delphi, Fool ! =D

Pearls for the swine eh ?! ;) :)

It makes perfect sense to me... the cg shader language does something
similiar which was kinda surprising... C++ does it too...

Example how it could look in Delphi:

const
MyConst1 = 5;
MyConst2 = 6;

const function MyFunction( Const Para1 : integer; Const Para2 : integer ) :
integer;
begin
result := Para1 + Para2;
end;

begin
writeln( MyFunction( MyConst1, MyConst2 ) );
end.

What happens is:

Delphi "pre-compiles" MyFunction according to a set of rules. Then Delphi
pre-calculate the outcome.

The actual compiled code will be:

begin
writeln( 11 );
end.

"Poof it's magic !", MyFunction is completely gone from the final executable
! (It's all turned into pre-calculated constants by delphi language... very
nice idea !)

This would offer programmers to capability of pre-calculating all kinds of
complex formula's by simply using the delphi language !

Bye,
Skybuck.


Maarten Wiltink

unread,
Dec 14, 2009, 5:42:49 AM12/14/09
to
"BRoberts" <berdon...@caneris.ca> wrote in message
news:7a114$4b256c9f$45c49ff1$4...@TEKSAVVY.COM-Free...
> "Skybuck Flying" <IntoTh...@hotmail.com> wrote in message
> news:db40e$4b245ff5$d53371df$27...@cache1.tilbu1.nb.home.nl...

>> I would like to write code as follows:

<compile-time constant functions>

> Then don't use Pascal.

Which Pascal? 1970 Wirth Pascal? ISO/ANSI Pascal? Turbo Pascal?
Object Pascal?

...FreePascal? That might work.

It's actually not an unreasonable idea (suggesting that it's a
bit below his faculties). The difference with constant expression
evaluation is minimal, and it has been observed that by its nature,
a compiler is well-equipped to resolve them. Just generate the
code, run it once inside the compiler, and use the Result.

It would be nice if Skybuck took his dogfood recipes and did the
work on baking them himself. *That* course of action might end with
a degree-worthy publication, instead of half-baked and unbacked
'luminous ideas'.

Groetjes,
Maarten Wiltink


0 new messages