Implicit casting for Nullable in XSharp 2.7

This forum is meant for questions and discussions about the X# language and tools
Post Reply
leon-ts
Posts: 429
Joined: Fri Feb 03, 2017 1:43 pm

Implicit casting for Nullable in XSharp 2.7

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

Implicit casting for Nullable in XSharp 2.7

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
leon-ts
Posts: 429
Joined: Fri Feb 03, 2017 1:43 pm

Implicit casting for Nullable in XSharp 2.7

Post 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
Best regards,
Leonid
Post Reply