Access to function with the same name as method
BlitzMax Forums/BlitzMax Beginners Area/Access to function with the same name as method
| ||
I want to call a global function from within a method, but the method's type already has another method with the same name as the global function. How do I specify that I'm referring to the global function instead of the method? In C++ I'd use ::FunctionName make it clear I meant the global namespace. What's the equivalent syntax in BlitzMax?Function SetPosition (x, y) '... EndFunction Type Color Method SetPosition (x, y) '... EndMethod Method Update () SetPosition (x, y) EndMethod EndTypeIn the example code, 'SetPosition' in the Update method will call the types method of that name. How do I get to the global function outside the type? |
| ||
If it's in a module, I believe you can use the module as a prefix, e.g. brl.stream.readstring calls readstring function in the brl.stream module. No idea in terms of code in your main app though... |
| ||
As it happens, the function was in a module, so your tip did the trick. Thanks for that. I'd still be interested in knowing how to do C++'s ::identifier to refer to the global namespace, as per my original example. |
| ||
I'd guess that there might be some kind of global __blitzapp "application namespace" that one could call? But I don't fancy trying a zillion different ones to discover there isn't one... |
| ||
As far as I know, this is handled automatically by BMax. Global Functions will always called prior Methods, to force the method, you can simply use the 'self' keyword. Example: Function SetPosition (x, y) '... EndFunction Type Color Method SetPosition (x, y) '... EndMethod Method Update () SetPosition (x, y) ' will call the Global Function self.SetPosition(x, y) ' will call the Method of the Object EndMethod EndType |
| ||
- |
| ||
This is not true, try your own sample:Framework brl.basic Import brl.standardio Function SetPosition (x, y) Print "global : " + x + "," + y EndFunction Type Color Method SetPosition (x, y) Print "class : " + x + "," + y EndMethod Method Update () SetPosition (1, 2) EndMethod EndType Local c:Color = New Color c.Update()And this is fortunate because if globals were called prior to methods, this would somewhat break encapsulation. Once written, a class should be netirely self-contained and not have its behavior modify by some other piece of code you may add to your source file elsewhere. If globals were called prior to methods, and at some point you add a function like SetPosition that wasn't there before, then you have borken your class behaviour. Wayward, I was wondering the same as you, and figured I could try to simply prepend a dot (".SetPosition(1,2)") and it actually worked (called the global version). Nice isn't it :) ? |
| ||
...then you have borken your class behaviour. And you really don't want your code to get borked. |
| ||
Surely not. But I'd really like not to be so close to a dislexic guy. |
| ||
No idea in terms of code in your main app though... Use the Type name. If your function is called doSomething() and it's part of MyType, then MyType.doSomething() will call the static reference. |
| ||
Wayward, I was wondering the same as you, and figured I could try to simply prepend a dot (".SetPosition(1,2)") and it actually worked (called the global version). Nice isn't it :) ? That's what I was after; prepending a dot does it. I'd actually tried this exact syntax before posting, but it appeared not to work. I tried it again and now it does. It failed for me the first time because I had multiple functions calls and I hadn't dotted them all (duh!). |
| ||
Use the Type name. If your function is called doSomething() and it's part of MyType, then MyType.doSomething() will call the static reference. Obviously. But that's not what they were after. From the post above, it looks like a simple . modifier (no prefix) allows you to access global functions that aren't inside a type. |