Me cansé de desempaquetar instancias Data.Text
todo el tiempo antes de imprimirlas para la depuración y pensé simplemente usar Text.Printf
para eso. Por desgracia, no pude hacer que funcione:Text.Printf with Data.Text?
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Data.Text
import Text.Printf
--instance PrintfArg Text where
-- toUPrintf = toUPrintf . unpack
main :: IO()
main = do
let input :: Text = "abc"
printf "Input: %s\n" input
El error:
src/Main.hs:12:3:
No instance for (PrintfArg Text)
arising from a use of `printf'
Possible fix: add an instance declaration for (PrintfArg Text)
In a stmt of a 'do' block: printf "Input: %s" input
In the expression:
do { let input :: Text = "abc";
printf "Input: %s" input }
In an equation for `main':
main
= do { let input :: Text = ...;
printf "Input: %s" input }
Después descomentando la declaración de la instancia:
src/Main.hs:7:7:
`toUPrintf' is not a (visible) method of class `PrintfArg'
src/Main.hs:7:19: Not in scope: `toUPrintf'
¿Alguna idea?
editado
Como se sugirió, intentó TH, sigue sin ir:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
import Data.Text
import Language.Haskell.TH
import Text.Printf
runQ [d| instance PrintfArg Text where toUPrintf = toUPrintf . unpack|]
main :: IO()
main = do
let input :: Text = "abc"
printf "Input: %s\n" input
error:
src/Main.hs:9:40:
'toUPrintf' is not a (visible) method of class 'PrintfArg'
src/Main.hs:9:52: Not in scope: 'toUPrintf'
Ayuda! Es sorprendente que esto no funcione de la caja dado todos los consejos para usar Data.Text por defecto.
obtener el paquete printf y exportación de la clase PrintfArg y el uso que en lugar del paquete de costumbre. – augustss
@augustss: printf está en la base, no es un paquete separado. ¿Y tal vez se podrían exportar suficientes cosas para que los usuarios puedan crear sus propias instancias de PrintfArg? –
Agregaré un módulo Text.Printf.Internal para exponer todas las clases de tipos y tipos implicados. – augustss