
Hello I try to have a generic way to open and close ressources for my software class DataResource a where data OpenedDataResource a :: * acquireDataResource :: a -> IO (OpenedDataResource a) releaseDataResource :: OpenedDataResource a -> IO () the idea is to have a generic method like this withDataResource :: DataResource a => a -> (OpenedDataResource a -> IO r) -> IO r withDataResource r = bracket (acquireDataResource r) releaseDataResource and later another one for the bracket of Pipes. So I try with something simple -- File newtype H5FilePath = H5FilePath FilePath instance DataResource H5FilePath where data OpenedDataResource H5FilePath = File acquireDataResource (H5FilePath f) = openFile (pack f) [ReadOnly] Nothing releaseDataResource = closeFile but I end up with this sort of error src/Hkl/H5.hs:231:40: error: • Couldn't match type ‘File’ with ‘OpenedDataResource H5FilePath’ Expected type: IO (OpenedDataResource H5FilePath) Actual type: IO File • In the expression: openFile (pack f) [ReadOnly] Nothing In an equation for ‘acquireDataResource’: acquireDataResource (H5FilePath f) = openFile (pack f) [ReadOnly] Nothing In the instance declaration for ‘DataResource H5FilePath’ | 231 | acquireDataResource (H5FilePath f) = openFile (pack f) [ReadOnly] Nothing | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ src/Hkl/H5.hs:232:25: error: • Couldn't match type ‘OpenedDataResource H5FilePath’ with ‘File’ Expected type: OpenedDataResource H5FilePath -> IO () Actual type: File -> IO () • In the expression: closeFile In an equation for ‘releaseDataResource’: releaseDataResource = closeFile In the instance declaration for ‘DataResource H5FilePath’ | 232 | releaseDataResource = closeFile | ^^^^^^^^^ cabal: Failed to build hkl-0.1.0.0 (which is required by exe:binoculars-ng from hkl-0.1.0.0). I do not understand why ? and more seriously, I do not know how to fix this. thanks for your help Frederic

On Mon, 14 Mar 2022, PICCA Frederic-Emmanuel wrote:
So I try with something simple
-- File
newtype H5FilePath = H5FilePath FilePath
instance DataResource H5FilePath where data OpenedDataResource H5FilePath = File acquireDataResource (H5FilePath f) = openFile (pack f) [ReadOnly] Nothing releaseDataResource = closeFile
but I end up with this sort of error
It means that openFile returns File type but your instance requires that it returns OpenedDataResource H5FilePath. That is, you must wrap the result of openFile in the File data constructor. I.e.: acquireDataResource (H5FilePath f) = File <$> openFile (pack f) [ReadOnly] Nothing releaseDataResource (File f) = closeFile f Since you used "data" instead of "type" in your type class, your data types are custom types per instance.
participants (2)
-
Henning Thielemann
-
PICCA Frederic-Emmanuel