Page 1 of 1

Casting Operator

Posted: Wed Nov 16, 2011 1:57 am
by Shield
Hi :)


Regarding recent topics about pointers here I'd like to make the following suggestion for a casting operator.

The problem is that PB only supports "lose" type safety which can result in unwanted errors,
as this example demonstrates:

Code: Select all

Define a.i = 10
Define b.i = 3
Define c.i = 0.0
Define d.i = 0.0

c = 5 * (a / b)
d = 5.0 * (a / b)

Debug c ; 15
Debug d ; 17
A Cast operator could solve this problem because it gives the programmer the possibility
to specify an explicit type for the result of the expression.

I'm proposing a syntax similar to SizeOf and OffsetOf, which could look like this:

Code: Select all

; value = Cast(type, expression)
a = Cast(Integer, expression)
This is especially useful when pointers are involved. For example, in a Windows callback,
'lparam' is used to pass different information to the procedure. 'lparam' can either be a regular
numerical value but also a pointer. What it actually is, is defined by the message value.

This means that 'lparam' has to be casted into different types / structures and currently
we have to create a pointer for each type / structure so we can modify the value directly.
In my opinion, this is very annoying. A cast operator would also solve this problem as you
could just do something like this:

Code: Select all


Debug Cast(SomeStructure, lparam)\SomeValue
Debug Cast(SomeOtherStructure, lparam)\SomeOtherValue


What do you guys think? :)