Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[map.mod] Single method to retrieve value and existence of passed key #327

Open
GWRon opened this issue Jul 31, 2024 · 2 comments
Open

[map.mod] Single method to retrieve value and existence of passed key #327

GWRon opened this issue Jul 31, 2024 · 2 comments

Comments

@GWRon
Copy link
Contributor

GWRon commented Jul 31, 2024

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").

@HurryStarfish
Copy link
Member

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

@GWRon
Copy link
Contributor Author

GWRon commented Aug 2, 2024

That version looks OK to me too.

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?"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants