At runtime formats are just thin wrappers around strings: the strings must be dynamically reinterpreted by a runtime interpreter to build a function value that matches the checkes made by the the type checker.

If you are feeling brave, you should take a look at the printf.fs implementation in the F# distribution. Most of the code there could be reused when giving other interpretations to format strings: indeed, if anyone wanted to implement the OCaml Format module then most of the bits and pieces required for the format string interpretation are here.

However it's no joke when I say that this represents some of the more difficult F# coding around: it lives at the boundary between dynamic and static programming, a place where F# excels (in the sense that it's amazing that things like this are possible at all). Here's a comment from that file

// The general technique used this file is to interpret
// a format string and use reflection to 'fluff-up' a function value that matches
// the specificaiton of the format string.
//
// Generics add some serious complications here - we have to generate
// a function value of exactly the right runtime type, though the most
// natural scheme is to produce one of type 'obj -> obj'. We get around
// this by using a semi-reflective approach to creating and invoking
// function values of the right type. This comes with some
// overheads (though they are not too bad) and thus could and should be
// optimized in some special cases.

Don

By on 4/27/2007 2:43 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper