Page 1 of 2

Cast types

Posted: Thu Jun 17, 2004 10:03 pm
by tinman
Like C, forcing things to be another type.

Posted: Fri Jun 18, 2004 7:03 am
by blueznl
euh, for what?
purebasic is autocasting, ie.

a.b = c.l

autocasts, so what would you need it for?

Posted: Fri Jun 18, 2004 9:32 am
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.

Posted: Fri Jun 18, 2004 11:38 am
by Fred
It's not a cast, it's a pointer type change 8O. How would you see the new feature be implemented ?

Posted: Fri Jun 18, 2004 11:45 am
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 :)

Posted: Fri Jun 18, 2004 11:53 am
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 :-)

Posted: Fri Jun 18, 2004 11:57 am
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 :)

Posted: Fri Jun 18, 2004 12:23 pm
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?

Posted: Fri Jun 18, 2004 12:38 pm
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..

Posted: Fri Jun 18, 2004 1:21 pm
by blueznl
why parenthesis at all?

Posted: Fri Jun 18, 2004 1:26 pm
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

Posted: Fri Jun 18, 2004 2:08 pm
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.

Posted: Fri Jun 18, 2004 4:13 pm
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! :-)

Posted: Fri Jun 18, 2004 4:29 pm
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...

Posted: Fri Jun 18, 2004 5:12 pm
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.