By the way, yes I do actually use the random library. I was just showing the idea behind it. Another solution could be just to start at 1 and increment....
Ok, I'll try to explain the goal more clearly.
In my Civ-like game, I have loads of things/entities.
A Game has many Civilizations and a GameMap. A Civilization has many Cities. A City has many buildings and many units. A GameMap is a 2D grid of tiles.
For numerous reasons, it would be convenient to have most of these things have "unique IDs" such that I may distinguish them. For example, telling one unit from another.
Another big example is how to deal with tiles. The problem of tiles has been discussed in a previous thread :
https://groups.google.com/forum/#!searchin/elm-discuss/tiles/elm-discuss/VhT9CFeWBLc/YpZcdy5NMa8JBasically, cities can "own" tiles. They can grow their borders to include more tiles and such gain wealth and other things from having these tiles. But, the tiles are all part of a "Map" (the 2D grid of tiles). Cities don't own the whole Map.
One solution would be to have both the Map (as a property of the general "Game" object) and have a list of tiles as a property of the cities. The problem is that any "modification" made to a tile by a City must be duplicated in the Map. This is a duplication of memory and completely error prone. So, the idea is have the cities have a property which is a list of "tileIDs". This would imply that any "modification" or "update" must be made directly on the Map and the "tileID" can be used for querying (this tileID can be as simple the index of the tile for example). So, in essence, I can do the whole thing without Unique IDs, but I did wonder how would I do it with? Could I make every "thing" or "entity" in my game have a "uniqueID"? How would that be acheived?
This is what has led me to the above question and exploration. Unique IDs are used in loads of settings (databases being a big one). So, I was thinking, how could it be done in Elm?
Now, that said, a
seperate question came to mind:
It would be cool if I could get the ID simply by doing "
thing.id".
Like, somehow, if I had a type:
type alias Object = {
id : ID,
a : A,
b : B
}
and I had another type:
type alias NestedObject = {
object : Object,
id : ID,
c : C
}
I could generate their "IDs" easily (syntactically easily I mean)
I have this habit of creating default "objects" whenever I create a "record type". It makes my APIs easier to use. The problem is that I can't just do:
object1 : Object
object1 = {
id = generateID,
...
}
object2 : Object
object2 = {
id = generateID,
...
}
and expect
object1.id to be different from
object2.id. Elm is all immutable and that's not possible.
So, I tried looking for ways.
I realized that the problem of generating Random numbers is a very similar problem. So I took a cue from it and used Extensible types to get what I want.
Then I went snooping in the Haskell world to see how things are done. They use the IO type, which is kinda like the upcoming Promise type. So, I thought, "hey, that could work".
And then I thought of asking you guys if you have already had this issue to see how you have dealt with it or if you have any ideas on how to do it. Perhaps there's a better solution that I haven't thought of.
I hope to have clarified things. I am sorry, I have probably unconsciously mixed in different questions into one but I was thinking about the whole thing as a unit. Like, the overarching question seemed to me: How best to deal with
uniquely identified objects generally? How to deal with objects with an ID as a property? How to deal with objects with an ID as a property and properties who are themselves objects with IDs and so on... How to all this while staying pure an immutable and remaining usable?
If things are still unclear, I'd be glad to continue to clarify things. Again, I'm really sorry with the confusion. While writing my question originally, I had like a million thoughts on the topic going through my head at once and didn't manage to crystallize them appropriately.