cannot create map using abstract class?

Monkey Forums/Monkey Programming/cannot create map using abstract class?

skid(Posted 2011) [#1]
not sure if I'm doing something wrong but the following generates an error in recent versions of Monkey:


Import mojo.app
Import mojo.audio

Function Main()

	Local soundmap:=New Map<Sound,IntObject>

End




marksibly(Posted 2011) [#2]
Hi,

You need to extend Map and implement the Compare() method yourself - see IntMap etc.

This is largely because Object doesn't have a built-in Compare method, so by default maps don't know how to compare keys.

And objects don't have a built-in compare method because not all targets provide a way to compare objects.

In bmx, the objects' 32 bit pointer were compared. However, this isn't so simple with some targets, where GC may actually physically move objects around in memory - and that's just on targets that even let you 'see' the pointer.

One solution would be to add an _id field to Object that's just set to an increasing global value each time a new object is created - but I'm a little nervous about imposing that on ALL objects.

What does everyone think? The overhead would be 4 bytes per object for the _id field (or perhaps 8), plus the extra 'Self._id=++_globalId' style code per New.


muddy_shoes(Posted 2011) [#3]
not all targets provide a way to compare objects.


Just curious. Which targets?


marksibly(Posted 2011) [#4]
Hi,

> Just curious. Which targets?

Pretty much all!

C# and Java provide an int 'HashCode' style method, but it's not guaranteed to be unique per object, so you can't just use it with an int compare.

Such a HashCode can be used to add objects to a plain hashtable, but doesn't work well with a Map or any general 'sorting'.

The other targets don't even provide this.


muddy_shoes(Posted 2011) [#5]
Are you talking about reference equality, value equality or some concept of ordered comparator?


marksibly(Posted 2011) [#6]
Hi,

Just reference comparisons for being to arbitrarily insert objects into a map as per the original question.


muddy_shoes(Posted 2011) [#7]
So you mean that they don't give you an integer or similar that is equivalent to an address value? Okay, well true enough, but you don't need that to implement a Map/Dictionary. I was confused by your statement that they don't allow you to compare objects, because they all do, AFAIK.

I can't see that a loss of a few bytes per object and an increment and assignment would create much of a blip in overhead. You could also use the existence of Interfaces to change the implementation of Map to remove the need to subclass.


ziggy(Posted 2011) [#8]
Maybe we could add a IComparable interface to the language, so any object subject to be placed inside a map just have to implement it?


Samah(Posted 2011) [#9]
Maybe we could add a IComparable interface to the language, so any object subject to be placed inside a map just have to implement it?

Diddy has one that it uses for sorting within ArrayList. If Diddy had its own implementation of Map it could use that (I'm still thinking about it...)

Interface Comparable
	Method Compare:Int(o:Object)
	Method CompareBool:Bool(o:Object)
End

CompareBool is like equals() in Java, I just didn't want to call it Equals.