Sign in Join

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

Mailing lists / fpc-pascal / 2011-12

alias a function + overload 'in'

10 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

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

David Emerson

two little questions

1. Is it possible to make an alias to a function ... so rather than just re-calling with the same parameters, it's actually the same thing? like the way we can do, e.g., type natural = cardinal, or const GG = 6, but with a function?

2. is it possible to overload the 'in' operator, so that it may work (using supplied code) on things that are not a pascal set?

Thanks!
~David.

Howard Page-Clark
to David Emerson
David Emerson wrote
1. Is it possible to make an alias to a function ... so rather than just re-calling with the same parameters, it's actually the same thing? like the way we can do, e.g., type natural = cardinal, or const GG = 6, but with a function?

There's always the simple-minded calling of identical code such as

function Name: string;
   begin
     result := 'Name';
   end;
   function NameAlias: string;
   begin
     Result := Name;
   end;

Perhaps you were looking for something more sophisticated?

Tomas Hajny
to David Emerson: two little questions 1. Is it possible to make an alias to a function ... so rather than…
David Emerson wrote 5 lines
two little questions 1. Is it possible to make an alias to a function ... so rather than just re-calling with the same parameters, it's actually the same thing? like the way we can do, e.g., type natural = cardinal, or const GG = 6, but with a function?

.
.

There are multiple solutions possible:

1) Making the functions public and creating the aliases as external:

procedure X; public name 'X';
begin
 WriteLn (1);
end;
procedure Y; external name 'X';
begin
 X;
 Y;
end.

2) Using procedural variables / constants:

type
 TProc = procedure;
procedure X;
begin
 WriteLn (1);
end;
const
 Y: TProc = @X;
begin
 X;
 Y;
end.

3) Using inlining:

procedure X;
begin
 WriteLn (1);
end;
procedure Y; inline;
begin
 X;
end;
begin
 X;
 Y;
end.

Tomas

Jürgen Hestermann
to David Emerson: two little questions 1. Is it possible to make an alias to a function ... so rather than…
1. Is it possible to make an alias to a function ... so rather than just re-calling with the same parameters, it's actually the same thing?

like the way

we can do, e.g., type natural = cardinal, or const GG = 6, but with a

function?

I think you can declare a type

type MyFunctionType = function(A : LongString; B : AnsiString; ...) of
whatever;

then you can declare multiple functions of this type:

var MyFunction1,MyFunction2,MyFunction3 : MyFunctionType;
so all these variables share the same declaration and you can change it
at a central point;
2. is it possible to overload the 'in' operator, so that it may work

(using

supplied code) on things that are not a pascal set?

I do not know whether it is possible but I hate such overloading. An operator defined by the Pascal language should not become a chimera. It destroys the logic of the language. If every identifier and every operator can get an arbitrary meaning then what is the grounding of the language? Hiding details with such mechanism does not help understanding the code, just the opposite.

Why not simply use a function for such things?

Marcos Douglas
to Tomas Hajny: . . There are multiple solutions possible: 1) Making the functions public and creating…
Tomas Hajny wrote 25 lines
On 17 Dec 11, at 21:07, David Emerson wrote: > two little questions > > 1. Is it possible to make an alias to a function ... so rather than just > re-calling with the same parameters, it's actually the same thing? like the way > we can do, e.g., type natural = cardinal, or const GG = 6, but with a function?  .  . There are multiple solutions possible: 1) Making the functions public and creating the aliases as external: procedure X; public name 'X'; begin  WriteLn (1); end; procedure Y; external name 'X'; begin  X;  Y; end.

What is this syntax using "public name"?

Marcos Douglas

Tomas Hajny
to Marcos Douglas
Marcos Douglas wrote 28 lines
On Sun, Dec 18, 2011 at 9:37 AM, Tomas Hajny wrote: > On 17 Dec 11, at 21:07, David Emerson wrote: > >> two little questions >> >> 1. Is it possible to make an alias to a function ... so rather than just >> re-calling with the same parameters, it's actually the same thing? like the way >> we can do, e.g., type natural = cardinal, or const GG = 6, but with a function? > . > . > > There are multiple solutions possible: > > 1) Making the functions public and creating the aliases as external: > > procedure X; public name 'X'; > begin >  WriteLn (1); > end; > > procedure Y; external name 'X'; > > begin >  X; >  Y; > end. What is this syntax using "public name"?

