New conditional operator or what?

This forum is meant for questions and discussions about the X# language and tools
Post Reply
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

New conditional operator or what?

Post by FFF »

Looking with ILSpy 3 and Fabrice' plugin into some code, i see things like:
self:cRptFile := (self:cRptFile ?? "")
Is this a new short form of default? Or something from the spy-integration?
Or is this old and i did miss it ;-?

Good night!
Karl
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

New conditional operator or what?

Post by Chris »

Hi Karl,

This is c#'s "Null coalescing" operator according to https://docs.microsoft.com/en-us/dotnet ... operators/

x ?? y – returns x if it is non-null; otherwise, returns y.

(I know, it makes code so easy to read :))

Probably ILSpy processed some code which looked like

IF self:cRptFile == NULL
self:cRptFile := ""
ENDIF

or similar and translated it into the ?? form. But this operator is not supported in X# and the plugin probably did not know how to translate it, so it left it like that.

One solution might be for Fabrice to extend this code to an IF statement, but I don't know if he has such freedom in the plugin. Maybe translate it to an iif() statement instead?

Another solution would be to add support for this operator in the compiler, but personally I'd really prefer not to :)

Chris
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

New conditional operator or what?

Post by wriedmann »

Hi Chris,

PMFJI, but was not added a default operator in the last versions of X#?

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

New conditional operator or what?

Post by robert »

Wolfgang,

Yes you are right. There is now a DEFAULT expression:

Code: Select all

a := b DEFAULT ""
This sets a to b, but if b is NULL then it sets it to ""

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

New conditional operator or what?

Post by Chris »

Oops!
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Post Reply