racket - How to make C-style union -


i can't find out how make c-style union. in example given in documentation:

(define a-union-type (_union (_list-struct _int _int)         (_list-struct _double _double))) (define a-union-val     (cast (list 3.14 2.71)           (_list-struct _double _double)           a-union-type)) 

all works. if cast changed _int:

(define a-union-val     (cast (list 3 2)           (_list-struct _int _int)           a-union-type)) 

i following error:

cast: representation sizes of , types differ   size of type: 8   size of size: 16 

which somehow makes sense question is, how create union?

could please explain me difference between make-union-type , _union also? unclear me docs.

thanks lot.

cast works when (ctype-sizeof from-type) equals (ctype-sizeof to-type), largest union variant(s) allowed. try using manual allocation , union-set! instead:

(define a-union-val (ptr-ref (malloc a-union-type) a-union-type)) (union-set! a-union-val 0 (list 3 2)) 

where second parameter of union-set! index of variant want.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -