Bug: Returned function pointer can't be called

 
#property version "1.00"
#property indicator_chart_window

typedef void (*Callback)(int i);

void PrintNumber(int i) {
  Print(i);
}

Callback getCallback() {
  return PrintNumber;
}

int OnInit() {
  getCallback()(1);
  // Error '1' - some operator expected
  return INIT_SUCCEEDED;
}

int OnCalculate(
  int const TOTAL,
  int const CALCULATED,
  int const BEGIN,
  double const & VALUE[]
) {
  return TOTAL;
}

A workaround is to create a temporary variable:

int OnInit() {
  Callback x = getCallback();
  x(1);
  return INIT_SUCCEEDED;
}
 
Alexandre Borela:A workaround is to create a temporary variable:
MQL does not support function pointers. MQL just looks like C/C++ but it is its own language. Read the documentation. Don't just assume that it works like C/C++.
 
Fernando Carreiro:
MQL does not support function pointers. MQL just looks like C/C++ but it is its own language. Read the documentation. Don't just assume that it works like C/C++.

It does on the latest builds, that's why this code works:

int OnInit() {
  Callback x = getCallback();
  x(1);
  return INIT_SUCCEEDED;
}

The other one is just a shortcut, the MQL5's parser is not recognizing that getCallback's result is a function pointer/descriptor.

int OnInit() {
  // This is equivalent to the other code.
  getCallback()(1);
  return INIT_SUCCEEDED;
}
Documentation on MQL5: Language Basics / Data Types / User-defined Types
Documentation on MQL5: Language Basics / Data Types / User-defined Types
  • www.mql5.com
User-defined Types - Data Types - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Alexandre Borela: It does on the latest builds, that's why this code works: The other one is just a shortcut, the MQL5's parser is not recognizing that getCallback's result is a function pointer/descriptor.

Ok, that that is news to me! I stand corrected then!

EDIT: At which build did this start?

EDIT2: Looks like I have been blind! Apparently this has been available for many years now. I looked at a 2017 documentation file and it was there too. Guess I was not doing a very good job of keeping up-to-date on things.

 
Fernando Carreiro:

Ok, that that is news to me! I stand corrected then!

EDIT: At which build did this start?

EDIT2: Looks like I have been blind! Apparently this has been available for many years now. I looked at a 2017 documentation file and it was there too. Guess I was not doing a very good job of keeping up-to-date on things.

I did find this some days ago too, got excited to implement an event emitter and found this minor issue, which wasn't that bad,

I was able to do it but it might get other people by surprise too. Still love MQL5, it is getting closer and closer to C++.

 
This is an ancient limitation of the MQL5. This also applies to an array of pointers.

The developers know about this from the very beginning.
Reason: