Features of the mql5 language, subtleties and tricks - page 38

 
fxsaber:

Rudiment functions (not needed)

  • StringToDouble
  • StringToInteger
  • StringToTime
  • StringToColor
  • StringAdd
  • StringConcatenate
  • GetPointer


why?
 
Konstantin:
Why?
Everything is done through the appropriate operators.
 
fxsaber:
Everything is done through the appropriate operators.

I don't quite understand, give me an example
 
Konstantin:

I don't quite understand, give me an example

Forum on trading, automated trading systems and trading strategies testing

Peculiarities of mql5 language, tips and tricks

fxsaber, 2017.04.26 15:16

All standard types are set to each other
void OnStart()
{
  string Str = "1.23qwedfg";
  
  Print((int)Str);
  Print((double)Str);
}

Result

1
1.23

 
fxsaber:



Ah, that's what you mean )) let them have them now that they have them )) otherwise the code will suddenly stop working as it did with the structures, at least they introduced union and in a sense we can get by with a crutch ))
 
fxsaber:

Rudiment functions (not needed)

  • GetPointer

And this function is duplicated by what?
 
Vasiliy Pushkaryov:
What does this function duplicate?
& Object
 

Using union instead of casting structures

Since build 1596 explicit conversion of structures of different types to each other is forbidden. Such an example that works with the old builds will not compile now:

struct struct_double
{
   double value;
}sd;

struct struct_long
{
   ulong value;
}sl;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   sl = (struct_long)sd;
}
cannot cast 'struct_double' to 'struct_long'    CastingValues.mq5       33      9

However, in MQL now it is possible to combine data into a union structure. Because of this, it is possible to cast types through a union:

union struct_union
{
   double d_value;
   ulong  l_value;
}su;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   su.l_value = 0;
   su.d_value = 3.14159;
   printf((string)su.l_value);
}
2017.04.27 13:20:03.375 CastingValues (Si-6.17,M1)      4614256650576692846

This structure is already available in the new builds.

p.s. Please ask the developers to start fixing the documentation in casting:https://www.mql5.com/ru/docs/basis/types/casting.

 
Vasiliy Sokolov:

Using union instead of casting structures

Forum on trading, automated trading systems and strategy testing

Libraries: TypeToBytes

fxsaber, 2017.04.26 10:49

Random structure casting is available
struct STRUCT2
{
  MqlTick Tick;
};

// Кастинг произвольных простых структур
  STRUCT2 Struct2 = _C(STRUCT2, Tick);   // Аналог классического кастинга STRUCT2 Struct2 = (STRUCT2)Tick;
union does not allow to do casting for structures with custom constructor and/or copy operator.
 
fxsaber:
union does not allow for casting for structures with custom constructor and/or copy operator.

There was no mention of completely replacing the casting with union.

p.s. I looked at your code:

class CASTING
  {
  public:
    template <typename T1>
    static const T Casting( const T1 &Value )
    {
  #ifdef  TYPETOBYTES_FULL_SLOW
      T Data = {0};

      const int handle = ::FileOpen("Casting.tmp", FILE_READ | FILE_WRITE | FILE_BIN);

      if (handle != INVALID_HANDLE)
      {
        ::FileWriteStruct(handle, Value);

        ::FileSeek(handle, 0, SEEK_SET);
        ::FileReadStruct(handle, Data);

        ::FileClose(handle);
      }

      return(Data);
  #else // TYPETOBYTES_FULL_SLOW
      union CAST

Arbitrary casting via write/read binary... Lights out. Don't read any further...

Reason: