BlackFox.MasterOfFoo 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package BlackFox.MasterOfFoo --version 1.0.2
NuGet\Install-Package BlackFox.MasterOfFoo -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BlackFox.MasterOfFoo" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BlackFox.MasterOfFoo --version 1.0.2
#r "nuget: BlackFox.MasterOfFoo, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install BlackFox.MasterOfFoo as a Cake Addin
#addin nuget:?package=BlackFox.MasterOfFoo&version=1.0.2

// Install BlackFox.MasterOfFoo as a Cake Tool
#tool nuget:?package=BlackFox.MasterOfFoo&version=1.0.2

A library to allow using printf style strings in more places.

The code is essentially an extracted version of printf.fs where the environement can not only decide what to do with the final blocks that compose the string (printf put them on the console, sprintf in a buffer, ...) but also what to do with the parameters passed for each format specifier.

Sample usage

module MyModule =
    open System.Text
    open BlackFox.MasterOfFoo
    type private MySprintfEnv() =
        inherit PrintfEnv<unit, string, string>()
        let buf = StringBuilder()
        override this.Finalize() = buf.ToString ()
        override this.Write(s : PrintableElement) = ignore(buf.Append(s.FormatAsPrintF()))
        override this.WriteT(s : string) = ignore(buf.Append(s))

    let mysprintf (format: Format<'T, unit, string, string>) =
        doPrintfFromEnv format (MySprintfEnv())

MyModule.mysprintf "Hello %s." "World"

Mini-Doc

PrintableElement

PrintableElement represent an element in a string, for example sprintf "Foo %s bar" "x" produce 3 PrintableElement, the first contains the string "Foo " the second is a format specifier 's' with an associated string value "x" and then there is the string the string " Bar".

Members :

  • ElementType: Tell your if this is a string or a format specifier.
  • Value: give the value if it was a format specifier.
  • ValueType: give the type of value expected by the format specifier.
  • StarWidth: The width if specified via another parameter as in "%*i".
  • StarPrecision: The precision if specified via another parameter as in "%.*f".
  • FormatAsPrintF(): Get the string representation that printf would have normally generated.
  • Specifier: The format specification for format specifiers.

PrintfEnv

PrintfEnv is the type to implement to create a printf variant it has 3 type parameters:

  • 'State: The state of the printer, passed as argument when using '%t'.
  • 'Residue: The type that methods passed to '%t' must return.
  • 'Result: The final result type for the printer.

Members:

  • Finalize: Create the final result for this printer
  • Write: Write an element from the format string to the printer
  • WriteT: Write the result of the method provided by %t to the printer.

Functions

  • doPrintfFromEnv: Take a format and a PrintfEnv to create a printf-like function
  • doPrintf: Same as doPrintfFromEnv but allow to know the number of elements when the PrintfEnv is created.

FAQ

What does it allow exactly that can't be done with the original set of functions ?

  • Generating complex object that aren't only a string like an SqlCommand or structured logging.
  • Escaping parts in strings, like an xmlprintf that would escape < to &lt in parameters but not in the format string.

What are the limitations ?

The main limitation is that the F# compiler allow a strict set of things an you can't go differently. The function signature that is the first argument to Format<_,_,_,_,> is generated from rules in the compiler and no library can change them.

The consequence is that we're limited to what is present in the F# compiler, can't add a %Z or allow %0s to work.

Aren't you just replicating ksprintf ?

ksprintf allow you to run code on the final generated result, essentially allowing you to run code during PrintfEnv.Finalize but you can't manipualte the format specifiers or their parameters.

What this Star syntax

When * is specified for either the width or the precision an additional parameter is taken by the format to get the value.

> sprintf "%*.*f";;
val it : (int -> int -> float -> string) = <fun:it@1>
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on BlackFox.MasterOfFoo:

Package Downloads
BlackFox.ColoredPrintf

Provide a printf replacement with colors. This library provide a colorprintf function where colors can be set for a range using the syntax : $foreground;background[text] For example: colorprintf "Hello $red[%s]." "world"

Interstellar.Core

Cross-platform F# browser library

FSharp.Logf

Printf-style logging for structured loggers.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.6 6,710 4/17/2021
1.0.5 3,500 12/4/2018
1.0.4 1,201 12/4/2018
1.0.3 1,324 9/9/2018
1.0.2 16,672 7/22/2018
1.0.1 3,196 2/11/2018
0.2.1 1,598 4/28/2017
0.2.0 1,739 10/23/2016
0.1.2 1,830 10/7/2016
0.1.1 1,517 10/7/2016

Larger FSharp.Core version choice