Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Subo.

Asked: September 02, 2016 - 6:32 pm UTC

Last updated: September 05, 2016 - 3:33 am UTC

Version: 11.2

Viewed 1000+ times

You Asked

What is a map member function in a type?Can anyone give me simple example to understand the concept?Thanks..

and Connor said...

If someone sends me two employee records, one for primary key=123 and the second one for primary key = 123, then I know that these are referring to the *same* employee. But what about objects...they dont *have* a primary a key as such.

Lets say I create a complex object type for a tree. It might be:

(
height,
width,
bark colour,
leaf colour,
has_flowers,
deciduous,
latitude,
longtitude
)

So if someone sends me two tree objects, how do I know if that is TWO tree definitions or the same tree sent to me twice. I need something to let me decide if two tree are *equal*.

That is what a map member function does. In the example above, I might decide on something like:

map member function tree_id return number is
  return latitude*1000+longtitude;


on the basis that if "two" trees are located in the exact same position on the earth, then we will assume them to be the same tree.

Hope this helps.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Map functions (can) do more than just equality comparisons

mathguy, November 24, 2022 - 10:18 pm UTC

This answer gives the impression that map functions are only used to define "equality" of objects, when in fact they are also used for inequality comparisons.

For example, if an object type "PO" (purchasing order) has "quantity" and "unit price" among its properties, the map function may return "quantity" times "unit price" (the total value of the PO), and use that for inequality comparisons, not just for equality. The answer doesn't need to give a full example like this, but it shouldn't give the impression that it's all about defining "equality" of objects.

The comparison to primary keys is particularly misleading - it is perfectly OK for distinct objects to map as equal: see the PO example, where two distinct PO's (distinct order numbers, as PK) can have the same total value, and therefore map as equal.