xsharp.eu • SELF: mandatory ?
Page 1 of 1

SELF: mandatory ?

Posted: Tue Feb 09, 2021 11:14 am
by robert
Hello you all,

One of our customers has asked us to generate a compiler warning when the compiler chooses to call a function over a method with the same name, like in the example below

Code: Select all

CLASS Test
METHOD Left(sValue as STRING, nLen as DWORD) AS STRING
   RETURN "Test"
METHOD ToString() AS STRING
   RETURN Left("abc",2)   // This will generate a warning that the function called and not the method . 
                          // if you want to call the method you will have to prefix the call with SELF:
END CLASS
This is something that would never cause a problem in Visual Objects (and FoxPro and Xbase++) since in these languages the SELF: (THIS.) prefix is mandatory to call methods inside a class.
In X# we have adopted the C# syntax (Vulcan had that too) and we allow to call a method inside a class without SELF: prefix.

We have implemented this change for an upcoming release of the compiler and we now see MANY compiler warnings in existing code, such as in the VO SDK Classes where there is code to check parameters

Code: Select all

Default(@lParam, TRUE)
but the class also has a Default method.

So we are now wondering:
- Should we really add this warning (of course you can always disable this warning the normal way)
- Or Should we add a compiler option to enable the check ?
- Should we make SELF: (THIS. in FoxPro) mandatory to avoid these problems ?
- Or should we add a compiler option to make SELF: mandatory ?

Even when SELF: is mandatory there can still be a problem when there is a STATIC method in a class with the same name as a Function. We'll generate the compiler warning in those cases.

Any feedback would be appreciated.

Robert

SELF: mandatory ?

Posted: Tue Feb 09, 2021 12:21 pm
by markus.lorenzi@dvbern.ch
Hi Robert,

I would prefer a warning


Markus

SELF: mandatory ?

Posted: Tue Feb 09, 2021 12:35 pm
by wriedmann
Hi Robert,
since C# does not knows functions as X#, there is no ambiguity.
I would like a compiler option to make the self: mandatory.
This option should be on by default in the VO/Xbase++/VFP dialects.
Wolfgang
P.S. using the self: prefix helps reading the code, so I would consider it a good style.

SELF: mandatory ?

Posted: Tue Feb 09, 2021 1:50 pm
by FFF
I'm with Wolfgang, add at least the option.
(Personally, i'd make it mandatory - if my feeling is correct, that this would reduce the code YOU have to have in place in the compiler to handle the possibilities ;) )

SELF: mandatory ?

Posted: Tue Feb 09, 2021 3:28 pm
by SHirsch
Hi Robert,
I prefer to make the SELF: mandatory by compiler option.
The same could be for STATIC methods: prefix it with CLASS name.
var result := ClassName.StaticMethod() //which is mandatory in C#, if I remember correctly

Stefan

SELF: mandatory ?

Posted: Tue Feb 09, 2021 3:40 pm
by robert
Stefan

var result := ClassName.StaticMethod() //which is mandatory in C#, if I remember correctly

in C# you can code StaticMethod() inside the class and you need to prefix with the ClassName outside of the class. The same is true for X#.
You can omit the ClassName if you add a USING STATIC at the start of your program.

Robert

SELF: mandatory ?

Posted: Tue Feb 09, 2021 9:41 pm
by robert
Guys,

We have added something in our internal build that takes care of this. It seems to work very well.

We are considering to:
- make "SELF:" (or This.) mandatory by default in the VO, Vulcan FoxPro, Harbour and XBase++ dialects
( Btw we have inherited from Xbase++ and Harbour that you can also use "::" for "SELF:")
- Make "SELF:" optional in the Core dialect.

We will add a command line option and matching project property that allows you to enable/disable this.

If you omit SELF when it is mandatory then you will get an error like the following:

Code: Select all

error XS0120: An object reference is required for the non-static field, method, 
or property 'YourClass.YourMethod()'
(This is a standard Roslyn error message).

How does that sound ?

Robert

SELF: mandatory ?

Posted: Wed Feb 10, 2021 6:31 am
by HansjoergP
The problem is that we have now already a lot of code without self. That's why I would at least prefer it if you could also switch on a warning

Hansjörg

SELF: mandatory ?

Posted: Wed Feb 10, 2021 7:52 am
by robert
Hansjörg,

We HAVE added the warning, but this produces so many warnings in existing (old VO) code that we looked a bit further.
With the new compiler option (which we have baptized /enforceself) you will no longer see warnings like the ones that I mentioned in my earlier message (a Default() function call in a GUI class where a Default() method exists) since the compiler knows that you intend to prefix calls to the Default() method with SELF:.
If you disable this check then the compiler will warn you for these calls as well. Of course you can suppress the warnings the normal way (in the project properties or with a #pragma).

Robert