On Sat, Nov 24, 2012 at 12:07 PM, Erik de Castro Lopo <span dir="ltr"><<a href="mailto:mle+hs@mega-nerd.com" target="_blank">mle+hs@mega-nerd.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi all,<br>
<br>
Below my .sig is a little Persist/Esqueleto program that works correctly.<br>
<br>
The getUserCount function does thr right thing and returns the number of<br>
rows in the User table. However, what I'd like is a generic function<br>
that returns the row count of any table, something vaguely like this:<br>
<br>
<br>
queryRowCount :: tableType -> SqlPersist IO Int64<br>
queryRowCount tableName = do<br>
[Value x] <- select . from $ \(_ :: SqlExpr (Entity tableName)) -><br>
return countRows<br>
return x<br>
<br>
<br>
Is there any way to do this? Its probably possible in Agda, but can it<br>
be made to work with GHC?<br></blockquote><div><br>How about something like this: <br><br> {-# LANGUAGE ScopedTypeVariables #-}<br><br> queryRowCount :: forall tableType. Proxy tableType -> SqlPersist IO Int64<br>
queryRowCount _ = do<br>
[Value x] <- select . from $ \(_ :: SqlExpr (Entity tableType)) -><br>
return countRows<br>
return x<br><br>The Proxy type is defined as:<br><br> data Proxy a = Proxy<br><br>You can use the 'tagged' package to get it.<br><br>ScopedTypeVariables and the explicit forall are needed to make sure the type 'tableType' from the top level signature is the same as the one used later.<br>
<br>Erik<br></div></div></div>