This syntax allows calling of the published procedure / function from other object files (typically created in other compilers / programming languages); "public" says that calling from other objects files shall be possible and the optional "name" part allows you to specify a fixed name under which it is be accessible (in addition to the standard mangled name used by FPC).

Tomas

Marcos Douglas
to Tomas Hajny

2011/12/18 Tomas Hajny

Show quoted text 37 lines
On 18 Dec 11, at 10:31, Marcos Douglas wrote: > On Sun, Dec 18, 2011 at 9:37 AM, Tomas Hajny wrote: > > On 17 Dec 11, at 21:07, David Emerson wrote: > > > >> two little questions > >> > >> 1. Is it possible to make an alias to a function ... so rather than just > >> re-calling with the same parameters, it's actually the same thing? like the way > >> we can do, e.g., type natural = cardinal, or const GG = 6, but with a function? > > . > > . > > > > There are multiple solutions possible: > > > > 1) Making the functions public and creating the aliases as external: > > > > procedure X; public name 'X'; > > begin > >  WriteLn (1); > > end; > > > > procedure Y; external name 'X'; > > > > begin > >  X; > >  Y; > > end. > > What is this syntax using "public name"? This syntax allows calling of the published procedure / function from other object files (typically created in other compilers / programming languages); "public" says that calling from other objects files shall be possible and the optional "name" part allows you to specify a fixed name under which it is be accessible (in addition to the standard mangled name used by FPC).

Thank you.

Marcos Douglas

David Emerson
to Tomas Hajny: . . There are multiple solutions possible: 1) Making the functions public and creating…

Thank you, Tomas, those are all excellent suggestions! I am glad I asked.

Tomas Hajny wrote 6 lines
On 17 Dec 11, at 21:07, David Emerson wrote: > two little questions > > 1. Is it possible to make an alias to a function ... so rather than just > re-calling with the same parameters, it's actually the same thing? like the

way

> we can do, e.g., type natural = cardinal, or const GG = 6, but with a

function?

Show quoted text 62 lines
. . There are multiple solutions possible: 1) Making the functions public and creating the aliases as external: procedure X; public name 'X'; begin WriteLn (1); end; procedure Y; external name 'X'; begin X; Y; end. 2) Using procedural variables / constants: type TProc = procedure; procedure X; begin WriteLn (1); end; const Y: TProc = @X; begin X; Y; end. 3) Using inlining: procedure X; begin WriteLn (1); end; procedure Y; inline; begin X; end; begin X; Y; end. Tomas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
David Emerson
to Jürgen Hestermann: like the way function? I think you can declare a type type MyFunctionType = function(A :…
Jürgen Hestermann wrote 12 lines
David Emerson schrieb: > 2. is it possible to overload the 'in' operator, so that it may work > (using supplied code) on things that are not a pascal set? I do not know whether it is possible but I hate such overloading. An operator defined by the Pascal language should not become a chimera. It destroys the logic of the language. If every identifier and every operator can get an arbitrary meaning then what is the grounding of the language? Hiding details with such mechanism does not help understanding the code, just the opposite. Why not simply use a function for such things?

I should know better than to debate this with you, Jürgen, but it's too tempting! It's because, to me, Pascal is an extension of mathematics and logic; and in the languages of math and logic, such constructs make perfect sense. The fact that I cannot use 'in' on certain types of things in Pascal makes it less elegant than mathematical language.

But anyway... my guess is that it's not happening anytime soon

~David.

Unknown author
to David Emerson: two little questions 1. Is it possible to make an alias to a function ... so rather than…
Show quoted text 8 lines
two little questions 1. Is it possible to make an alias to a function ... so rather than just re-calling with the same parameters, it's actually the same thing? like the way we can do, e.g., type natural = cardinal, or const GG = 6, but with a function?

You can also define procedures and functions as variables or types.

program ...

function OtherFunc: string;
begin
  //
end;
var
  AliasFunc: function: string = @OtherFunc;

...

begin
  AliasFunc();
end.