Private functions
BlitzMax Forums/BlitzMax Beginners Area/Private functions
| ||
Ok, so I'm pretty new to this whole OOP lark but as I understand it, it's not possible in blitzmax to have a function inside a type that's only callable from within the type. i.e. a private function. Wouldn't this be good to have helper functions for methods etc.? I'm told other OOP languages have this feature. Feature request? :P |
| ||
You sort of can. You can use Public and Private but I *think* that makes functions/methods/whatever only accessible from the current source file, rather than a specific type. Might be wrong. |
| ||
But it does not work if the function is inside a type. If the type is public, all its members are. If the type is private, all its members are also private. |
| ||
That's why I said "sort of" ;) I generally use an underscore prefix for stuff that I shouldn't directly mess with. Not ideal but it works. (and yes, private methods/functions etc would be nice). |
| ||
I generally use an underscore prefix for stuff that I shouldn't directly mess with. Not ideal but it works. Yeah, that seems to be the general consensus for a 'workaround'. However, such functions will still be callable from outside the type's scope. Just seems to go against the grain of proper OOP - and good programming practice in general - IMO. |
| ||
Yes it would be really nice. As Gfk said, most Max code circulating arraound uses the underscore prefix to identify private-like methods or functions. There's another much more secure approach if you're working with modules you're planning to distribute and support, mixing private types with public types that internally deal with the privates one. It is a fast and a bit dirty approach but it works. Public Type MyClass Field PrivateData:TMyPrivateClass = New TMyPrivateClass Method DoThis() PrivateData.Privatefield = 4555 PrivateData.AnotherPrivatefield = 534 Privatedata.PrivateFunction(34) End Method End Type Private Type TMyPrivateClass Field PrivateField:int = 0 field AnotherPrivateField:int = 0 Function PrivateFunction(Value:int) ... ... ... End Function End Type Public This example shows how to create a class that internally deals with a private class on a module. Module user's won't be able to modify the private data as the public field 'PrivateData' is seen as a 'Object' field from the programs importing the module, as the datatype of this field has been properly set as private. |
| ||
Yeah, that seems like a decent workaround for a module, ziggy. Of course, in a regular program we, as the coder, know what functions/methods shouldn't be called externally. :) I just think being able to declare methods/functions directly as private/public would be nice. Of course, I have no idea how hard this would be to implement. :P |
| ||
I'd like to see these as well. Mostly for Fields, but may as well get em for everything. Properties would also be nice. |
| ||
Yes, for Fields would be good, too. |
| ||
Isn't this really the same question as wanting to have method pointers? What you could do is have a bunch of functions in a type and some wrapper methods - you store a function pointer as a field which you can map to a given function and then when you call the method it calls the function pointer. |
| ||
Not sure what you mean but it sounds way more complicated than simply being able to declare things as private/public. |
| ||
I'd like to have this too. My framework was quite far along before I found out about the _ standard and it's too late to change without breaking everyone's code. So know I just put my fields in different sections with comments above saying Flags, Read Only, Private etc. Also I note if a method/function is private as the first comment. Of course any IDEs that pick up Types' method/function names won't know this... |
| ||
Useful topic, just started on blitzmax myself. In Blitz3D I would put _Pvt at the end of the function if I shouldn't mess with it. I think I will adopt the standard prefix _ from now on. :) |