Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Arrays of procedures
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
chaos




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: 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
Insectoid




PostPosted: 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.
Zren




PostPosted: 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
chrisbrown




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: