module call external procedure

Just starting out? Need help? Post your questions and find answers here.
jassing
Addict
Addict
Posts: 1768
Joined: Wed Feb 17, 2010 12:00 am

module call external procedure

Post 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
  
infratec
Always Here
Always Here
Posts: 6866
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: module call external procedure

Post 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()
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: module call external procedure

Post 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.
"Have you tried turning it off and on again ?"
A little PureBasic review
jassing
Addict
Addict
Posts: 1768
Joined: Wed Feb 17, 2010 12:00 am

Re: module call external procedure

Post 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.
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: module call external procedure

Post 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).
"Have you tried turning it off and on again ?"
A little PureBasic review
Post Reply