I am unclear about what the question/issue is.

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
open System.Collections.Generic 
 
let li = new List<int>()
li.Add(3)
li.Remove(3) |> ignore
printfn "%d" li.Count 
 
let o = new obj()
let o2 = new obj()
let o3 = new obj()
let lo = new List<obj>()
lo.Add(o)
lo.Add(o2)
lo.Remove(o) |> ignore
lo.Remove(o3) |> ignore
printfn "%d" lo.Count 
 
type MyDU = Foo of int
let f = Foo(3)
let f2 = Foo(3)
let f3 = Foo(3)
let ld = new List<MyDU>()
lo.Add(f)
lo.Add(f2)
lo.Remove(f) |> ignore
lo.Remove(f3) |> ignore
printfn "%d" lo.Count 
By on 8/9/2010 6:40 PM ()

My quiestion is how to treate identity of objects.

In C# and C++,I'm used to use pointer(a kind of) value as object's identity.

In F#, most of type values are treated like value type,so I'm confused.

In most of the cases,it may work well if I change my mind to treate most of them as just a value.

But if there are two list view of Schedule(suppose Schedule is discriminated union) for one collection of Schedule and I remove one Schedule from first view and want to reflect this deletion to second view,which row of second view should I delete?

In SQL,every rows have to have some unique identifier,but collection of f#'s value dosen't.

This makes me feel strange...

Should I add some fields for revealing unique identity?

By on 8/9/2010 8:07 PM ()

F# runs on the same CLR that C# does - objects have the same representation.

What may be confusing you is that F# types (which are just normal object references/pointers like C# objects) such as lists, tuples, etc., for your convenience have automatic structural equalty functionality defined so that they are equal (and compare) in "obvious" ways - so that (1,"xx") = (1,"xx"), (2,"xx") > (1,"xx"), etc. (Just as if you had defined Equals/HashCode/etc. on your class yourself with deep semantics.) If you don't want this behavior, you can tell F# not to do this for you by using attributes.

Some data-structures such as lists are immutable, but in no way are these "value types" from the CLR persective - they are still reference types, unless you define a struct - which is the same as C#.

By on 8/9/2010 9:20 PM ()

BTW, if your question was equality vs. equivalance (same object) and you want to be sure you have the *exact same object* you can use either one of these:

1
2
let inline (==) a b = LanguagePrimitives.PhysicalEquality a b
let inline (==) a b : System.Object.ReferenceEquals(a, b)
By on 8/9/2010 9:24 PM ()

Thxs for your reply.

I heared the book ,Expert F# ,explains how to treate identity of values and objects.

I'll read it.

By on 8/11/2010 7:53 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper