Repository navigation
Expand file tree
/
Copy path04TypesAndObjects.fs
More file actions
74 lines (59 loc) · 2.03 KB
/
Copy path04TypesAndObjects.fs
File metadata and controls
74 lines (59 loc) · 2.03 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// aka a record
// Records are essentially sealed classes with extra topping: default immutability, structural equality, and pattern matching support.
// https://github.com/adelarsq/fsharp-cheatsheet#TypesAndLiterals
module fSharpExamples.TypesAndObjects
type Animal = {
name: string
noises: string list
}
type Person =
{
name : string
hobbies : string list
}
static member Empty =
// doesn't compile { name = "", hobbies = [] }
{
name = ""
hobbies = []
}
let Kevin =
{
Person.Empty with
name = "Kevin"
hobbies = ["guitar"; "biking"; "reading"]
}
let Kevin2 = {
name = "Kevin"
hobbies = ["guitar"; "biking"; "reading"]
}
let Joey = {
Person.Empty with
name = "Joey"
}
let dog =
{
name = "Toto"
noises = ["bark"]
}
// if you only have name, it takes the type that is closest above.
// let d = { name = "K" } // Person, until Animal is moved down
// classes are different
// https://fsharpforfunandprofit.com/posts/classes/
// created with a ctor, have () after the class name
// You could use "this" or "self" or "me" or any other word that commonly indicates a self reference.
type CustomerName(firstName:string, middleInitial, lastName) =
member _.FirstName = firstName
member _.MiddleInitial = middleInitial
member _.LastName = lastName
member _.AddOne x = x + 1
let customer = CustomerName("Kurt", "M", "Woolworth")
// immutable
// customer.LastName <- "kjkl";
// "F# classes which are defined outside a module are generated as normal top-level .NET classes, which is probably what you want." modules are a static class
// https://fsharpforfunandprofit.com/posts/type-extensions/#downsides-of-methods
// "So, a plea for those of you new to functionally programming. Don't use methods at all if you can, especially when you are learning.
// They are a crutch that will stop you getting the full benefit from functional programming."
// Tuples
// https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/tuples
// let t = ("one", "two", "three")