Private functions

BlitzMax Forums/BlitzMax Beginners Area/Private functions

big10p(Posted 2008) [#1]
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


GfK(Posted 2008) [#2]
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.


ziggy(Posted 2008) [#3]
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.


GfK(Posted 2008) [#4]
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).


big10p(Posted 2008) [#5]
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.


ziggy(Posted 2008) [#6]
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.


big10p(Posted 2008) [#7]
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


Jim Teeuwen(Posted 2008) [#8]
I'd like to see these as well. Mostly for Fields, but may as well get em for everything.

Properties would also be nice.


big10p(Posted 2008) [#9]
Yes, for Fields would be good, too.


ImaginaryHuman(Posted 2008) [#10]
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.


big10p(Posted 2008) [#11]
Not sure what you mean but it sounds way more complicated than simply being able to declare things as private/public.


Grey Alien(Posted 2008) [#12]
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...


boomboom(Posted 2008) [#13]
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. :)