xs0030 ?

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

xs0030 ?

Post by FFF »

Folks,
erroy xs0030: cannot convert shortint to logic.
I got this here (from Willie's rightsle):
IF !LOGIC(_And(GetKeyState(VK_SHIFT), SHORTINT(_CAST, 0x8000)))
but also in a simplified
LOCAL n := 2 AS INT
? LOGIC(n)
? (LOGIC)n

Searched through x# help for casts/conversion, but didn't find something suitable ;), VO-help as an article (Type Casting and Conversion Operators) following which i'd expect the convert to work.

Any idea (obviously i can workaround, but...)?

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

xs0030 ?

Post by Chris »

Hi Karl,

Make it a cast instead of conversion and it will work:

LOGIC(_CAST,n)

Maybe we should support also the regular conversion syntax, for the VO/Vulcan dialects, but I am not sure if allowing it is a good idea. Conversion for example from usual to logic is ok, but INT to LOGIC? With _CAST at least you denote to the compiler that you're doing that on purpose and not accidentally.

Chris
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

xs0030 ?

Post by FFF »

Chris,
indeed, this works.
But i'm not sure, i understand the underlying logic ;)
The cited article in Vo-Help states:
...
Unlike the type conversion operators, the type casting operators change only the data type of an expression, not its value: Long(_CAST, TODAY())means take the date value Today() and use it as a long integer.
...

Doesn't that mean in my case, the compiler has to have a rule how to translate e.g. 1238 to True? And if so, why not do the conversion? A warning "implicit conversion" might be helpful.

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

xs0030 ?

Post by Chris »

Hi Karl,

In VO/win32, you can work directly with memory locations, go to any memory location and treat the contents of the bytes there (CAST them) as if it was the location where a LOGIC is stored. Or even a DATE, STRING, PSZ, anything, and that goes for both reading and writing, which is in big part responsible for many of the hard to find bugs in code that result to corrupted memory etc, this is why "unmanaged" code is considered unsafe.

In .Net, you do not work with direct memory locations, there's instead an abstraction layer, the IL language, in which you cannot do casting in the VO/c++ etc sense, only conversion. So actually the commands INT(num) and INT(_CAST,num) are translated to the exact same IL code in x# (and vulcan). We only differentiate the _CAST syntax just as an indication from the programmer to the compiler that he knows what he's doing with the data, so for example there's never overflow checking when "casting" from one numeric type to another.

About the LOGIC<->INT case, AFAIK also in VO there's no such conversion, only a cast, treating the memory location as a LOGIC or INT. As I said in .Net you cannot do that, so it's done with a conversion, but we're allowing it only with the _CAST syntax which is saying to the compiler "yes, I really want to do that, despite being a bit strange". Otherwise, if LOGIC(num) was allowed, it could very possibly just be a typo from the programmer who in fact intended to do a different "valid" conversion. So the compiler points out such possible user errors, unless the _CAST syntax is used.

Chris
Chris Pyrgas

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