Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.Object F# snippets #7811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions snippets/fsharp/System/Object/Equals/equals2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module equals2

// <Snippet1>
type Point(x, y) =
new () = Point(0, 0)
member _.X = x
member _.Y = y

override _.Equals(obj) =
//Check for null and compare run-time types.
match obj with
| :? Point as p ->
x = p.X && y = p.Y
| _ ->
false

override _.GetHashCode() =
(x <<< 2) ^^^ y

override _.ToString() =
$"Point({x}, {y})"

type Point3D(x, y, z) =
inherit Point(x, y)
member _.Z = z

override _.Equals(obj) =
match obj with
| :? Point3D as pt3 ->
base.Equals(pt3 :> Point) && z = pt3.Z
| _ ->
false

override _.GetHashCode() =
(base.GetHashCode() <<< 2) ^^^ z

override _.ToString() =
$"Point({x}, {y}, {z})"

let point2D = Point(5, 5)
let point3Da = Point3D(5, 5, 2)
let point3Db = Point3D(5, 5, 2)
let point3Dc = Point3D(5, 5, -1)

printfn $"{point2D} = {point3Da}: {point2D.Equals point3Da}"
printfn $"{point2D} = {point3Db}: {point2D.Equals point3Db}"
printfn $"{point3Da} = {point3Db}: {point3Da.Equals point3Db}"
printfn $"{point3Da} = {point3Dc}: {point3Da.Equals point3Dc}"
// The example displays the following output:
// Point(5, 5) = Point(5, 5, 2): False
// Point(5, 5) = Point(5, 5, 2): False
// Point(5, 5, 2) = Point(5, 5, 2): True
// Point(5, 5, 2) = Point(5, 5, -1): False
// </Snippet1>
51 changes: 51 additions & 0 deletions snippets/fsharp/System/Object/Equals/equals3.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module equals3

// <Snippet1>
type Point(x, y) =
member _.X = x
member _.Y = y

override _.Equals(obj) =
// Performs an equality check on two points (integer pairs).
match obj with
| :? Point as p ->
x = p.X && y = p.Y
| _ ->
false

override _.GetHashCode() =
(x, y).GetHashCode()

type Rectangle(upLeftX, upLeftY, downRightX, downRightY) =
let a = Point(upLeftX, upLeftY)
let b = Point(downRightX, downRightY)

member _.UpLeft = a
member _.DownRight = b

override _.Equals(obj) =
// Perform an equality check on two rectangles (Point object pairs).
match obj with
| :? Rectangle as r ->
a.Equals(r.UpLeft) && b.Equals(r.DownRight)
| _ ->
false

override _.GetHashCode() =
(a, b).GetHashCode()

override _.ToString() =
$"Rectangle({a.X}, {a.Y}, {b.X}, {b.Y})"

let r1 = Rectangle(0, 0, 100, 200)
let r2 = Rectangle(0, 0, 100, 200)
let r3 = Rectangle(0, 0, 150, 200)

printfn $"{r1} = {r2}: {r1.Equals r2}"
printfn $"{r1} = {r3}: {r1.Equals r3}"
printfn $"{r2} = {r3}: {r2.Equals r3}"
// The example displays the following output:
// Rectangle(0, 0, 100, 200) = Rectangle(0, 0, 100, 200): True
// Rectangle(0, 0, 100, 200) = Rectangle(0, 0, 150, 200): False
// Rectangle(0, 0, 100, 200) = Rectangle(0, 0, 150, 200): False
// </Snippet1>
48 changes: 48 additions & 0 deletions snippets/fsharp/System/Object/Equals/equals4.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module equals4

// <Snippet1>
[<Struct; CustomEquality; NoComparison>]
type Complex =
val mutable re: double
val mutable im: double

override this.Equals(obj) =
match obj with
| :? Complex as c when c = this -> true
| _ -> false

override this.GetHashCode() =
(this.re, this.im).GetHashCode()

override this.ToString() =
$"({this.re}, {this.im})"

static member op_Equality (x: Complex, y: Complex) =
x.re = y.re && x.im = y.im

static member op_Inequality (x: Complex, y: Complex) =
x = y |> not

let mutable cmplx1 = Complex()
let mutable cmplx2 = Complex()

cmplx1.re <- 4.0
cmplx1.im <- 1.0

cmplx2.re <- 2.0
cmplx2.im <- 1.0

printfn $"{cmplx1} <> {cmplx2}: {cmplx1 <> cmplx2}"
printfn $"{cmplx1} = {cmplx2}: {cmplx1.Equals cmplx2}"

cmplx2.re <- 4.0

printfn $"{cmplx1} = {cmplx2}: {cmplx1 = cmplx2}"
printfn $"{cmplx1} = {cmplx2}: {cmplx1.Equals cmplx2}"

// The example displays the following output:
// (4, 1) <> (2, 1): True
// (4, 1) = (2, 1): False
// (4, 1) = (4, 1): True
// (4, 1) = (4, 1): True
// </Snippet1>
27 changes: 27 additions & 0 deletions snippets/fsharp/System/Object/Equals/equals_ref.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module equals_ref

// <Snippet2>
// Define a reference type that does not override Equals.
type Person(name) =
override _.ToString() =
name

let person1a = Person "John"
let person1b = person1a
let person2 = Person(string person1a)

printfn "Calling Equals:"
printfn $"person1a and person1b: {person1a.Equals person1b}"
printfn $"person1a and person2: {person1a.Equals person2}"

printfn "\nCasting to an Object and calling Equals:"
printfn $"person1a and person1b: {(person1a :> obj).Equals(person1b :> obj)}"
printfn $"person1a and person2: {(person1a :> obj).Equals(person2 :> obj)}"
// The example displays the following output:
// person1a and person1b: True
// person1a and person2: False
//
// Casting to an Object and calling Equals:
// person1a and person1b: True
// person1a and person2: False
// </Snippet2>
54 changes: 54 additions & 0 deletions snippets/fsharp/System/Object/Equals/equals_static2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module equals_static2

// <Snippet1>
open System

// Class constructor
type Dog(dogBreed) =
// Public property.
member _.Breed = dogBreed

override this.Equals(obj) =
match obj with
| :? Dog as dog when dog.Breed = this.Breed -> true
| _ -> false

override _.GetHashCode() =
dogBreed.GetHashCode()

override _.ToString() =
dogBreed

let m1 = Dog "Alaskan Malamute"
let m2 = Dog "Alaskan Malamute"
let g1 = Dog "Great Pyrenees"
let g2 = g1
let d1 = Dog "Dalmation"
let n1 = Unchecked.defaultof<Dog>
let n2 = Unchecked.defaultof<Dog>

printfn $"null = null: {Object.Equals(n1, n2)}"
printfn $"null Reference Equals null: {Object.ReferenceEquals(n1, n2)}\n"

printfn $"{g1} = {g2}: {Object.Equals(g1, g2)}"
printfn $"{g1} Reference Equals {g2}: {Object.ReferenceEquals(g1, g2)}\n"

printfn $"{m1} = {m2}: {Object.Equals(m1, m2)}"
printfn $"{m1} Reference Equals {m2}: {Object.ReferenceEquals(m1, m2)}\n"

printfn $"{m1} = {d1}: {Object.Equals(m1, d1)}"
printfn $"{m1} Reference Equals {d1}: {Object.ReferenceEquals(m1, d1)}"

// The example displays the following output:
// null = null: True
// null Reference Equals null: True
//
// Great Pyrenees = Great Pyrenees: True
// Great Pyrenees Reference Equals Great Pyrenees: True
//
// Alaskan Malamute = Alaskan Malamute: True
// Alaskan Malamute Reference Equals Alaskan Malamute: False
//
// Alaskan Malamute = Dalmation: False
// Alaskan Malamute Reference Equals Dalmation: False
// </Snippet1>
14 changes: 14 additions & 0 deletions snippets/fsharp/System/Object/Equals/equals_val1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module equals_val1

