Forums

Call a private method from a shared method?

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Call a private method from a shared method?

Postby jcsmnt0 » Fri Sep 28, 2012 12:41 pm

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?
jcsmnt0
 
Posts: 2

Re: Call a private method from a shared method?

Postby nerdzero » Fri Sep 28, 2012 6:08 pm

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?
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: Call a private method from a shared method?

Postby Charles » Fri Sep 28, 2012 8:17 pm

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
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Call a private method from a shared method?

Postby jcsmnt0 » Sat Sep 29, 2012 11:09 am

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!
jcsmnt0
 
Posts: 2


Return to Discussion

Who is online

Users browsing this forum: No registered users and 26 guests