Hi! I'm trying to cast a Function to a procedure, but my little program fails with a "Segmentation fault". Could anyone please tell me, what is wrong? 8<------- (snip) ---------------- Program TestIt; Type bar = Procedure(a:Integer); Var BarFunc : bar; Function FooFunc(a:Integer) : Integer; Begin Writeln('I am of foo with:', a); FooFunc := a+1 End; Begin Writeln('FooFunc(3) is ', FooFunc(3)); Writeln('now bar(FooFunc(2))'); BarFunc := bar(FooFunc(2)); BarFunc(2) End. ------- (snap) ---------------->8 Thank you Eike
Hi! Eike Lange wrote:
I'm trying to cast a Function to a procedure, but my little program fails with a "Segmentation fault".
Could anyone please tell me, what is wrong? [...] BarFunc := bar(FooFunc(2));
You are casting the result of the function call - the integer 2 - to a `Barfunc'. Please try (untested): BarFunc := bar(@FooFunc); Hope this helps, Peter -- http://home.pages.de/~Peter.Gerwinski/ - G-N-U GmbH: http://www.g-n-u.de Maintainer GNU Pascal - http://home.pages.de/~GNU-Pascal/ - gpc-20001116 GnuPG key fingerprint: 9E7C 0FC4 8A62 5536 1730 A932 9834 65DB 2143 9422 keys: http://www.gerwinski.de/pubkeys/ - AntiSpam: http://spam.abuse.net
On 16 Nov 2000, at 14:32, Eike Lange wrote:
Hi! I'm trying to cast a Function to a procedure, but my little program fails with a "Segmentation fault".
Could anyone please tell me, what is wrong?
8<------- (snip) ---------------- Program TestIt;
Type bar = Procedure(a:Integer);
Var BarFunc : bar;
Function FooFunc(a:Integer) : Integer; Begin Writeln('I am of foo with:', a); FooFunc := a+1 End;
Begin Writeln('FooFunc(3) is ', FooFunc(3)); Writeln('now bar(FooFunc(2))'); BarFunc := bar(FooFunc(2));
This seems to me to be an illegal assignment. AFAIK the compiler should reject it as an invalid variable reference. Best regards, The Chief --------- Prof. Abimbola Olowofoyeku (The African Chief) Email: African_Chief@bigfoot.com Author of Chief's Installer Pro v5.24 for Win32 http://www.bigfoot.com/~African_Chief/chief32.htm
Prof. A Olowofoyeku (The African Chief) wrote:
On 16 Nov 2000, at 14:32, Eike Lange wrote:
Hi! I'm trying to cast a Function to a procedure, but my little program fails with a "Segmentation fault".
Could anyone please tell me, what is wrong?
8<------- (snip) ---------------- Program TestIt;
Type bar = Procedure(a:Integer);
Var BarFunc : bar;
Function FooFunc(a:Integer) : Integer; Begin Writeln('I am of foo with:', a); FooFunc := a+1 End;
Begin Writeln('FooFunc(3) is ', FooFunc(3)); Writeln('now bar(FooFunc(2))'); BarFunc := bar(FooFunc(2));
This seems to me to be an illegal assignment. AFAIK the compiler should reject it as an invalid variable reference.
The type cast makes it "valid". Basically, a type casts tells the compiler: I know what I'm doing, don't complain... Frank -- Frank Heckenbach, frank@g-n-u.de, http://fjf.gnu.de/ GPC To-Do list, latest features, fixed bugs: http://agnes.dida.physik.uni-essen.de/~gnu-pascal/todo.html
Hi On Thu, 16 Nov 2000, Frank Heckenbach wrote:
Prof. A Olowofoyeku (The African Chief) wrote:
On 16 Nov 2000, at 14:32, Eike Lange wrote:
Hi! I'm trying to cast a Function to a procedure, but my little program fails with a "Segmentation fault".
...
Type bar = Procedure(a:Integer);
...
BarFunc := bar( ... );
Am I missing something here? This looks like trying to use a proceedure as a function, which is a no-no. If you want to use a function as a proceedure why not just do it? The compiler will complain with a warning message, but it works. Thanks Russ
On Thu, Nov 16, 2000 at 03:19:53PM -0800, Russ Whitaker wrote:
This looks like trying to use a proceedure as a function, which is a no-no. If you want to use a function as a proceedure why not just do it? The compiler will complain with a warning message, but it works.
It wasn't as trivial as shown here. I would like to use this function (after cast) as a parameter to an other function which needs a precedural-parameter like Function foo2(a:Integer; b:bar); But how is it vice-versa? Can I assign a function-value to a Procedure? Example: 8<----------- (snip) ----------- Program TestIt; Type myfoo = Function(a:Integer):Integer; Var FooFunc : myfoo; Procedure BarFunc(a:Integer); Begin Writeln('I am of bar with:', a) End; Begin BarFunc(1); { Thank you Peter } FooFunc := myfoo(@BarFunc); Writeln('FooFunc(2) returns: ', FooFunc(2)); End. ----------- (snap) ----------->8 The resut is: I am of bar with:1 I am of bar with:2 FooFunc(2) returns: 134641184 I would like to to set the value of FooFunc to 0. (This is too needed as a parameter to a function expecting a fuctional-parameter but this function *must* be 0) Eike
Eike Lange wrote:
On Thu, Nov 16, 2000 at 03:19:53PM -0800, Russ Whitaker wrote:
This looks like trying to use a proceedure as a function, which is a no-no. If you want to use a function as a proceedure why not just do it? The compiler will complain with a warning message, but it works.
It wasn't as trivial as shown here. I would like to use this function (after cast) as a parameter to an other function which needs a precedural-parameter like
Function foo2(a:Integer; b:bar);
But how is it vice-versa? Can I assign a function-value to a Procedure? Example: 8<----------- (snip) ----------- Program TestIt; Type myfoo = Function(a:Integer):Integer; Var FooFunc : myfoo;
Procedure BarFunc(a:Integer); Begin Writeln('I am of bar with:', a) End;
Begin BarFunc(1); { Thank you Peter } FooFunc := myfoo(@BarFunc); Writeln('FooFunc(2) returns: ', FooFunc(2)); End. ----------- (snap) ----------->8 The resut is:
I am of bar with:1 I am of bar with:2 FooFunc(2) returns: 134641184
I would like to to set the value of FooFunc to 0.
(This is too needed as a parameter to a function expecting a fuctional-parameter but this function *must* be 0)
You can use a wrapper function: function BarWrapper (a:Integer); begin BarFunc (a); BarWrapper := 0 end; ... FooFunc := BarWrapper; Frank -- Frank Heckenbach, frank@g-n-u.de, http://fjf.gnu.de/ GPC To-Do list, latest features, fixed bugs: http://agnes.dida.physik.uni-essen.de/~gnu-pascal/todo.html
participants (5)
-
Eike Lange -
Frank Heckenbach -
Peter Gerwinski -
Prof. A Olowofoyeku (The African Chief) -
Russ Whitaker