No Protected mode?
Monkey Forums/Monkey Programming/No Protected mode?
| ||
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. |
| ||
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 |
| ||
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. |
| ||
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. |
| ||
Yep. I've got that from the documentation. |
| ||
Oh that explains it, I haven't read those yet. Been to busy finishing my game in max before I move to monkey. |
| ||
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. |
| ||
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. |
| ||
I didn't realize that a private field could be viewed outside of the class. Is there a valid reason for this? |
| ||
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!) |