Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I read the input parameter of a function that is passed into another function?

I am writing a method that accepts as its argument a function which also has one argument. Is it possible to access the argument of the passed-in function within the body of the method that receives the function?


Here’s the method’s syntax:
T theMethod(T Function(T inputT) theFunction) {}

The method (theMethod) is meant to return a T.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

theMethod accepts as its argument a T function (theFunction) which takes another T as its argument (inputT).

I want to evaluate theFunction conditionally depending on some status of inputT.

I know I could achieve this if I pass in inputT as a separate parameter, but is there any way with the current syntax that I can access the value of inputT within the body of theMethod?

I’ve tried accessing inputT directly, as well as accessing it with dot-notation inside the function and neither of those seem to work.

>Solution :

inputT is not a variable, it’s just a part of the declaration of the type of theFunction. When you call theMethod, there is no way to pass an input to theFunction directly without creating another parameter.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading