Here's a way that works more closely to your original version:<br><br>instance Enumerated a => Target a where<br> convert n<br> | n >= 0 && n < numConstrs = Just (constrs !! n)<br> | otherwise = Nothing<br>
where<br> constrs = constructors<br> numConstrs = length constrs<br><br>Alternatively:<br><br>instance Enumerated a => Target a where<br> convert n<br> | n >= 0 && n < numConstrs = Just result<br>
| otherwise = Nothing<br> where<br> numConstrs = length (constructors `asTypeOf` [result])<br> result = constructors !! n<br><br>However let me warn you that you aren't going to be happy with this instance when it comes time to use this. Instead, you probably want one of the following:<br>
<br>defaultConvert :: Enumerated a => Int -> a<br>defaultConvert n<br> | n >= 0 && n < numConstrs = Just (WithEnumerated (constrs !! n))<br>
| otherwise = Nothing<br>
where<br>
constrs = constructors<br>
numConstrs = length constrs<br><br>(a)<br>instance Target SomeEnumeratedType where convert = defaultConvert<br><br>(b)<br>
newtype WithEnumerated a = WithEnumerated a<br>instance Enumerated a => Target (WithEnumerated a) where<br> convert n = WithEnumerated (defaultConvert n)<br><br>OverlappingInstances basically never does what you want in the long run.<br>
<br> -- ryan<br><br><div class="gmail_quote">On Thu, Sep 17, 2009 at 9:01 AM, Andy Gimblett <span dir="ltr"><<a href="mailto:haskell@gimbo.org.uk">haskell@gimbo.org.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im"><br>
On 17 Sep 2009, at 16:50, Daniel Fischer wrote:<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Yes, the second appearance of 'constructors' is at an unspecified type.<br>
<br>
instance (Enumerated a) => Target a where<br>
convert n<br>
| n < 0 = Nothing<br>
| otherwise = case drop n constructors of<br>
(x:_) -> Just x<br>
_ -> Nothing<br>
<br>
would make it compile.<br>
</blockquote>
<br></div>
Neat trick. It works: thanks!<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
But there'd be a risk that Target is unusable, depending on how instance resolution is<br>
done.<br>
</blockquote>
<br></div>
Unusable? How so? Sorry, but I don't follow...<div><div></div><div class="h5"><br>
<br>
-Andy<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>
</div></div></blockquote></div><br><br>