xsharp.eu • Implicit casting for Nullable in XSharp 2.7
Page 1 of 1

Implicit casting for Nullable in XSharp 2.7

Posted: Tue Dec 01, 2020 9:31 am
by leon-ts
Hi,

When migrating from XSharp 2.6 to 2.7, the code with implicit casting for Nullable variables stopped compiling.
Error: XS0266 Cannot implicitly convert type 'int?' to 'dword'. An explicit conversion exists (are you missing a cast?)

Code example:

Code: Select all

LOCAL a AS BYTE[]
a := <BYTE>{ 1, 2, 3 }
LOCAL dwLength AS DWORD
dwLength := a?:Length
/vo7 option enabled in project.

I was able to easily work around this problem by explicitly casting to DWORD.

Code: Select all

dwLength := (DWORD)a?:Length
But at the same time I want to clarify, is this an omission in the 2.7 compiler, relative to 2.6, or is it intended?

Best regards,
Leonid

Implicit casting for Nullable in XSharp 2.7

Posted: Tue Dec 01, 2020 9:59 am
by robert
Leonid,

We had to change this because there was a problem with the implicit conversions that was difficult to fix.
And since Nullable types are "new" in .Net we decided that it would be better to make the conversion explicit.
But of course, we should have documented this decision.

Btw: your example has 2 "implicit" conversions:
- From int? to int
- From int to dword

Robert

Implicit casting for Nullable in XSharp 2.7

Posted: Tue Dec 01, 2020 10:06 am
by leon-ts
Robert,
Thanks for clarifying!

In my project, there was only one similar line of code, so its modification (explicit casting of types) did not cause any difficulties.

Best regards,
Leonid