You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There might be maps containing null or "" (empty string). For now in such cases it requires 2 method calls to a (potentially big) map to first check for existence of a key, and then fetching the value.
Alternatively one could fetch the value and in case of being Null (or an empty string) one needs to check if the key actually exists.
So I suggest to simply add another overloaded ValueForKey supporting an extra parameter which is filled accordingly.
Rem
bbdoc: Finds a value given a @key.
returns: The value associated with @key.
about: If the map does not contain @key, a #Null object is returned. Also fills @keyExists to retrieve the key existence in the same call.
End Rem
Method ValueForKey:Object( key:Object, keyExists:Int Var )
Local node:TNode=_FindNode( key )
If node<>nil
keyExists = True
Return node._value
Else
keyExists = False
Return Null
EndIf
End Method
A sample use could look like this:
Method GetVariableString:String(key:String, defaultValue:string = "", createDefault:int = True)
Local result:String
Local keyExists:Int
If variables
result = String(variables.ValueForKey(key, keyExists))
EndIf
if not keyExists
result = defaultValue
if createDefault
variables.Insert(key, defaultValue)
EndIf
EndIf
Return result
End Method
Of course one could consider returning a Struct SMapResult instead ...
What would you favor? Or would you do it even differently?
The idea is of course to avoid 2 calls and handle the potential case of "not existing, so let me create it now" (which then happens less likely the more you work with "that map").
The text was updated successfully, but these errors were encountered:
It might be more convenient to swap the "key exists" boolean and the dictionary value: Method ValueForKey:Int(key:Object, value:Object Var)
Then you can use it like this:
Local map:TMap = ...
Local key:Object = ...
...
Local value:Object
If map.ValueForKey(key, value) Then
' do something with value
End If
Btw this aside I would also prefer a ContainsKey:Int(key) + Get(key) (or Get:Int(key, object var)) instead of the more ambiguous sounding Contains() - without IDE help this is "contains the key?" vs "contains the value?"
There might be maps containing
null
or""
(empty string). For now in such cases it requires 2 method calls to a (potentially big) map to first check for existence of a key, and then fetching the value.Alternatively one could fetch the value and in case of being Null (or an empty string) one needs to check if the key actually exists.
So I suggest to simply add another overloaded
ValueForKey
supporting an extra parameter which is filled accordingly.A sample use could look like this:
Of course one could consider returning a
Struct SMapResult
instead ...What would you favor? Or would you do it even differently?
The idea is of course to avoid 2 calls and handle the potential case of "not existing, so let me create it now" (which then happens less likely the more you work with "that map").
The text was updated successfully, but these errors were encountered: