No Protected mode?

Monkey Forums/Monkey Programming/No Protected mode?

JaviCervera(Posted 2011) [#1]
I see that in Monkey you can set visibility to Private or Public within a class, but not to Protected, which would be really nice to have to allow for a field or method to only be visible to the class and its subclasses.


Xaron(Posted 2011) [#2]
Well Private isn't private anyway as you can access private members of another class if they are in the same file.

So file1.monkey:
Class A
Private
  Field _a:Int
End

Class B
...
End


Class B can access _a in Class A even though it's private. If Class B is in another file it can't access A._a


JaviCervera(Posted 2011) [#3]
Yeah, but I'd like to have each class in a separate source file.

I guess that some of the target languages do not support Protected and that's the reason why it's not included? I know C++, C# and Java all support it, but maybe JavaScript or Flash don't. It makes sense that Monkey only supports the features available in all languages.


wiebow(Posted 2011) [#4]
Thanks for that info about classes being in the same file. As I got used to create a file for each class this will not be problem for me, but it could be confusing when checking out other peoples' code.


Xaron(Posted 2011) [#5]
Yep. I've got that from the documentation.


wiebow(Posted 2011) [#6]
Oh that explains it, I haven't read those yet. Been to busy finishing my game in max before I move to monkey.


Nobuyuki(Posted 2013) [#7]
sorry to zombie-bump this thread, but a Protected directive would be really keen right about now, and I'm hoping maybe there's a chance it still might be implemented if requested enough.

In a class I've written, I'm stuck between making my Private fields public to let a subclass I wrote access them (and thus be dangerously accessible from outside the class, as well as being messy and ugly), having the subclass in the same module as the base class (messy, ugly, and not acceptable for writing a portable library), or making all the private fields inline local and calling a bunch of redundant (and slow) code in multiple methods. I've read in another thread about this an argument against having Protected level scope in Monkey, but I think it provides useful functionality in some circumstances and I'm hoping it'll be a feature someday.


invaderJim(Posted 2013) [#8]
I second this. The Protected keyword would be great to have, as well as having the Private keyword properly privatize fields and methods within a class.


Why0Why(Posted 2013) [#9]
I didn't realize that a private field could be viewed outside of the class. Is there a valid reason for this?


Nobuyuki(Posted 2013) [#10]
I'm guessing it has something to do with the way Monkey sees scope on the module level. Maybe the Private directive (as it exists now) was included mainly because it was easy to make it work somewhat like how it would be expected in other languages with just a simple addition or change to the linker -- something which would've already need to be coded to deal with stuff like Import and Extern directives. (I don't actually know for sure, and I'm a bit too lazy to dig into the trans source to find out!)