
-----------------------------------
Dusk Eagle
Sat Apr 04, 2009 11:07 pm

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".


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.

-----------------------------------
richcash
Sun Apr 05, 2009 4:28 am

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
Sun Apr 05, 2009 1:54 pm

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
Sun Apr 05, 2009 2:55 pm

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
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:
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 :if monster (i) -> isHungry () then
