Skip to content

Instantly share code, notes, and snippets.

@jackfoxy
Last active December 23, 2015 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackfoxy/6679238 to your computer and use it in GitHub Desktop.
Save jackfoxy/6679238 to your computer and use it in GitHub Desktop.
A sandbox for F# Type explorations
namespace TypeSystemSandbox
#if INTERACTIVE
#load @"C:\FsEye\FsEye.fsx"
#I "C:\Packages"
#endif
open System
//constraints from "Constraints (F#)" MS F# Language Reference
// Base Type Constraint
type Class1<'T when 'T :> System.Exception> =
class end
// Interface Type Constraint
type Class2<'T when 'T :> System.IComparable> =
class end
// Null constraint
type Class3<'T when 'T : null> =
class end
// Member constraint with static member
type Class4<'T when 'T : (static member staticMethod1 : unit -> 'T) > =
class end
// Member constraint with instance member
type Class5<'T when 'T : (member Method1 : 'T -> int)> =
class end
// Member constraint with property
type Class6<'T when 'T : (member Property1 : int)> =
class end
// Constructor constraint
type Class7<'T when 'T : (new : unit -> 'T)>() =
member val Field = new 'T()
// Reference type constraint
type Class8<'T when 'T : not struct> =
class end
// Enumeration constraint with underlying value specified
type Class9<'T when 'T : enum<uint32>> =
class end
// 'T must implement IComparable, or be an array type with comparable
// elements, or be System.IntPtr or System.UIntPtr. Also, 'T must not have
// the NoComparison attribute.
type Class10<'T when 'T : comparison> =
class end
// 'T must support equality. This is true for any type that does not have the NoEquality attribute.
type Class11<'T when 'T : equality> =
class end
// The provided type must be a delegate type that has the specified arguments and return value; not intended for common use.
type Class12<'T when 'T : delegate<obj * System.EventArgs, unit>> =
class end
// The provided type must be an unmanaged type. Unmanaged types are either certain primitive types
// (sbyte, byte, char, nativeint, unativeint, float32, float, int16, uint16, int32, uint32, int64, uint64, or decimal),
// enumeration types, nativeptr<_>, or a non-generic structure whose fields are all unmanaged types.
type Class13<'T when 'T : unmanaged> =
class end
// Units of Measure
// Distance, meters.
[<Measure>] type m
// Distance, miles.
[<Measure>] type mile
// Time, seconds.
[<Measure>] type s
// Time, hours.
[<Measure>] type h
module console1 =
[<EntryPoint>]
let main argv =
//demo generics and type inference
let inline unsigned bitSize (zero : 'a) (one : 'a) = //could have explicitly coded (zero : ^a) (one : ^a), would have to if I had explicitly coded constraints
let one' = one + zero
let x' = one <<< (bitSize - 1)
x' ||| one'
//note type inference
let uByte = unsigned 8 0uy 1uy
let uInt16 = unsigned 16 0us 1us
let uInt32 = unsigned 32 0u 1u
let uInt64 = unsigned 64 0uL 1uL
// Units of Measure
let distance1 = 100.0<m>
let distance2 = 250.0<mile>
let speedMetersPerSec = distance1 / 10.0<s>
let speedMPH = distance2 / 5.0<h>
//let x = distance1 + distance2 //will not compile
printfn "Hit any key to exit."
System.Console.ReadKey() |> ignore
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment