I'm hoping to achieve a certain style of enum syntax/functionality, however I'm not sure how to achieve it. Currently I have the following:
internal enum Timeframe: Equatable {
// MARK: - Cases
case hour(count: Int)
case day(count: Int)
case week(count: Int)
case month(count: Int)
case year(count: Int)
case all
case exact(date: Date)
case unspecified
}
I would like to eliminate the count associated value, unless it's required. For example:
let oneDayTimeframe: Timeframe = .day
let twoDayTimeframe: Timeframe = .day.multiplied(by: 2)
Is this possible? Even if there isn't a way to achieve exactly what I'm looking for, I'd appreciate suggestions for potential improvements. In most cases, I end up using (count: 1), which seems a bit verbose. If default values were available with associated values, I would have used
case hour(count: Int = 1)
Any suggestions?
Iterating a bit on the answer you provided:
Would disallow misuse e.g. disallow:
And allow multiplication using the * operator, e.g.
And .unspecified: