Ghost-functions?

 

It sounds a bit sarcastic, maybe impossible to do as well, but if you can give any trick/workaround, please have a look! 
(the red-colored lines gives you details of the areas in discussion). The goal is to : not to import functions, not to define functions in global scope, either.

//+------------------------------------------------------------------+
//                                               ghostFunction.mq4   |
//+------------------------------------------------------------------+

#property copyright "Copyright 3077AD, MetaQuotes Software Corp."
#property indicator_chart_window

//want to define body of the function in form of macros, or something like that................
#define GHOST_FUNCTION_MACRO "string  join(string a, string b){ return a+b;}"// THIS LOOKS BATSHIT CRAZY,YE I KNOW MAN

/////////////////////////////////////////////////////////
int OnInit()

  {
   EventSetTimer(3);

   return(INIT_SUCCEEDED);

  }

////////////////////////////////////
int OnCalculate(const int rates_total,

                const int prev_calculated,
               .................................
               ................................
                        )

  {

   Alert(join("good", " day!"));//this requires us to have the join() function defined in global scope, right?

   return(rates_total);

  }
///////////////////////////
void OnTimer() 
{
......... 
}

//retrieve or define the ghost-function(join()), from the MACRO (above) OR via some other tricks maybe??
GHOST_FUNCTION_MACRO................??
//+------------------------------------------------------------------+
 
Tusher Ahmed:

It sounds a bit sarcastic, maybe impossible to do as well, but if you can give any trick/workaround, please have a look! 
(the red-colored lines gives you details of the areas in discussion). The goal is to : not to import functions, not to define functions in global scope, either.

...
#define JOIN_MACRO(a,b) a+b
...

    Alert(JOIN_MACRO("good", " day!"));

...
 
Petr Nosek:

Hi Peter, thanks for passing by.

how about defining a full-fledge/complex function, rather-than simple string-concatenation? (example ->)

void DeleteAllObjects()
{
   int objs = ObjectsTotal();
   string name;
   for(int cnt=ObjectsTotal()-1;cnt>=0;cnt--)
   {
      name=ObjectName(cnt);
      if (StringFind(name,"xpMA",0)>-1) ObjectDelete(name);
      ObjectsRedraw();
   }
}

How to avoid defining function-body in global-scope OR importing them from dlls, but still being able to use the function just like all other normal functions?

Maybe the function will reside in memory, not in physical global-scope???? (well this is exactly why i called it "GHOST FUNCTION" :D )

 
Tusher Ahmed:

Hi Peter, thanks for passing by.

how about defining a full-fledge/complex function, rather-than simple string-concatenation? (example ->)

How to avoid defining function-body in global-scope OR importing them from dlls, but still being able to use the function just like all other normal functions?

Maybe the function will reside in memory, not in physical global-scope???? (well this is exactly why i called it "GHOST FUNCTION" :D )

I'm sorry, I don't know what you really want to do. Why do you want to avoid defining function-body in global-scope (where would you like to define it?) if you want to be able to use the function?
 
Tusher Ahmed:

Hi Peter, thanks for passing by.

how about defining a full-fledge/complex function, rather-than simple string-concatenation? (example ->)

How to avoid defining function-body in global-scope OR importing them from dlls, but still being able to use the function just like all other normal functions?

Maybe the function will reside in memory, not in physical global-scope???? (well this is exactly why i called it "GHOST FUNCTION" :D )

Is it what you mean ?

#define GHOST_FUNCTION_DeleteAllObjects                     \
  {                                                         \
   int objs=ObjectsTotal();                                 \
   string name;                                             \
   for(int cnt=ObjectsTotal()-1;cnt>=0;cnt--)               \  
     {                                                      \
      name=ObjectName(cnt);                                 \
      if(StringFind(name,"xpMA",0)>-1) ObjectDelete(name);  \
      ObjectsRedraw();                                      \
     }                                                      \
  }

void OnStart()
  {
   GHOST_FUNCTION_DeleteAllObjects
  }
 

to put it another way :


how to define a function/method from string??

A JavaScript example comes to mind :

//Create string representation of function
var s = "function test(){  alert(1); }";

//"Register" the function
eval(s);

//Call the function
test();

Anyway to register function in MQL?

 
Alain Verleyen:

Is it what you mean ?

that great sir, I'm thinking on  it!

 
string func="Alert(\"less than 5\");";

#define GHOST_FUNCTION(x)    (func!=NULL)?func:x   

int OnInit()
  {
   GHOST_FUNCTION(7);//I'm expecting it to pop up the alertbox
  }

Suppose, the string(which is basically a "whole-function" converted into a literal-string) comes from a remote server/webrequest. I want to turn it into a function via macros, that can be used/called anywhere!


This is almost close to what exactly i'm trying to achieve!

 
Alain Verleyen:

Is it what you mean ?

What is the advantage/reason (in this case) in using a macro instead of simple calling the function?

void DeleteAllObjects()
{
   int objs = ObjectsTotal();
   string name;
   for(int cnt=ObjectsTotal()-1;cnt>=0;cnt--)
   {
      name=ObjectName(cnt);
      if (StringFind(name,"xpMA",0)>-1) ObjectDelete(name);
      ObjectsRedraw();
   }
}

void OnStart()
  {
   DeleteAllObjects();
  }
 
Tusher Ahmed:

Suppose, the string(which is basically a "whole-function" converted into a literal-string) comes from a remote server/webrequest. I want to turn it into a function via macros, that can be used/called anywhere!


This is almost close to what exactly i'm trying to achieve!

Now I hope I understand what you want to do. But you can't achieve it in MQL IMO.
 
Tusher Ahmed:

to put it another way :


how to define a function/method from string??

A JavaScript example comes to mind :

Anyway to register function in MQL?

Not possible in mql.