I’d like to create a single function in Verse that handle a map without worrying about the key or value type passed in the parameter..
My goal is to avoid writing multiple functions, such as AddAgentToMap, AddStringToMap, AddIntToMap, etc. Instead, I want a single function that accepts any key type and any value type (agent, string, int, float, logical, or even a class) and simply assigns the key-value pair to the map.
The idea would be something like this:
# Just the function example to be more direct
AddKeyToMap(Map : [K]V, Key : K, Value : V where K : AnyType, V : AnyType):[AnyType]AnyType =
var NewMap := Map
if:
not NewMap[Key]
set NewMap[Key] = Value
return NewMap
# Or within a class:
Map_Manager := class():
var Map : [AnyType]AnyType
AddKeyToMap(Key : AnyType, Value : AnyType):[AnyType]AnyType =
if:
not Map[Key]
set Map[Key] = Value
return Map
What I want to know:
Is there a way in Verse to declare any type parameters so that a function can work with any type of map?
It’s not that I need it. It’s just that I like having functions for things that happen frequently, like assign a map[Key]Value.
Of course, I could do it the common way every time, like this:
var PlayersData : [player]Player_Data = map{}
AssignPlayerData(Player : player):void=
if:
not PlayersData[Player]
set PlayersData[Player] = Player_Data{}
This is more of a curiosity and a possibility within Verse. I do a lot of things that might seem unnecessary when I’m programming. I just think that the more I can simplify my code, the better.
I found the extension method interesting. It might not be useful for all the map types I have, especially those with objects that need some initialization data.