SELF: mandatory ?

This forum is meant for anything you would like to share with other visitors
Post Reply
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

SELF: mandatory ?

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
markus.lorenzi@dvbern.ch
Posts: 114
Joined: Mon May 02, 2016 9:10 am
Location: Schweiz

SELF: mandatory ?

Post by markus.lorenzi@dvbern.ch »

Hi Robert,

I would prefer a warning


Markus
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

SELF: mandatory ?

Post 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.
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

SELF: mandatory ?

Post 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 ;) )
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
SHirsch
Posts: 286
Joined: Tue Jan 30, 2018 8:23 am
Location: Germany

SELF: mandatory ?

Post 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
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

SELF: mandatory ?

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

SELF: mandatory ?

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
HansjoergP
Posts: 142
Joined: Mon Jul 25, 2016 3:58 pm
Location: Italy

SELF: mandatory ?

Post 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
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

SELF: mandatory ?

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
Post Reply