Export toList which gets lists from the stack
Having this function can serve as documentation by making the pushed
type more explicit. It also helps to avoid problems with overlapping
instances.
@@ -44,6 +44,7 @@ module Foreign.Lua.Types.FromLuaStack
, Result
, peekEither
, pairsFromTable
+ , toList
) where
import Data.ByteString (ByteString)
@@ -126,7 +127,14 @@ instance FromLuaStack String where
peek = fmap T.unpack . peek
instance FromLuaStack a => FromLuaStack [a] where
- peek n = catchLuaError (go . enumFromTo 1 =<< rawlen n) amendError
+ peek = toList
+
+instance (Ord a, FromLuaStack a, FromLuaStack b) => FromLuaStack (Map a b) where
+ peek idx = fromList <$> pairsFromTable idx
+
+-- | Read a table into a list
+toList :: FromLuaStack a => StackIndex -> Lua [a]
+toList n = (go . enumFromTo 1 =<< rawlen n) `catchLuaError` amendError
where
go [] = return []
go (i : is) = do
@@ -134,9 +142,6 @@ instance FromLuaStack a => FromLuaStack [a] where
(ret:) <$> go is
amendError err = throwLuaError ("Could not read list: " ++ show err)
-instance (Ord a, FromLuaStack a, FromLuaStack b) => FromLuaStack (Map a b) where
- peek idx = fromList <$> pairsFromTable idx
-
-- | Read a table into a list of pairs.
pairsFromTable :: (FromLuaStack a, FromLuaStack b) => StackIndex -> Lua [(a, b)]
pairsFromTable idx = do
