Casting Operator

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Casting Operator

Post 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? :)
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds