ShapeSifter is a type-safe datatype-generic programming library for F#. It offers a type-safe and extensible way to inspect, decompose and create values for various kinds of common F# and .NET types.
Get Started
| Product | Versions |
|---|---|
| .NET Standard |
README
ShapeSifter
Type-safe datatype-generic programming for F#.
Getting started
The most useful place to start is likely with the tType<'a> function and corresponding active patterns in the Patterns module.
These patterns reflectively determine what type 'a was, and give you evidence in the form of a Teq (see TypeEquality).
Where appropriate, you also get a type-safe representation of the type's structure.
Here is a brief example.
Everything written here was forced by the types: once we chose to match on the Unit and Record active patterns, there was only one way to write this function so that it compiled.
open ShapeSifter
open ShapeSifter.Patterns
let manipulateType<'a> () =
match tType<'a> with
| Unit teq ->
printfn "'a was a unit type, and `teq` witnesses this!"
| Record data ->
{ new RecordConvEvaluator<_> with
member _.Eval (fieldData : RecordTypeField list) (fieldTypes : TypeList<'ts>) (conv : Conv<'a, 'ts HList>) =
failwith "manipulate the type here"
}
|> data.Apply
| _ -> failwith "unrecognised type"
Inside the RecordConvEvaluator, we have gained access to:
- The list
fieldDataof record fields, telling us the name of each field and any attributes that were on the field (as well as the rawPropertyInfoassociated with each field). - The same list of field types, but expressed as a
HeterogeneousCollections.TypeList. - A
Conv(converter) which lets us interchange between an'aand a heterogeneous list of its field values.
We have now seen a pattern for a primitive type (Unit) and for an arbitrary record.
Using the patterns in ShapeSifter, we can recognise the following types:
- Many primitive types, and
DateTimeandTimeSpan Array<_>,_ list,Seq<_>,Set<_>Option<_>Map<_, _>_ * _,_ * _ * _, and arbitrary tuples_ -> _Dictionary<_,_>,ResizeArray<_>Teq<_, _>- Records and unions
- "Sums of products" (that is, unions, but where we give you easier access to the products which make up the union fields).
More examples
See the tests for examples demonstrating how to perform type-safe manipulation of various different types. There is a whistlestop tour and a specific example of type-safe CSV parsing.
Credits
This library was originally built by Nicholas Cowle.
Downloads by Version
Similar & Alternative Packages
Hand-picked matching NuGet libraries based on tags & ecosystem
Depended On By
Find packages that declare this package as a dependency. NuGet does not provide a reverse-dependency endpoint, so candidates are checked on demand.
Adoption Assessment
Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.