// <Snippet3>
let value1 = 12uy
let value2 = 12

let object1 = value1 :> obj
let object2 = value2 :> obj

printfn $"{object1} ({object1.GetType().Name}) = {object2} ({object2.GetType().Name}): {object1.Equals object2}"

// The example displays the following output:
// 12 (Byte) = 12 (Int32): False
// </Snippet3>
24 changes: 24 additions & 0 deletions snippets/fsharp/System/Object/Equals/equals_val2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module equals_val2

// <Snippet4>
// Define a value type that does not override Equals.
[<Struct>]
type Person(personName: string) =
override _.ToString() =
personName

let person1 = Person "John"
let person2 = Person "John"

printfn "Calling Equals:"
printfn $"{person1.Equals person2}"

printfn $"\nCasting to an Object and calling Equals:"
printfn $"{(person1 :> obj).Equals(person2 :> obj)}"
// The example displays the following output:
// Calling Equals:
// True
//
// Casting to an Object and calling Equals:
// True
// </Snippet4>
27 changes: 27 additions & 0 deletions snippets/fsharp/System/Object/Equals/equalsoverride.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module equalsoverride

// <Snippet6>
open System

type Person(name, id) =
member _.Name = name
member _.Id = id

override _.Equals(obj) =
match obj with
| :? Person as personObj ->
id.Equals personObj.Id
| _ ->
false

override _.GetHashCode() =
id.GetHashCode()

let p1 = Person("John", "63412895")
let p2 = Person("Jack", "63412895")
printfn $"{p1.Equals p2}"
printfn $"{Object.Equals(p1, p2)}"
// The example displays the following output:
// True
// True
// </Snippet6>
23 changes: 23 additions & 0 deletions snippets/fsharp/System/Object/Equals/equalssb1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module equalssb1

// <Snippet5>
open System
open System.Text

let sb1 = StringBuilder "building a string..."
let sb2 = StringBuilder "building a string..."

printfn $"sb1.Equals(sb2): {sb1.Equals sb2}"
printfn $"((Object) sb1).Equals(sb2): {(sb1 :> obj).Equals sb2}"

printfn $"Object.Equals(sb1, sb2): {Object.Equals(sb1, sb2)}"

let sb3 = StringBuilder "building a string..."
printfn $"\nsb3.Equals(sb2): {sb3.Equals sb2}"
// The example displays the following output:
// sb1.Equals(sb2): True
// ((Object) sb1).Equals(sb2): False
// Object.Equals(sb1, sb2): False
//
// sb3.Equals(sb2): False
// </Snippet5>
18 changes: 18 additions & 0 deletions snippets/fsharp/System/Object/Equals/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="equals_ref.fs" />
<Compile Include="equals_val1.fs" />
<Compile Include="equals_val2.fs" />
<Compile Include="equalssb1.fs" />
<Compile Include="equalsoverride.fs" />
<Compile Include="equals2.fs" />
<Compile Include="equals3.fs" />
<Compile Include="equals4.fs" />
<Compile Include="equals_static2.fs" />
</ItemGroup>
</Project>
24 changes: 24 additions & 0 deletions snippets/fsharp/System/Object/Finalize/finalize1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// <Snippet1>
open System.Diagnostics

type ExampleClass() =
let sw = Stopwatch.StartNew()
do
printfn "Instantiated object"

member this.ShowDuration() =
printfn $"This instance of {this} has been in existence for {sw.Elapsed}"

override this.Finalize() =
printfn "Finalizing object"
sw.Stop()
printfn $"This instance of {this} has been in existence for {sw.Elapsed}"

let ex = ExampleClass()
ex.ShowDuration()
// The example displays output like the following:
// Instantiated object
// This instance of ExampleClass has been in existence for 00:00:00.0011060
// Finalizing object
// This instance of ExampleClass has been in existence for 00:00:00.0036294
// </Snippet1>
Loading