I think kvb makes an excellent point--you can do it with the code that he's posted but just because you can doesn't make it a great approach. Casting everything from and to System.Object isn't going to give you the best performance.

By on 8/30/2010 5:13 PM ()

F#'s maps are ordered data structures, so the keys need to support some way of being compared. If you really need to support objects as keys (which seems like an odd requirement), you could create a simple wrapper type supporting comparison:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
type ComparableWrapper(o) =
  static let list = ResizeArray()
  do
    if not (list.Contains(o)) then
      list.Add(o)
  interface System.IComparable with
    member x.CompareTo(:? ComparableWrapper as y) =
      list.IndexOf(o).CompareTo(list.IndexOf(y.ToObject()))
  member x.ToObject() = o

let wrap o = ComparableWrapper(box o)
let unwrap (c:ComparableWrapper) = c.ToObject()

let m = Map[wrap 1, box 1]

Note that the running time of the comparison function here will be proportional to the number of unique objects wrapped, so this technique should not be used if you are going to have huge maps. In that case, you could come up with a more efficient mechanism to compare two arbitrary objects (such as by using their hash codes if they are unequal as quick first test).

By on 8/30/2010 11:48 AM ()

Ask a silly question, etc... ;)

I see now that I am able to Map *to* obj without trouble, it's only mapping *from* obj that's a problem. It's interesting that System.Object doesn't just implement IComparable with GetHashCode.

I'll likely work around this by not using obj as the key. Your workaround is cute, but has terrible performance implications that rule it out from anything but the most trivial use.

I appreicate your help in any case.

Regards,
-Harold

By on 8/31/2010 9:28 AM ()

It's interesting that System.Object doesn't just implement IComparable with GetHashCode.

What should CompareTo return if two objects yield the same hash code? I don't know if obj.GetHashCode still relies on the SyncBlock index or if the likeliness of such collisions has increased by a new implementation, but you could use kvb's approach and store only these collisions in a static

1
Map<int, obj list>

.

By on 9/10/2010 3:06 AM ()

It's interesting that System.Object doesn't just implement IComparable with GetHashCode.

What should CompareTo return if two objects yield the same hash code?

Zero?

[link:msdn.microsoft.com]

-Harold

By on 9/10/2010 12:23 PM ()

It's interesting that System.Object doesn't just implement IComparable with GetHashCode.

What should CompareTo return if two objects yield the same hash code?

Zero?

[link:msdn.microsoft.com]

-Harold

That would result in any two objects with the same hash code being treated as equal, which is almost certainly the wrong behavior. For example, this would lead to "" (the empty string) being considered equal to the boxed integer 757602046. It's hard to imagine a scenario where this is expected or desired.

By on 9/10/2010 1:45 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