Errors, bugs, questions - page 2680

 
Koldun Zloy:

It's all right here too.

not correct!

I specifically checked it in VS2019:

class A
    {
        private int f1() { return 1; }
        private int f2() { return 2; }
        public int  f(int v) { const int a = f1(); const int b = f2(); return (v + a + b); }
    }

Error CS0133 Expression assigned to "a" must be constant. ConsoleApp2 ..\repos\ConsoleApp2\ConsoleApp2\Program.cs 13 Active

Error CS0133 Expression assigned to "b" must be constant. ConsoleApp2 ..\repos\ConsoleApp2\ConsoleApp2\Program.cs 13 Active

 
Igor Makanu:

this is not how it worked last year - I specifically tested const modifiers

and it seems that@Ilyas explained how to run an MQL-program, first all const and static are initialized, no matter where they are defined, and then the class constructors are called

ok, with this initialization static / const - it can still be properly distributed, but the fact that the const modifier does not guarantee that the compiler will check it - very unexpected - my second example

In both your examples everything works correctly and is expected.

 
Koldun Zloy:

In both your examples everything works correctly and is expected.

expectedly, const modifier is 99% sure that compiler will find my error when compiling, VS2019 does it, MQL does not do it

 
Igor Makanu:

expected const modifier is 99% sure that compiler will find my error when compiling, VS2019 does it, MQL does not do it

What exactly do you consider an error?

I don't get any errors from VS2019.

 
Igor Makanu:

last year it didn't work that way - I specifically tested const modifiers

and it seems that@Ilyas explained how to run MQL-program, first all const and static variables are initialized, no matter where they are described, and then the class constructors are called.

It seems to be global variables with const/static modifiers and you are using local variables.

 
Koldun Zloy:

What exactly do you consider an error?

I don't get any errors from VS2019.

the error is that the MQL compiler allows ambiguous expressions, if there is a const modifier, then it is a constant

this same code was nottracked by the compiler? the other situation has been tracked.

Sharp in VS2019 underlined everything before compilation.

class A{
public:
   int ivalue;
   int inc()const {static int v = 0; return(++v);}
   A():ivalue(0){}
};

class B{
public:
   int ivalue;
   int inc(){return(++ivalue);}
   B():ivalue(0){}
};

//+------------------------------------------------------------------+
void OnStart()
{
   const A a;
   Print(a.inc());  
   B b1;
   Print(b1.inc());
   const B b2;
   Print(b2.inc()); // 'inc' - call non-const method for constant object
}
//+------------------------------------------------------------------+
Sergey Dzyublik:

You must have meant global variables with const/static modifiers, while you use local ones.

I may be confused, but const modifier behavior is very different from C#

 

A variable declared with a const modifier cannot be changed. The compiler keeps track of this.

You probably expect it to do something else.

 
Koldun Zloy:

You probably expect something different from it.

Apparently yes, I expect it to be the same when jumping between MQL and C#.

 

Build 2366

namespace X
{

class A
{
public:
   A(){}
};

}  //  namespace X

class B : public X::A
{
public:
   B() : X::A()  //'X' - struct member undefined
   {
   }
};
 
Ilyas:

Thanks for the post, I will check

mq5 - normal. mq4 - broken. You can take TypeToBytes_ExampleScript.mq4 from here. It only compiles if you change the extension to mq5.

Reason: