[lazarus] classesh.inc change required
Florian Klaempfl
Florian.Klaempfl at gmx.de
Thu Dec 23 05:01:18 EST 1999
Alexandre Grenier wrote:
>
> > + From: Kevin Berry [mailto:kevinbe71 at yahoo.com]
> > +
> > + mmmm... just thought of something...
> > + Because " MyVar: TMyFunc;" is present wouldn't
> > + it still do type-checking anyway.
> >
> > Ok... I'm not into the compiler internals, but if I'm correct, if you have
> > an untyped pointer, it is just a pointer and the compiler shouldn't
> (can't)
> > do typechecking anything else then if it is correct to asign a plain
> > pointer.
> >
> > Marc
>
> That's why I say not using @ is better.. that way it's more consistent. If
> you assign a value to a var, the compiler will make sure that the types are
> good.. and you can always do a typecast if you want to force it.
>
> type
> TFunc1: function:Integer;
> TFunc2: function:Boolean;
> var
> VarFunc1 : TFunc1
> VarFunc2 : TFunc2
> Int:Integer;
>
> function Func1:Interger;
> function Func2:Boolean;
>
> VarFunc1:= Func1;
> VarFunc2:= Func2;
>
> Int:= VarFunc1;
>
> // ^- that makes perfect sence, right?
>
> Int:= VarFunc2;
>
> // ^- that's not good, right?
>
> VarFunc1:= @Func2;
> Int:= VarFunc1;
>
> // ^- then that's no good either....
To clarify things you can write
Int:=VarFunc1();
in FPC (don't flame me for that C like construct).
So I think
VarFunc1:=@Func2;
Int:=VarFunc1();
is very clear, everybody knows what happens without knowing the actual types!!!
>
> Or I migh tjust be missing the point.
One important point is overloading, consider the following:
type
tf = function : integer;
function f : integer;
procedure p(i : integer);
procedure p(f : tf);
In Delphi mode you would write:
p(f);
but what should the compiler do? Call f and using the first version p or
using the second one and passing the address of f?
In FPC mode it's clear:
p(f);
means calling f and using the integer version of p
while
p(@f);
calls the second one
It even works if you've a third version of p:
procedure p(p : pointer);
p(@f);
still calls the second version because the last one would require an (implicit) type cast,
if you
want to call the last one you have to write
p(pointer(@f));
More information about the Lazarus
mailing list