Cast types

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Cast types

Post by tinman »

Like C, forcing things to be another type.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

euh, for what?
purebasic is autocasting, ie.

a.b = c.l

autocasts, so what would you need it for?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

blueznl wrote:euh, for what?
purebasic is autocasting, ie.

a.b = c.l

autocasts, so what would you need it for?

Code: Select all

Structure foo
    a.w
    b.l
EndStructure

Structure bar Extends foo
    c.l
EndStructure

; And many others

*a.foo = AllocateMemory(SizeOf(bar))

*a\c....; ah bollocks, doesn't work.
It's a laziness thing, instead of me having to create a new pointer variable of the correct type for each extended structure I have.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

It's not a cast, it's a pointer type change 8O. How would you see the new feature be implemented ?
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

Fred wrote:It's not a cast, it's a pointer type change 8O. How would you see the new feature be implemented ?
Sorry the way I wrote it wouldn't be how I'd expect it to be done. You know in C you'd have this:

Code: Select all

struct foo {
    int a; short b;
};

struct bar {
    int c;
};

bar blah;
foo *plip = (struct foo *)&blah;

(struct bar *)plip->c = 23; /* Now that would work */
But like I say, it's a laziness thing :)
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

now i understand... it's indeed something i have ran into as well...

lemme see... if it's a pointer, the pointer itself contains 4 bytes, so...

*a = whatever

*a.c = whatever

tells the compiler to use the struct c to identify fields (ie. offset) from now on... so... why not allow pointers to be retyped?

Code: Select all

structure s1
  b.b
endstructure

structure s2
  l.l
  w.w
endstructure

x.s2
x\w = 1

*y.s1
*y\b = 1

*y.s2
debug *y\l
and if you do, why not allow it to be done on one line?

Code: Select all

*y.s1\b = 1
debug *y.s2\l
that makes sense, doesn't it? in fact, doing it this way makes the choice of a backward slash for pointer fields suddenly a great decision :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

blueznl wrote:tells the compiler to use the struct c to identify fields (ie. offset) from now on... so... why not allow pointers to be retyped?
Not sure if I'd be keen on permanently changing the type, then the compiler would not be able to perform any type checking. Consider when you have a global pointer variable and some procedures that use it. How can the compiler know what type the pointer will be at when it cannot know when (during execution) the procedures would be called.

Otherwise it would get into a whole mess of polymorphism :)
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

hmmm if you want typechecking in combination with pointer you're right... but then again, what does the compiler do? run from top to bottom and translate, so in the case of a pointer struct, it sees:

*a.s1
*a\b = 1

so it first encounters typing *a as s1, then it nows the next use of a field with *a will be using the structure s1

it woudl generate an error if it, at that moment, would suddenly encounter a field name that is not part of the last known structure used with *a

so this would work (assuming

Code: Select all

structure s1 
  b.b 
endstructure 

structure s2 
  l.l 
  w.w 
endstructure 

x.s2 
x\w = 1 

*y.s1 
*y\b = 1 

*y.s2 
debug *y\l 
as the compiler KNOWS what struct was last used in combination with *y

there's not much polymorphism in there as far as i can see, or are you refering to the change of type / struct?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

could it not be done like this?

Code: Select all

*a.{newtype}\structitem = b
; or just use ordinary paranthesis?
*a.(newtype)\structitem = b
This would be fairly easy to implement..
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

why parenthesis at all?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

i mean, the var itself doesn't change, all that changes is the offset to the field depending on the name of the field and the struct
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

blueznl wrote:why parenthesis at all?...

i mean, the var itself doesn't change, all that changes is the offset to the field depending on the name of the field and the struct
I think it's easier to spot errors if you have a special way to mark that you want to use another structure with your pointer variable.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

... which is one of the reasons why that second syntax is so interesting:

normal syntax:

*a.s1
*a\fieldn = 1

means threat from now on all references to fields as if they are part of structure s1

and

*a.s1\fieldn = 1

written that way there can be no doubt about it! :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
PolyVector
Enthusiast
Enthusiast
Posts: 499
Joined: Wed Sep 17, 2003 9:17 pm
Location: Southern California
Contact:

Post by PolyVector »

If this feature had been implimented, it would have saved me from creating a variant structure to simplify my skin engine...

I like this idea a lot :)
I really do hope it gets implimented...
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

I like the *Pointer.(Structure)\Test, as it's somewhat similar to the C syntax. Blueznl solution looks good too, but it will be confusing, as the current syntax already allow *Pointer.Structure\Test = 40. If you need to cast a lot, you can use another pointer with a different structure.
Post Reply