File: Generics.fs

package info (click to toggle)
mono 6.12.0.199%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,192 kB
  • sloc: cs: 11,181,844; xml: 2,850,076; ansic: 689,413; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,845; makefile: 19,951; sh: 15,030; python: 4,771; pascal: 925; sql: 859; sed: 16; php: 1
file content (28 lines) | stat: -rw-r--r-- 1,287 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module Generics

// In the following code example, makeList is generic, 
// even though neither it nor its parameters are explicitly declared as generic.
let makeList a b = [a; b]

// You can also make a function generic by using the single quotation mark syntax 
// in a type annotation to indicate that a parameter type is a generic type parameter. 
// In the following code, function1 is generic because its parameters are declared in this manner, as type parameters.
let function1 (x: 'a) (y: 'a) =
    printfn "%A %A" x y

// You can also make a function generic by explicitly 
// declaring its type parameters in angle brackets (<type-parameter>)
let function2<'T> x y =
    printfn "%A, %A" x y


type Map2<[<EqualityConditionalOn>]'Key,[<EqualityConditionalOn>][<ComparisonConditionalOn>]'Value when 'Key : comparison and 'Value : comparison> = class
    //member this.Item ('Key) : 'Value (requires comparison)
//    member Item : key:'Key -> 'Value with get
    member this.fffff : option<int> = None
    member this.l : list<int> = [ 1; 2; 3 ]
    member this.c : Choice<int, float> = Choice1Of2 0
    member this.c2 : Choice<int, float> = Choice2Of2 0.5
    member this.r : ref<int>  = ref 0
    member this.s : seq<int>  = seq { for i in 1 .. 10 do yield i * i }    
end