Page 1 of 1

Call a private method from a shared method?

PostPosted: Fri Sep 28, 2012 12:41 pm
by jcsmnt0
If I have:
Code: Select all
class Test
    def a
        pass

    def b is shared
        .a()

I get the compilation error:
Code: Select all
error: Cannot access instance member "a" from the current shared member. Make the member "shared" or create an instance of the type.


Is there a way to call a private method from a shared method?

Re: Call a private method from a shared method?

PostPosted: Fri Sep 28, 2012 6:08 pm
by nerdzero
Sure, just make the member "shared" or create an instance of the type ;)

By default, a method will be public. So, in this case the issue is that you don't have an instance of the Test class from which to call the a method as opposed to a being private. Check this out:

class Test
def a
print "a was called"

def b is shared
print "b was called"
Test().a # we need an instance of 'Test' in order to call 'a'

class TestProgram

def main
"""
Prints...
b was called
a was called
"""
Test.b


Makes sense?

Re: Call a private method from a shared method?

PostPosted: Fri Sep 28, 2012 8:17 pm
by Charles
While we're on the subject:
class A

def thisIsPublic
pass

def _thisIsProtected
pass

def __thisIsPrivate
pass

And you don't have to use underscores. You can use explicit declaration:

class A

def thisIsPublic is public
pass

def thisIsProtected is protected
pass

def thisIsPrivate is private
pass

Finally, the accessibility (public, protected, private, internal) is separate from the "level" (instance, shared).

HTH

Re: Call a private method from a shared method?

PostPosted: Sat Sep 29, 2012 11:09 am
by jcsmnt0
Finally, the accessibility (public, protected, private, internal) is separate from the "level" (instance, shared).


That's the bit that I was missing in my understanding - I thought shared was synonymous with public. Thanks to both of you for the good explanations!