Jump to content
ti.teg.tnod.I

Store functions/procedures with pre-set variable values (Like a lambda object)

Recommended Posts

Basically I want something like this to work:

 

program New;
type foo = record
 bar: procedure();
end;

var oof: foo;

procedure rab(test: String);
begin
 WriteLn(test);
end;

begin
 oof.bar := @rab('Hello World');
 oof.bar;
end.

 

That returns the error "Type mismatch". My question is: What type is it returning?

 

That would output "Hello World". The python implementation of this would be:

 

def rab(test):
 print test

oofbar = lambda: rab('Hello World')
oofbar()

 

A more advanced example of lambda use can be found here. This makes it so I don't need to have multiple functions/procedures when I'm storing them into variables for later, static use (Cannot pass parameters without hacks).

Link to comment
Share on other sites

Uh, are you trying to do this?

[scar]

program New;

type foo = record

bar: String;

end;

 

var oof: foo;

 

function rab(test: String): String;

begin

WriteLn(test);

Result := test;

end;

 

begin

oof.bar := rab('Hello World');

writeln(oof.bar);

end.

[/scar]

 

Its kinda difficult to emulate what python does in scar because they both are pretty much opposite to each other(in terms of the way code is written/executed)

Edited by KingKong
Link to comment
Share on other sites

Uh, are you trying to do this?

[scar]

program New;

type foo = record

bar: String;

end;

 

var oof: foo;

 

function rab(test: String): String;

begin

WriteLn(test);

Result := test;

end;

 

begin

oof.bar := rab('Hello World');

writeln(oof.bar);

end.

[/scar]

 

Its kinda difficult to emulate what python does in scar because they both are pretty much opposite to each other(in terms of the way code is written/executed)

 

No, that's storing a string. Also I don't see what makes python the opposite of SCAR at all actually ...

 

Don't think this is possible. Maybe in titan though..

 

Damn, oh well. I just figured "Type mismatch" implied it was actually returning a usable type. Is there any way to check the type of an object?

Link to comment
Share on other sites

In the newer Delphi versions you can accomplish something similar using anonymous methods and variable binding: http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/anonymousmethods_xml.html

Not possible in SCAR, though.

Well yes, the method part, but as far as I'm aware you can't set parameters in advance?

Link to comment
Share on other sites

Well yes, the method part, but as far as I'm aware you can't set parameters in advance?

 

It's more like an object that calls the function with it's own parameters ... So when you call the function, you're actually calling a lambda object, that calls the function, and the lambda object fills in the parameter. So you're not actualy "presetting" the parameter, I guess it was poor choice of wording on my part.

 

Edit: Does this solve anything? http://delphisorcery.blogspot.com/2011/05/lambdas-and-expression-trees-in-delphi.html

Edited by ti.teg.tnod.I
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...