
-----------------------------------
Clayton
Thu Dec 14, 2006 4:07 pm

Can't pass functions within a class....
-----------------------------------
hey guys, I'm having trouble. First of all, if you haven't seen my String Class, and the discussion thats been taking place there, I suggest you do as you will then know more thouroughly what I'm trying to do.

Now, In my string class, I have a function to remove punctuation, or spaces, or whatever. Now, because I have multiple checks to do, I figured (with some help from wtd) that I should just pass a function into my main generic function that removes said characters from the main string. However, it appears that you cannot use class subprograms as subprogram variables. So for example, the following is illegal:


class Foo
    export qux
    
    proc bar (f : function x : string) 
        put f
    end bar

    fcn wooble : string
        result "hey"
    end wooble

    proc qux
        bar (wooble)
    end qux
end Foo

var baz : ^Foo

new Foo, baz

baz -> qux


I know all I've done here is (in theory) made a really complex way to say "hey", but it illustrates the point. You can't do such a thing, as wooble is a class subprogram, and apparently you can't pass class subprograms, so my question to you is, is it possible to get around this some other way?

-----------------------------------
Tony
Thu Dec 14, 2006 4:15 pm


-----------------------------------
what if you declear that function outside of a class?

-----------------------------------
Clayton
Thu Dec 14, 2006 4:17 pm


-----------------------------------
Then you can pass it, however, then it's not as portable, and that's not exactly what I want. If it comes to it, I suppose I could take it out and leave it so that you have to pass a function to the generic method that takes care of everything else, but I don't really want to...

-----------------------------------
ericfourfour
Thu Dec 14, 2006 5:01 pm


-----------------------------------
The only problem with Tony's suggestion is that you cannot use it with units.

-----------------------------------
Clayton
Fri Dec 15, 2006 9:54 am


-----------------------------------
exactly, and that's what I want to be able to do. Does anybody have any theory why class functions can't be passed anyways?

-----------------------------------
Tony
Fri Dec 15, 2006 9:58 am


-----------------------------------
could you pass the class itself, and call the function from within?

-----------------------------------
Clayton
Fri Dec 15, 2006 10:00 am


-----------------------------------
that might work, I don't have time to try right now, but I will when I get the chance

-----------------------------------
wtd
Fri Dec 15, 2006 10:00 am


-----------------------------------
Because they shouldn't be able to be used this way, given the style of OOP Turing implements.

Functions inside a class carry around an extra "this" or "self" context, and as such are not the same as plain old functions.

-----------------------------------
Clayton
Fri Dec 15, 2006 10:04 am


-----------------------------------
Hmmm, nevermind, that won't work, the function that you pass is still a class subprogram, even when referenced through self. I still don't understand why you can't use class subprograms as parameters, that just seems dumb to me.
