Page 1 of 1
module call external procedure
Posted: Sat Nov 17, 2018 9:09 am
by jassing
When in a module, is there anyway to use a function not declared in it? (short of including it in each module)
something like
Code: Select all
Procedure.s WinErrorMessage()
procedurereturn "Some Error"
EndProcedure
DeclareModule test
Declare.s WinErrMsg()
EndDeclareModule
Module test
Procedure.s WinErrMsg()
ProcedureReturn WinErrorMessage()
EndProcedure
EndModule
Re: module call external procedure
Posted: Sat Nov 17, 2018 9:29 am
by infratec
The only way is like described in the Help:
Code: Select all
DeclareModule GlobalModule
Declare.s WinErrorMessage()
EndDeclareModule
Module GlobalModule
Procedure.s WinErrorMessage()
ProcedureReturn "Some Error"
EndProcedure
EndModule
DeclareModule test
Declare.s WinErrMsg()
EndDeclareModule
Module test
Procedure.s WinErrMsg()
ProcedureReturn GlobalModule::WinErrorMessage()
EndProcedure
EndModule
Debug test::WinErrMsg()
Re: module call external procedure
Posted: Sat Nov 17, 2018 1:08 pm
by luis
It's not the only way.
If you want to keep the global function outside of any stupid module, you can pass its address to the module.
Code: Select all
Procedure.s WinErrorMessage()
ProcedureReturn "Some Error"
EndProcedure
DeclareModule test
Declare RegisterFunc(*fp)
Declare.s WinErrMsg()
EndDeclareModule
Module test
Prototype.s WinErrorMessage()
Procedure RegisterFunc(*fp)
Global WinErrorMessage.WinErrorMessage = *fp
EndProcedure
Procedure.s WinErrMsg()
ProcedureReturn WinErrorMessage()
EndProcedure
EndModule
test::RegisterFunc(@WinErrorMessage())
Debug test::WinErrMsg()
But unless you have some other constraints against it (sometimes you do), it's easier to just put your main program inside a module Main and then call Main::func() from any other module.
Re: module call external procedure
Posted: Sun Nov 18, 2018 5:56 am
by jassing
infratec wrote:
The only way is like described in the Help:
Thanks -- but there is
always another way to do something, people are creative, and often find work arounds.
When adding to existing code, especially when you are limited to writing your own code so it doesn't adversely effect existing code, but requires you to use pre-existing procedures/functions makes putting global functions in a module an unlikely alternative...
luis wrote:
But unless you have some other constraints against it (sometimes you do), it's easier to just put your main program inside a module Main and then call Main::func() from any other module.
Interesting idea, but like the 'global module', I think not so practical when updating existing code.
I like the passing the address idea; another way that I started to do before I asked, was to move shared procedures to a dll; and just import/declare them in the beginning of each module; but this has it's own problems too...
Would be nice to have a "DeclareExternal" to indicate the procedure is outside the module.
Thanks.
Re: module call external procedure
Posted: Sun Nov 18, 2018 1:54 pm
by luis
jassing wrote:
Would be nice to have a "DeclareExternal" to indicate the procedure is outside the module.
I believe all could be more simply fixed by just adding the possibility to access the implicitly defined "global module" by using the "::" qualifier like other languages do.
There is no need to invent something new, the problem has already been solved.
So inside your module you would write MyModule::MyFunc() or MyFunc() to call a function in the module scope and ::MyFunc() to call a function defined in the global scope.
Same thing for constants: #MYCONST and ::#MYCONST
But the developers said no to this.
Constants defined in residents already break the module barrier BTW.
PB modules are a strangely limited beast in my opinion, it's too bad they were not fixed/extended when they were introduced and some people commented about these shortcomings because I was very happy about the idea of a namespace-like model when initially announced.
I don't think they are gonna change it now (but I would love to be wrong).