Cast types
- tinman
- PureBasic Expert
- Posts: 1102
- Joined: Sat Apr 26, 2003 4:56 pm
- Location: Level 5 of Robot Hell
- Contact:
Cast types
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)
(WinXPhSP3 PB5.20b14)
euh, for what?
purebasic is autocasting, ie.
a.b = c.l
autocasts, so what would you need it for?
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... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
- tinman
- PureBasic Expert
- Posts: 1102
- Joined: Sat Apr 26, 2003 4:56 pm
- Location: Level 5 of Robot Hell
- Contact:
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.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
(WinXPhSP3 PB5.20b14)
- tinman
- PureBasic Expert
- Posts: 1102
- Joined: Sat Apr 26, 2003 4:56 pm
- Location: Level 5 of Robot Hell
- Contact:
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:Fred wrote:It's not a cast, it's a pointer type change 8O. How would you see the new feature be implemented ?
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 */
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
(WinXPhSP3 PB5.20b14)
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?
and if you do, why not allow it to be done on one line?
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 
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
Code: Select all
*y.s1\b = 1
debug *y.s2\l

( 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... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
- tinman
- PureBasic Expert
- Posts: 1102
- Joined: Sat Apr 26, 2003 4:56 pm
- Location: Level 5 of Robot Hell
- Contact:
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.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?
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)
(WinXPhSP3 PB5.20b14)
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
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?
*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
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... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
could it not be done like this?
This would be fairly easy to implement..
Code: Select all
*a.{newtype}\structitem = b
; or just use ordinary paranthesis?
*a.(newtype)\structitem = b
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... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
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... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
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.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
... 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!
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... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
-
- Enthusiast
- Posts: 499
- Joined: Wed Sep 17, 2003 9:17 pm
- Location: Southern California
- Contact: