Skip to content

Instantly share code, notes, and snippets.

@scvalex
Last active Dec 18, 2015
Embed
What would you like to do?
Make strings work like printf.
23. Hello, World!
{-# LANGUAGE OverloadedStrings, FlexibleInstances, UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
import Data.String ( IsString(..) )
import Text.Printf ( PrintfType, printf )
instance (PrintfType a) => IsString a where
fromString s = printf s
main :: IO ()
main = do
let x = 23 :: Int
s = "World!" :: String
"%d. Hello, %s\n" x s
@scvalex

This comment has been minimized.

Copy link
Owner Author

@scvalex scvalex commented Jun 25, 2013

The associated blog post is here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment