Hi,

I see the only useful (natural, widely used) extension for hashes. It is HB_HGET() extension:
  HB_HGET( hHash, xKey [, xDefaultValue ] )

Nice. But do you mean that if xDefaultValue is passed, we
should change the RTE behavior of this function? It's okay-ish
with me, but it's a bit strange. For me it would be the
most natural if this function would simply return NIL in
case the key isn't found, or xDefaultValue, if passed. But
changing this function may cause compatibility concerns for
some users (I can't judge).

What was the reasoning behind adding an RTE for non-existing
key lookup, does anyone have any memories on that?

I do not remember any discussion about RTE on non-existing key lookup, but I see this natural. HB_HGET( hHash, xKey ) is a king of synonym for hHash[ xKey ]. So, if Harbour generates runtime error for:
  aArray := {}
  ? aArray[ 1 ]
it should generate RTE for:
  hHash := {=>}
  ? hHash[ 1 ]  // or HB_HGET( hHash, 1 )

I'd like to catch hash access error, but not return NIL and mask it. If the user do not want generate RTE if key does non-exists, there are two possible solutions (both of them are from my real life app development):

1) if it's OK, that hash is extended (missing key added) and default value is always the same, developer can use auto-add feature. The missing key will be added to hash using hash's default value. 2) if hash should not be extended or default value is different for different keys, it had to write:
  IF HB_HHASKEY( hHash, xKey )
     xResult := hHash[ xKey ]
  ELSE
     xResult := xDefaultValue
  ENDIF
or
  SomeFunction( IF(HB_HHASKEY(hHash,Key), hHash[xKey], xDefaultValue) )

I have a dozen expressions like this in my code. About 40% of HB_HHASKEY() calls in my code are used exactly for this purpose, i.e. to test if key exists and return default value otherwise. hHash[ xKey ] expression can not be extended to indicate the default return value, but HB_HGET() function can easy be extended and I see this extension useful.

Yes, I would be extension of the existing function. I do not see a big problems about backward compatibility here. Code using two parameters version HB_HGET( hHash, xKey ) will be compatible. I don't think many people currently are using code:

HB_HGET(hHash, xKey, "let's pass some extra params", "to make my own code", "incompatible with future extensions", .T., {|| __QUIT()}, 123, DATE())


Best regards,
Mindaugas
_______________________________________________
Harbour mailing list
Harbour@harbour-project.org
http://lists.harbour-project.org/mailman/listinfo/harbour

Reply via email to