[web-devel] Esqueleto type trickery question

Erik Hesselink hesselink at gmail.com
Sat Nov 24 13:54:57 CET 2012

On Sat, Nov 24, 2012 at 12:07 PM, Erik de Castro Lopo
<mle+hs at mega-nerd.com>wrote:

> Hi all,
> Below my .sig is a little Persist/Esqueleto program that works correctly.
> The getUserCount function does thr right thing and returns the number of
> rows in the User table. However, what I'd like is a generic function
> that returns the row count of any table, something vaguely like this:
>     queryRowCount :: tableType -> SqlPersist IO Int64
>     queryRowCount tableName = do
>         [Value x] <- select . from $ \(_ :: SqlExpr (Entity tableName)) ->
>                         return countRows
>         return x
> Is there any way to do this? Its probably possible in Agda, but can it
> be made to work with GHC?

How about something like this:

    {-# LANGUAGE ScopedTypeVariables #-}

    queryRowCount :: forall tableType. Proxy tableType -> SqlPersist IO
    queryRowCount _ = do
        [Value x] <- select . from $ \(_ :: SqlExpr (Entity tableType)) ->
                        return countRows
        return x

The Proxy type is defined as:

  data Proxy a = Proxy

You can use the 'tagged' package to get it.

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/web-devel/attachments/20121124/007779fa/attachment.htm>

More information about the web-devel mailing list