Object.HasValue/Contains/KeyOf/FindValue
Posted: 13 Apr 2017, 17:15
This mightn't be strictly for v2, but it may be a replacement for if var in MatchList.
It was also suggested and discussed on the old forum, and recently in the v2-thoughts topic.
When it finds the value, it also finds the key. Returning the key would be more useful, but then {0: "A"}.HasValue("A") and {"": "A"}.HasValue("A") would be false. Even if it's called something like FindValue or KeyOfValue, it doesn't seem correct to "reserve" the empty string to mean "not found".
Maybe Object.HasValue(Value [, CaseOrTypeSensitive, ByRef Key])...
I like the name HasValue for symmetry with HasKey, but it I'm not sure it would be appropriate if the method has more functionality similar to InStr (i.e. StartingKey and Occurrence). On the other hand, those parts are probably not needed often and can already be done with a for-loop.
It was also suggested and discussed on the old forum, and recently in the v2-thoughts topic.
IN/CONTAINS
Has anybody considered a normal function for this? E.g. StrContains, ArrayContains, ObjContains.
If Arr.HasValue(Val) is implemented, should it always return a boolean value?I wrote:Arr.HasValue(Val) is not much different to Val in Arr, maybe even clearer. That would eliminate the (unasked?) question of how or whether Val in Obj should be handled when Obj isn't an array (even though there's currently no clear distinction between arrays and other objects). In JavaScript, "A" in {A: 1} is true and "A" in ["A"] is false. Using HasKey() or HasValue() makes it clear, and also provides a way to specify case sensitivity that doesn't involve changing a thread-global setting.
When it finds the value, it also finds the key. Returning the key would be more useful, but then {0: "A"}.HasValue("A") and {"": "A"}.HasValue("A") would be false. Even if it's called something like FindValue or KeyOfValue, it doesn't seem correct to "reserve" the empty string to mean "not found".
Maybe Object.HasValue(Value [, CaseOrTypeSensitive, ByRef Key])...
I like the name HasValue for symmetry with HasKey, but it I'm not sure it would be appropriate if the method has more functionality similar to InStr (i.e. StartingKey and Occurrence). On the other hand, those parts are probably not needed often and can already be done with a for-loop.