Skip to content

Instantly share code, notes, and snippets.

@scvalex
Last active December 18, 2015 21:49
Show Gist options
  • Save scvalex/5850172 to your computer and use it in GitHub Desktop.
Save scvalex/5850172 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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