1. Phix : nested structs

Hallo,

I want to create an array of nested structs.

I came up with this (and experimented with it), and it seems to work.

But is this the right way to do it? It looks a little strange to me.

constant MAX=10 
 
 
public struct Vector2  
    atom x = 0   
    atom y = 0   
end struct 
 
public struct Camera2D   
    Vector2 offset 
    Vector2 target 
    atom rotation =10   
    atom zoom = 20      
end struct 
 
Camera2D camX  
 
 
sequence array=(repeat(0,MAX)) 
 
for i=1 to MAX do 
--Camera2D camX 
    array[i]=new(Camera2D) -- This is not enough 
    camX=array[i] 
    camX.offset=new(Vector2) -- i need also this 
    camX.target=new(Vector2) -- and this 
    camX.offset.x=i 
    camX.offset.y=camX.offset.x*i 
end for 
--?array[1].zoom --nope 
? "--" 
 
for i=1 to MAX do 
--Camera2D camX 
    camX=array[i] 
    ?camX.offset.y 
end for 
 
? "--" 
 
--?array[2].zoom 
camX=array[2] 
camX.offset.x=100 
?camX.offset.x 
 
camX=array[3] 
camX.offset.x=130 
?camX.offset.x   
 
camX=array[2] 
?camX.offset.x 
 
wait_key() 
new topic     » topic index » view message » categorize

2. Re: Phix : nested structs

Seems about right. If you store a struct in an array (inherently of possibly mixed types),
the compiler does not know that array[i] is a struct until you store it in an appropriately typed variable.
I remember something like "sequence of Camera2D" being on someone's RDS Eu wish list long before Phix was even born.

Structs are just a collection of fields, with atom/bool/string/sequence/struct|object defaulting to 0/false/""/{}/NULL.
Hence offset and target will be NULL unless you explicitly create new Vector2 for them. This should also work:

for i=1 to MAX do  
    array[i]=new(Camera2D,{new(Vector2,{i,i*i}),new(Vector2)}) 
end for  

I'm not going to try and pretend the Phix implementation of structs is particularly wonderful.
I should probably say structs/classes most likely wont be in Phix v2.0, if ever released, though v1.x won't suddenly vanish.

new topic     » goto parent     » topic index » view message » categorize

3. Re: Phix : nested structs

Thank you for your quick and detailed response.

petelomax said...

Structs are just a collection of fields, with atom/bool/string/sequence/struct|object defaulting to 0/false/""/{}/NULL.
Hence offset and target will be NULL unless you explicitly create new Vector2 for them. This should also work:

for i=1 to MAX do  
    array[i]=new(Camera2D,{new(Vector2,{i,i*i}),new(Vector2)}) 
end for  

I think such an explanation would be good for the documentation.

petelomax said...


I'm not going to try and pretend the Phix implementation of structs is particularly wonderful.

What matters to me is not whether the solution is perfect or beautiful. What matters is that it exists at all.

petelomax said...


I should probably say structs/classes most likely wont be in Phix v2.0, if ever released, though v1.x won't suddenly vanish.

What are the arguments against structs and classes, and what should replace them?

new topic     » goto parent     » topic index » view message » categorize

4. Re: Phix : nested structs

andreasWagner said...

What are the arguments against structs and classes, and what should replace them?

They can be noticeably slower than plain sequences, without offering much benefit, unless you happen to think s.field vs s[FIELD] is a game-changer, and they're not debug-friendly.
Plus as you've already found out that something like s[i].field and/or s.i.field is simply not supported, whereas in contrast there is no hindrance of any kind whatsoever on s[i][FIELD].
I haven't yet managed to dream up any replacement, maybe something'll crop up in time for 2.1... Go and/or Rust might yet inspire me, they do something neater/simpler than most.
(Then again, I'm not exactly enamoured by either syntax offerings, and certainly rather averse to those kinds of harsh, draconian, nit-picking, and downright fussy type systems.)

new topic     » goto parent     » topic index » view message » categorize

5. Re: Phix : nested structs

andreasWagner said...

What are the arguments against structs and classes, and what should replace them?

There have been plenty of proposals on Rapideuphoria about simulated structures and classes. Maybe the answer is there?

Jean-Marc

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu