Re: [racket-users] FFI | Unions

2016-04-04 Thread Pedro Caldeira
> No. Racket's evaluation model doesn't include the notion of an
> allocation stack that is associated with a continuation.
> 
> You could implement some form of stack manually, but heap allocation
> (especially as managed by the GC) is usually the way to go.

I see, so Racket only stores a reference. However we still need to use the 
union-ptr procedure when using the union as a pointer parameter in a foreign 
function, right? Why is that?

Thank you again for your previous reply.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] FFI | Unions

2016-04-04 Thread Matthew Flatt
At Sat, 2 Apr 2016 12:18:45 -0700 (PDT), Pedro Caldeira wrote:
> The way you've proposed allocates the union on the heap, is it
> possible to declare an union on the stack?

No. Racket's evaluation model doesn't include the notion of an
allocation stack that is associated with a continuation.

You could implement some form of stack manually, but heap allocation
(especially as managed by the GC) is usually the way to go.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] FFI | Unions

2016-04-02 Thread Pedro Caldeira
On Friday, 1 April 2016 00:11:30 UTC+2, Matthew Flatt  wrote:
> At Thu, 31 Mar 2016 15:00:35 -0700 (PDT), Pedro Caldeira wrote:
> > Hello everyone,
> > 
> > I am trying to use a set of bindings to SDL2 and I am at loss on how to use 
> > C 
> > unions.
> > 
> > If I understood correctly both make-union-type and _union procedures create 
> > a 
> > new ctype; but how do you actually create instances of these unions?
> 
> One way is to use `malloc` to allocate space and `ptr-ref` to treat the
> memory as a union:
> 
>  #lang racket
>  (require ffi/unsafe)
> 
>  (define _iord (_union _int64 _double))
> 
>  (define u (ptr-ref (malloc _iord) _iord))
>  (union-set! u 1 4.2)
>  (union-ref u 0)

Thank you for your reply, it is much clearer now.

The way you've proposed allocates the union on the heap, is it possible to 
declare an union on the stack?

Thanks again for your attention.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] FFI | Unions

2016-03-31 Thread Matthew Flatt
At Thu, 31 Mar 2016 15:00:35 -0700 (PDT), Pedro Caldeira wrote:
> Hello everyone,
> 
> I am trying to use a set of bindings to SDL2 and I am at loss on how to use C 
> unions.
> 
> If I understood correctly both make-union-type and _union procedures create a 
> new ctype; but how do you actually create instances of these unions?

One way is to use `malloc` to allocate space and `ptr-ref` to treat the
memory as a union:

 #lang racket
 (require ffi/unsafe)

 (define _iord (_union _int64 _double))

 (define u (ptr-ref (malloc _iord) _iord))
 (union-set! u 1 4.2)
 (union-ref u 0)

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] FFI | Unions

2016-03-31 Thread Pedro Caldeira
Hello everyone,

I am trying to use a set of bindings to SDL2 and I am at loss on how to use C 
unions.

If I understood correctly both make-union-type and _union procedures create a 
new ctype; but how do you actually create instances of these unions?

Thank you for your attention.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.