<br>
<div class="gmail_quote"><br><br><br>Cast uses unsafeCoerce. but well, itīs more elegant ;)
<div>
<div></div>
<div class="h5"><br><br>
<div class="gmail_quote">2009/12/26 Miguel Mitrofanov <span dir="ltr"><<a href="mailto:miguelimo38@yandex.ru" target="_blank">miguelimo38@yandex.ru</a>></span><br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">
<div>On 26 Dec 2009, at 12:45, Alberto G. Corona wrote:<br><br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">You can also make use of Typeable and unsafeCoerce (Data.Dynamic style)<br><br><br>data Bar = forall a. (BarLike a, Typeable a) => Bar a<br>
<br>unwrapBar :: Bar -> a<br>unwrapBar (Bar x) = case typeof x== typeof a of<br> True -> unsafeCoerce x<br> False -> error $ "casting error for type: "++ typeOf a<br>
</blockquote><br></div>That's gross. Don't use unsafe functions unless you really need one.
<div><br><br>data Bar = forall a. (BarLike a, Typeable a) => Bar a<br></div>unwrapBar :: Typeable a => Bar -> a<br>unwrapBar (Bar x) = fromMaybe (error $ "casting error for type: " ++ show (typeOf x)) $ cast x
<div>
<div></div>
<div><br><br></div></div></blockquote>
<div> </div>
<div> </div>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">
<div>
<div><br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote"><br><br><br>2009/12/26 Miguel Mitrofanov <<a href="mailto:miguelimo38@yandex.ru" target="_blank">miguelimo38@yandex.ru</a>><br>
<br>On 26 Dec 2009, at 11:58, <a href="mailto:haskell@kudling.de" target="_blank">haskell@kudling.de</a> wrote:<br><br>class BarLike a where<br> doSomething :: a -> Double<br><br>data Bar = forall a. BarLike a => Bar a<br>
<br>unwrapBar :: Bar -> a<br>unwrapBar (Bar x) = x<br><br>How can i deconstruct the enclosed value of type a?<br><br>You can't write a function with a type that mentions existentially quantified "a". Period.<br>
<br>But you can deconstruct the enclosed value temporarily:<br><br>getSomething :: Bar -> Double<br>getSomething b =<br> case b of<br> Bar a -> doSomething a<br><br>_______________________________________________<br>
Haskell-Cafe mailing list<br><a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br><a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote><br></div></div></blockquote></div><br></div></div></div><br>