Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Nice addition to Foreign: castAny
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Maurí­cio CA  
View profile  
 More options Oct 26, 10:49 pm
Newsgroups: fa.haskell
From: Maurí­cio CA <mauricio.antu...@gmail.com>
Date: Tue, 27 Oct 2009 02:49:16 UTC
Local: Mon, Oct 26 2009 10:49 pm
Subject: [Haskell-cafe] Nice addition to Foreign: castAny
This could be beside castPtr, castCharToCChar etc.

----

castAny :: (Storable a, Storable b) => a -> b
castAny = unsafePerformIO . genericCast
   where
     genericCast :: (Storable a, Storable b) => a -> IO b
     genericCast v = return undefined >>= \r ->
       allocaBytes (max (sizeOf v) (sizeOf r)) $ \p ->
         poke p v >> if False then return r else peek (castPtr p)

----

GHCi:

    > let a = -1000 :: Int16
    > castAny a :: Word16  -->
    64536
    > castAny a :: Ptr ()
    0xb4c2fc18
    > castAny (castAny a :: Ptr ()) :: Int16
    -1000

    > let b = pi :: CLDouble
    > b
    3.141592653589793
    > castAny b :: CInt
    1413754136
    > castAny b :: Ptr ()
    0x54442d18
    > castAny b :: CFloat
    3.3702806e12
    > castAny b :: Int8
    24

At minimum, this is safer than 'unsafeCoerce'. What do you think?

Best,
Maurício

_______________________________________________
Haskell-Cafe mailing list
Haskell-C...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Meacham  
View profile  
 More options Nov 3, 11:35 pm
Newsgroups: fa.haskell
From: John Meacham <j...@repetae.net>
Date: Wed, 04 Nov 2009 04:35:04 UTC
Local: Tues, Nov 3 2009 11:35 pm
Subject: Re: [Haskell-cafe] Nice addition to Foreign: castAny

Try it on a big endian architecture, or one that has alignment
restrictions, or a different size for HsChar or so forth. Casting by
'punning' (as the C folks like to call it) does have uses, but they are
generally hardware dependent and useful only in certain rare
circumstances that a generic cast probably isn't likely to fill.

        John

--
John Meacham - ⑆repetae.net⑆john⑈ - http://notanumber.net/
_______________________________________________
Haskell-Cafe mailing list
Haskell-C...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Maurí­cio CA  
View profile  
 More options Nov 7, 11:48 pm
Newsgroups: fa.haskell
From: Maurí­cio CA <mauricio.antu...@gmail.com>
Date: Sun, 08 Nov 2009 04:48:17 UTC
Local: Sat, Nov 7 2009 11:48 pm
Subject: [Haskell-cafe] Re: Nice addition to Foreign: castAny

Do you think this could be used as a way to handle
C unions? If I had something like

union example {
   struct firstview {
     char c;
     int n;
   } fv;
   long double ld;

};

and 'firstview' had been mapped in Haskell as, say,

   FirstView {firstViewC :: CChar, firstVewN :: CInt}

I could check what I would get after pokeing values
using:

(firstViewN . unionCast) (pi :: CDouble)

Note that I changed the name from castAny to unionCast
to reflect its use.

Thanks for your comments,
Maurício

_______________________________________________
Haskell-Cafe mailing list
Haskell-C...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google