Private Methods Error

Monkey Forums/Monkey Programming/Private Methods Error

Tibit(Posted 2012) [#1]
I can't make methods private.

Anyone else experiencing this?


ziggy(Posted 2012) [#2]
no. can you provide source code so we can see what are you actually tring to do?


Tibit(Posted 2012) [#3]
You are Right


Function Main:Int()
	Local test:MyClass = new MyClass
	test.Test1()
	test.Test2()
	test.Test3()
	test.Test4()
End

Class MyClass
Private
	Method Test1:Void()
		Print "test1"
	End
Public
	Method Test2:Void()
		Print "test2"
	End
Private
	Method Test3:Void()
		Print "test3"
	End
	Method Test4:Void()
		Print "test4"
	End
End

Do you get an error? btw Zigge, I see all 4 in Jungle too. but sometimes private/public works in jungle.


ziggy(Posted 2012) [#4]
Aaaaah I see...
In Monkey Private and Public is based on a module scope, not in a class scope, so your code is correct. You will always be able to access any private member from within the same module (the same file) where they're created. Monkey encapsulation is designed in a per-file basis.


MrHanson(Posted 2012) [#5]
Still doesn't seem to work even if you put them in separate files. Is Private and Public only for Fields? In my opinion, this behavior should be changed. I think if you have a Private Method or Field in a class that is in the same file it should still be invisible to other classes.


Tibit(Posted 2012) [#6]
Ah right.. duh I think I knew that :)

But it should always coamplain when called from "another" file right?


Tibit(Posted 2012) [#7]
Yes it works as expected for fields


ziggy(Posted 2012) [#8]
@MrHanson: As far as I know, it works for any class member. Post an example otherwise, so we can take a look and see if a bug report has to be generated


MrHanson(Posted 2012) [#9]
I just took his example and put them in separate files (sorry for the formatting).


'--------TestClasses.monkey--------

Class MyClass
Private
Method Test1:Void()
Print "test1"
End
Public
Method Test2:Void()
Print "test2"
End
Private
Method Test3:Void()
Print "test3"
End
Method Test4:Void()
Print "test4"
End
End

'-----Test.monkey----

Import TestClasses

Function Main:Int()
Local test:TestClasses.MyClass = New TestClasses.MyClass()
test.Test1()
test.Test2()
test.Test3()
test.Test4()
End

Only tried it on the GLFW target and the html5 target.
Works the same on both. It prints:

test1
test2
test3
test4


Tibit(Posted 2012) [#10]
@MrHansen - A Tip is to use [ Code ] and [/Code] to paste code. btw Great that you tested it. I get the same, but unsure if it is *always* so.

Ziggy can you show an example where it works as expected? I know it has worked as expected once upon a time or maybe it was just a dream :)


Samah(Posted 2012) [#11]
Tibit don't forget there's also the [ monkeycode ] tags now too. :)

[monkeycode]Function Main()
Print "Monkey is awesome!"
End[/monkeycode]


Tibit(Posted 2012) [#12]
What? That is just AWESOME! Did not know. Thanks for the tips Samah! :)


jpoag(Posted 2012) [#13]
Just to weigh in before anything is 'fixed.' I like the module level access better than class level access. The reason being that if you create class level access descriptors, then you need to have 'friend' classes.

Private methods are broken, they should certainly be fixed.

In parser.monkey in the method "ParseClassDecl" around line 1477, when the parser encounters "private", it sets the private flag for the decl_attrs, but not the func_attrs.

[monkeycode]
Case "private"
NextToke
decl_attrs=decl_attrs | DECL_PRIVATE
func_attrs=func_attrs | DECL_PRIVATE
Case "public"
NextToke
decl_attrs=decl_attrs & ~DECL_PRIVATE
func_attrs=func_attrs & ~DECL_PRIVATE[/monkeycode]

Fixes it, but I've already encountered a few libraries that this causes issues with, namely the json library from Damian Sinclair. That needs a 'Public' method added after the New() in tokeniser.monkey @ line 55.


jpoag(Posted 2012) [#14]
BTW, when I was testing this, it didn't create a 'function private' error message.

To get that message, comment out line 524 of decl.monkey
[monkeycode]' If Not func.CheckAccess() Continue[/monkeycode]


Tibit(Posted 2012) [#15]
Great, that is a very simple fix.

Should be fixed, and any library that my mistake have used a private method will have to be fixed - but that fix should be trivial.


ziggy(Posted 2012) [#16]
What Tibit said.


muddy_shoes(Posted 2012) [#17]
I've already encountered a few libraries that this causes issues with, namely the json library from Damian Sinclair.


I've no doubt that it would cause issues with stuff I've written. My relationship with Monkey's access modifier mechanisms and syntax is strained, possibly even estranged.


jpoag(Posted 2012) [#18]
I love the stuff you've written. If I didn't, I wouldn't use it.

I thought the JSON lib was a good example because it's a great module that could take the criticism. If I were to name a smaller module or lib that wasn't as great, I'd be worried about hurting someone's pride.

no worries


muddy_shoes(Posted 2012) [#19]
Don't worry. I didn't mean to sound offended or defensive, just agreeing that there would be problems.

I should probably put a warning on my code that I don't much concern myself with Private/Public usage for the most part. I tried initially but found that Monkey's mechanisms are so blunt that it's mostly not worth bothering with considering the generally limited numbers of users and small-scale of projects.


Gerry Quinn(Posted 2012) [#20]
My philosophy is not to fret too much about private members, but to be very careful with write access (as distinct from read).


AaronK(Posted 2012) [#21]
+1 for a fix too.

I find private members extremely important, especially when writing reusable code. The public part of the class is your contract with the client. If people can go and just start using all the private parts of your classes, trouble will ensue when you change how you implement that public interface.