Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2007-10

function alias

9 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mattias Gaertner

How can I create an alias for a function?
For example:

procedure DoSomething(...params...); cdecl; external 'useful';
const
  DoAliasSomething = DoSomething;

fpc does not allow this. So how can it be done?

Mattias

Michael Van Canneyt
to Mattias Gaertner
Mattias Gaertner wrote 9 lines
How can I create an alias for a function? For example: procedure DoSomething(...params...); cdecl; external 'useful'; const DoAliasSomething = DoSomething; fpc does not allow this. So how can it be done?
program test;
procedure something; external name 'something';
Type
  TProcedure = Procedure;
const
  DoSomething : TProcedure = @something;
begin
end.

Works fine.

Michael.

Mattias Gaertner
to Michael Van Canneyt
Show quoted text 28 lines
On Thu, 4 Oct 2007, Mattias Gaertner wrote: > How can I create an alias for a function? > For example: > > procedure DoSomething(...params...); cdecl; external 'useful'; > > const > DoAliasSomething = DoSomething; > > fpc does not allow this. So how can it be done? program test; procedure something; external name 'something'; Type TProcedure = Procedure; const DoSomething : TProcedure = @something; begin end. Works fine.

Yes, but I hoped to declare it in situ - without adding another type. ;) The reason is that I'm auto translating some C headers that contains aliases for functions.

Mattias

Daniël Mantione
to Mattias Gaertner

Op Thu, 4 Oct 2007, schreef Mattias Gaertner:

Yes, but I hoped to declare it in situ - without adding another type. ;) The reason is that I'm auto translating some C headers that contains aliases for functions.
procedure DoAliasSomething(...params...); cdecl; external 'useful';

... is another approach.

Daniël

Mattias Gaertner
to Daniël Mantione
Show quoted text 11 lines
Op Thu, 4 Oct 2007, schreef Mattias Gaertner: > Yes, but I hoped to declare it in situ - without adding another > type. ;) The reason is that I'm auto translating some C headers > that contains aliases for functions. procedure DoAliasSomething(...params...); cdecl; external 'useful'; ... is another approach.

Ok. I implemented that.

I'm curious: Why does fpc not allow
const FuncAlias = MyFunc;
?

Mattias

Daniël Mantione
to Mattias Gaertner

Op Sat, 6 Oct 2007, schreef Mattias Gaertner:

Show quoted text 20 lines
On Thu, 4 Oct 2007 20:17:36 +0200 (CEST) Daniël Mantione wrote: > > > Op Thu, 4 Oct 2007, schreef Mattias Gaertner: > > > Yes, but I hoped to declare it in situ - without adding another > > type. ;) The reason is that I'm auto translating some C headers > > that contains aliases for functions. > > procedure DoAliasSomething(...params...); cdecl; external 'useful'; > > ... is another approach. Ok. I implemented that. I'm curious: Why does fpc not allow const FuncAlias = MyFunc; ?

It is not part of the Pascal language, not implemented by any other compiler and nobody ever saw a need to implement such an extension.

Daniël

ik
to Mattias Gaertner: On Thu, 4 Oct 2007 20:17:36 +0200 (CEST) Ok. I implemented that. I'm curious: Why does…
Mattias Gaertner wrote 20 lines
On Thu, 4 Oct 2007 20:17:36 +0200 (CEST) Daniël Mantione wrote: > > > Op Thu, 4 Oct 2007, schreef Mattias Gaertner: > > > Yes, but I hoped to declare it in situ - without adding another > > type. ;) The reason is that I'm auto translating some C headers > > that contains aliases for functions. > > procedure DoAliasSomething(...params...); cdecl; external 'useful'; > > ... is another approach. Ok. I implemented that. I'm curious: Why does fpc not allow const FuncAlias = MyFunc; ?

Yuck, that reminds me of C++ templates. it allows you to alias "{" as "begin" (for example) and also to make such aliases... it can create a lot of problems when people does not fully understand what you have done.

I can think on other way to implement "alias":

procedure OriginalProc(..).. {$IFDEF USEINLINE_ALIAS}inline;{$ENDIF}
begin
end;
...
procedure AliasProc(...);
begin
  {$DEFINE USEINLINE_ALIAS}
  OriginalProc(...);
  {$UNDEF USEINLINE_ALIAS}
end;

But it looks so ugly ...

Mattias

Ido

Mattias Gaertner
to ik
Show quoted text 25 lines
On 10/6/07, Mattias Gaertner wrote: > On Thu, 4 Oct 2007 20:17:36 +0200 (CEST) > Daniël Mantione wrote: > > > > > > > Op Thu, 4 Oct 2007, schreef Mattias Gaertner: > > > > > Yes, but I hoped to declare it in situ - without adding another > > > type. ;) The reason is that I'm auto translating some C headers > > > that contains aliases for functions. > > > > procedure DoAliasSomething(...params...); cdecl; external > > 'useful'; > > > > ... is another approach. > > Ok. I implemented that. > > I'm curious: Why does fpc not allow > const FuncAlias = MyFunc; > ? Yuck, that reminds me of C++ templates. it allows you to alias "{" as "begin" (for example) and also to make such aliases...

Sounds more like macros, than templates.

Show quoted text 19 lines
it can create a lot of problems when people does not fully understand what you have done. I can think on other way to implement "alias": procedure OriginalProc(..).. {$IFDEF USEINLINE_ALIAS}inline;{$ENDIF} begin end; ... procedure AliasProc(...); begin {$DEFINE USEINLINE_ALIAS} OriginalProc(...); {$UNDEF USEINLINE_ALIAS} end; But it looks so ugly ...

And it's even more complicate. ;)

Mattias

Johann Glaser
to ik: Yuck, that reminds me of C++ templates. it allows you to alias "{" as "begin" (for…

Hi!

Show quoted text 11 lines
procedure OriginalProc(..).. {$IFDEF USEINLINE_ALIAS}inline;{$ENDIF} begin end; ... procedure AliasProc(...); begin {$DEFINE USEINLINE_ALIAS} OriginalProc(...); {$UNDEF USEINLINE_ALIAS} end;

Are you sure this works? AFAIK the "preprocessor" doesn't follow function calls but simply reads the .pas file from its first line to the end.

Another problem is that the OP wanted to alias external functions, where he can't define them as inline.

But he can reverse your approach:

Procedure NewName(...); inline;
Begin
  OriginalProc(...);
End;

Bye
Hansi