I need to simulate assigning a procedure to a variable in order to be able to call it through the variable.
How could I do that, is it possible?
In other languages this would be a function type variable to dynamically link calls to subroutines.
Any suggestion,
Osmany
ABG
August 29, 2024, 2:30am
2
Nothing there, just static calls to procedures from procedures drawer.
ABG
August 29, 2024, 2:44am
4
You really have to jump through hoops to attempt such a thing in AI2.
You could assign different Clock components to variables, and enable their Timers using generic blocks. But the Clocks would need to communicate through global variables or other side effects.
Or you would have to code your own interpreter for data representations of the calls, stack and all.
Some people run JavaScript for various stunts.
I figured that, but I had to ask.
What I really need is to invert dependency in my code. Something like passing dynamic linked procedure by parameter.
Something similar to "Any component" drawer, but for procedures.
Thanks any way.
In fact there is a PR for anonymous procedure
mit-cml:ucr
← ColinTree:implement/anonymous-procedure
opened 01:45PM - 15 Feb 19 UTC
This PR implements anonymous procedure defining and calling. As suggested by @wx… bit, name `anonymous procedure` is avoid to be displayed in these blocks, in order to avoid increasing learning costs.
What's more, blocks `get procedure` & `calling procedure with input list` are suggested by @wxbit. (Thank you!)
With anonymous procedure introduced into appinventor, callbacks can now be defined and implementing multi-thread blocks can be added into the plan.
Need help/suggestion:
- [x] TOOLTIPs
- [ ] HELPURLs
- [ ] type blocks
- [ ] order of blocks
- [ ] block naming
Blocks added (in control)
![image](https://user-images.githubusercontent.com/22613139/52966148-84ca0100-33e1-11e9-8424-cf81f7e5410c.png)
(in category procedure):
![image](https://user-images.githubusercontent.com/22613139/52966129-77147b80-33e1-11e9-8126-fb726718068f.png)
Some samples:
* calling a global defined procedure
![image](https://user-images.githubusercontent.com/22613139/52894481-01dc5700-31e5-11e9-9cf0-1f83bbc5d7ab.png)
* Calculating with mode switcher
![image](https://user-images.githubusercontent.com/22613139/52967902-94981400-33e6-11e9-88b7-bd8be1c4f032.png)
* Calculating with mode switcher (2)
![image](https://user-images.githubusercontent.com/22613139/52967867-77634580-33e6-11e9-8a94-501d789a52cb.png)
but MIT have no plan to merge it.
ABG
August 29, 2024, 2:59am
7
What kind of app needs such a thing?
When you want to organize your code following the dependency injection design pattern.
A simple solution would be to provide the possibility of defining and implementing interfaces (set of procedures). Then you only have would to instantiate the desired implementation and pass it as a parameter.
This would require AI2 to provide object oriented programming.