
Where DateRangeIterator is the iterator we have yet to implement.ĭateRangeIterator inherits from Iterator and ClosedRange.


Return DateRangeIterator(start,endInclusive) Override val endInclusive:Date):Iterable, We also have to implement iterator which returns a suitable iterator for the range: class DateRange(override val start: Date, We need to add the override modifier to the constructor because the properties created by the primary constructor are hiding properties inherited from ClosedRange. Override val endInclusive:Date):Iterable,ClosedRange To create a date range class we need to implement a class, DateRange, that implements the Iterable interface, and the ClosedRange interface which has the endInclusive property: class DateRange(override val start: Date, To use this in a Kotlin project you need to add: import java.util.* In Java there are far too many date classes in use at the moment, so for simplicity we will use the original Date class, despite most of its methods being deprecated rather than LocalDate, which is its successor. As an example let's create a range that works for dates. It isn't difficult to create your own ranges and progressions. Roughly speaking, Range allows you to write things like: for(i in 1.10)Īnd a Progression is a range for which you can specify a step size, for example: for(i in 1.10 step 2)Īs already explained in connection with the for loop, ranges and progressions integrate into the for using the rangeTo function as an operator. Kotlin provides two data types that make it easier to write for loops – the Range and the Progression.
