Code Monkey home page Code Monkey logo

dependenttypesprovider's Introduction

Dependent Types Provider for Domain-driven Development

The goal of this project is to allow certain primitive types to be contrained according to rules for your domain. Loosely defined this means dependent types for certain primitives (string, number types).

Some code:

open FSharp.DependentTyping

// let's define our product description to be 10-2000 characters
open FSharp.DependentTypes.Strings
type ProductDescription = BoundedString<10, 2000>

// factory methods available for all types
ProductDescription.TryCreate("").IsNone // true
ProductDescription.TryCreate("what a groovy library").IsSome // true

// if you don't care about safety, use the constructor
let ``boom!`` = ProductDescription(null)
ProductDescription.TryCreate(null).IsNone // true

// These are actually System.String!
type TwitterHandle = BoundedString(1, 15)
let b = Name("CAIndy")
let (s  : string) = upcast b // warning "String has no proper subtypes"
Assert.AreEqual("CAIndy", b) // yup!

// It goes to 11
open FSharp.DependentTypes.Numbers
type VolumeLevel = BoundedByte<11> // lower bound defaults to 0

// all number types are supported
[<Literal>] let PerihelionKm = 147.1e+6
[<Literal>] let AphelionKm   = 152.1e+6
type DistanceToSunKm = BoundedDouble<PerihelionKm,AphelionKm>
// yes, Units of measure are coming soon :)

#Discussion

The constructor and factory method are both here because sometimes you trust your inputs and sometimes you don't. This approach should incur less overhead than using a single-case discriminated union, as the type is erased to string (or numeric type).

Request for Feedback

There's already some good guidance in the community on dealing with constrained strings as semantically richer types, but I think this approach is lighter weight.

Is there a feature here you'd like to see? Let me know!

Roadmap

That's all for now. Here's the plan thus far:

  • PatternString accepting RegEx string type parameter

  • Units of measure for bounded numbers

  • NuGet love

  • Cross-targetting

  • C# Interop tests

Copyright 2016 Christopher Atkins

dependenttypesprovider's People

Contributors

caindy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.