Discussion of article "LifeHack for traders: Blending ForEach with defines (#define)" - page 3

 
fxsaber:

It's flawed (bummer for structures) and not optimal.

If we do it with a flaw, then it's the other way round, as far as I'm concerned. It is also flawed, also suboptimal, but I find it more convenient.

#define  ForEach(type, element, array)                             \
for (int __i = 0, __max = ArraySize((array)); __i < __max; __i++) \
{                                                                 \
type element = array[__i];                                        \

void OnStart()
{
   string array[] = {"12", "23", "34", "45"};
   
   ForEach(string, item, array)
      Print(item);
   }
}
 
Комбинатор:

If you're going to do it with a flaw, I think it should be the other way round. Also flawed, also suboptimal, but I find it more comfortable.

Yes, there seems to be no way out with the type. The way to get out of the previous one with the structure is this.

template <typename T>
bool Assign( const T &Value1, T &Value2 )
{
  Value2 = Value1;
  
  return(true);
}

#define  forEach(element, array)  for (int __i = 0, __max = ArraySize((array)); __i < __max && Assign(array[__i], element); __i++)

An incompleteness, in short. Especially since forEach is only for reading the array, without writing it.

 

tUT looks like work for work's sake.... :-)

Golden words:

"I myself consider the techniques given in the article as a lifehack and remain a follower of the classical MQL5 approach. Perhaps these articles will help those who are used to writing in the MQL4 style to overcome the psychological barrier when switching to the MetaTrader 5 platform, which is much more convenient in all respects."

 
Roman Shiredchenko:

tUT looks like work for work's sake.... :-)

Golden words:

"I myself consider the techniques given in the article as a lifehack and remain a follower of the classical MQL5 approach. Perhaps these articles will help those who are used to writing in the MQL4 style to overcome the psychological barrier when switching to the MetaTrader 5 platform, which is much more convenient by all parameters."

I'm sure it's normal when there are people who are ready to write three times more code with the same result.

 
To finish the forEach, you need to use ## in the macro to be able to use nested forEach.
[Deleted]  
Комбинатор:

If you're going to do it with a flaw, I think it should be the other way round. Also flawed, also suboptimal, but I find it more comfortable.

You've demonstrated one of the flaws of macros.

That you can put one bracket in it, but you have to remember to put the second bracket outside.

Don't ever do that!

 
Koldun Zloy:

You have demonstrated one of the disadvantages of macros.

Oh, to me it's better than macroing variables in the current context.

I told you directly, there is no good version and in current realities there can't be.

 
Комбинатор:

Oh, that sounds better to me than macroing variables in the current context.

I told you directly, there is no good version and in current realities there can't be.

I think this is a good version

#define  ForEach(Index, array) for (int Index = 0, max##Index = ArraySize((array)); Index < max##Index;  Index++) 

void OnStart()
{
  string array[] = {"12", "23", "34", "45"};
  
  ForEach(i, array)
  {
   Print(array[i]);
   
   ForEach(j, array)
     Print(array[j]);
  }    
}
 
fxsaber:

I think it's a good version

Yeah, it is.
 
fxsaber:

I think it's a good version

The nested ForEach is to show that there are no collisions. Right?