Arrays of procedures
Author |
Message |
chaos
|
Posted: Sat Mar 26, 2011 4:25 pm Post subject: Arrays of procedures |
|
|
I was wondering if it was possible to create an array of procedures. If so, could someone tell me the code for it please? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Sat Mar 26, 2011 4:42 pm Post subject: RE:Arrays of procedures |
|
|
Not sure about the why of it, but giving it a dummy proc name seems to work for no apparent reason other than being useless.
You gonna have to convince me of why you need to do this however... It's not very practical as you have to define it in a non dynamic way first, and would be better off just calling them directly instead of hiding it within an array.
Also, I'm pretty sure all the procedures have to have the same number and type of arguments. If decide to replace this with functions, you'd also have to have the same return type. *I do believe*
Turing: |
var p : array 1 .. 10 of proc m
proc g
put "meh"
end g
proc h
put "bleh"
end h
for i : 1 .. 10 by 2
p (i) := g
end for
for i : 2 .. 10 by 2
p (i) := h
end for
for i : 1 .. 10
p (i)
end for
|
|
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Sat Mar 26, 2011 5:03 pm Post subject: RE:Arrays of procedures |
|
|
There are instances where this is useful (I love my C function pointers!) but these situations are relatively rare and you likely won't need to do this.
Also, I had no idea you could do this in Turing. Every now and again it surprises me. |
|
|
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Sat Mar 26, 2011 5:32 pm Post subject: RE:Arrays of procedures |
|
|
On further examination, I've pretty good theory on why this works. My origional idea that this could work is because you can pass functions as parameters. Which means methods are types to some sense.
array 1..2 of proc
That doesn't work. It gives a syntax error, as Turing was never designed, nor did the creators ever think anyone would do this. So it needs to follow proper syntax.
proc m
That part is equivalent to defining the procedure, and storing the member variable name. Because of the syntax, it usually would try to compile the actual content of the function next. But the syntax of defining a variables type only looks for the next keyword after : type, or : array 1..3 of type, it doesn't need to define the body of the code. But, the syntax will check if there's a keyword after proc to define it, even if it's never used.
TL;DR: It's unintended to the best of my knowledge.
Turing: |
var p : proc m
proc g
put "meh"
end g
p := g
p
|
|
|
|
|
|
![](images/spacer.gif) |
chrisbrown
![](http://compsci.ca/v3/uploads/user_avatars/18814724584bcbb8192aae8.png)
|
Posted: Sat Mar 26, 2011 7:43 pm Post subject: RE:Arrays of procedures |
|
|
http://compsci.ca/holtsoft/doc/subprogramtype.html
Not unintended, just clunky syntax -- probably to simplify parsing.
It's not very useful, anyway. All functions must be known at compile-time, which means no closures or anonymous functions. |
|
|
|
|
![](images/spacer.gif) |
|
|