I want to use the Date library date 4.0.0
This library represents months as Time.Month values instead of integers.
I need to build a Dict that uses months as key.
It doesn’t seem to be possible with Time.Month (although the compiler error doesn’t say explicitly that the problem is that Month is not comparable, I guess this is the problem).
I’d like to know if Time.Month is comparable. If not how to convert Time.Month to Int ? The Time library says it’s possible but it doesn’t say how.
User defined Types are always not comparable, so you can not use them as key in a Dict.
You can use the function monthNumber
Thanks. I was looking for it in the Time library since Month is defined there.
You can also use AssocList which is like a
Dict but allows custom types as keys – which helps a lot to improve code IMO.
(Just don’t use it in a super performance sensitive part of your app (if you have one) and expect it to be as fast as standard
Dict. I haven’t encountered a situation where it’s too slow yet, but I’ve never used for something like a game either.)
Also don’t use AssocList if you expect normal equality to work.
If you add, say,
"foo" at key
"bar" at key
2 into a normal
Dict in both possible orders, the two results will return
True when compared with
==. This is sensible, because
Dict is supposed to be an unordered container, so insertion order should not matter. (But it is actually a special case in the compiler which will turn both
Dicts into sorted lists and compare those instead.) If you do the same for an
AssocList the two results will return
I see, thanks! Didn’t know about that.
This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.