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

Username:   Password: 
 RegisterRegister   
 Class subprograms cannot be subprogram variables
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Dusk Eagle




PostPosted: Sat Apr 04, 2009 11:07 pm   Post subject: Class subprograms cannot be subprogram variables

I am having trouble with a certain part of a program I'm writing, listed below. What I want to do is have a class with a function that accepts as a parameter an instance of itself, and can then call it's various public procedures and functions on the instance of that class. However, when I try to do this, I get an error message saying "Class subprograms cannot be subprogram variables".

Turing:

class Foo
    export isHungry, numNeighbors
   
    var hungry := true
   
    fcn isHungry () : boolean
        result hungry
    end isHungry
   
    %This fcn isn't particularly useful, but this is just an example. I cut out the real fcn for the sake of brevity.
    fcn neighbors() : array 1 .. 2 of int
        var n: array 1 .. 2 of int := init(1,2)
        result n
    end neighbors
   
    /*Public
    Returns the number of hungry neighbors.*/

    fcn numNeighbors (monster : array 1 .. * of ^Foo) : int
        var neighbor_monsters : array 1 .. 2 of int := neighbors()
        var num_hungry_monsters : int := 0
        for i : lower(neighbor_monsters) .. upper (neighbor_monsters)
            if monster (i) -> isHungry then %%The problem is right at this line
                num_hungry_monsters += 1
            end if
        end for
        result num_hungry_monsters
    end numNeighbors
end Foo


Can anyone offer any help? Thanks.
Sponsor
Sponsor
Sponsor
sponsor
richcash




PostPosted: Sun Apr 05, 2009 4:28 am   Post subject: Re: Class subprograms cannot be subprogram variables

If you define your function isHungry with parentheses (meaning takes no parameters), you must call it by including the parentheses. If you reference it without the parentheses, you are referencing the actual function itself (rather than the result of the function), and turing can not treat class subprograms as first-class members (i.e. they cannot be subprogram variables or passed as arguments).

So, in short, put parentheses next to the isHungry call or define isHungry without parentheses.
corriep




PostPosted: Sun Apr 05, 2009 1:54 pm   Post subject: RE:Class subprograms cannot be subprogram variables

I have run into this problem before also. There is no real work-around but in your case you could just make the hungry variable public instead of using a function to access it
richcash




PostPosted: Sun Apr 05, 2009 2:55 pm   Post subject: Re: Class subprograms cannot be subprogram variables

corriep wrote:
I have run into this problem before also. There is no real work-around but in your case you could just make the hungry variable public instead of using a function to access it

There is no limitation for what the OP is trying to do. He forgot the parentheses in the function call, it is the same as doing this, which will also give you an error:
Turing:
fcn x () : int
    result 3
end x
put x

The limitation you're talking about is trying to use a class subprogram as a subprogram variable or subprogram argument. That is not what the OP is doing, but the compiler thinks that is what he's doing since he forgot the parentheses (and his function is a class subprogram).

Just do this :
Turing:
if monster (i) -> isHungry () then
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  [ 4 Posts ]
Jump to:   


Style:  
Search: