-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a Range<T: Integer>
type and allow in for-loop
#2482
Comments
Range<T: Number>
type, which can be used for e.g. iterationRange<T: Number>
type and allow in for-loop
Range<T: Number>
type and allow in for-loopRange<T: Integer>
type and allow in for-loop
Before starting an implementation, we should discuss the design and publish a proper design document, potentially a FLIP. |
Maybe it is good time to expose built in interfaces (comparable, enumerable etc) to Cadence. Also ‘step’ here can be useful too. ( reverse ranges especially) |
Ack, will write up a FLIP. I'm sharing my proposal below but happy to write a FLIP if we want to exclusively discuss on the FLIP itself. Here is what Kotlin does: Forward range - for ( num in 1.rangeTo(10)) {
println(num)
}
for ( num in 1..10) {
println(num)
} for ( num in 1.rangeTo(10) step 2) {
println(num)
}
for ( num in 1..10 step 2) {
println(num)
} Reverse range - for ( num in 10 downTo 1) {
println(num)
} for ( num in 10 downTo 1 step 2) {
println(num)
} Here is what I would like to propose for Cadence:
I believe using struct Range<T: Integer> {
let start: T
let endInclusive: T
let step: T // Only positive values allowed. Default = 1.
} Examples: A) With default step and forward direction for x in 1..6 {
// do something
} for x in 1 upTo 6 {
// do something
} Will iterate for x = 1, 2, 3, 4, 5, 6. B) With custom step and forward direction for x in 1..6 step 2 {
// do something
} for x in 1 upTo 6 step 2 {
// do something
} Will iterate for x = 1, 3, 5. C) With default step and reverse direction for x in 6 downTo 1 {
// do something
} Will iterate for x = 6, 5, 4, 3, 2, 1. D) With custom step and reverse direction for x in 6 downTo 1 step 2 {
// do something
} Will iterate for x = 6, 4, 2. |
It might be a good idea to include the concept of a step, but for the concept of a range, that seems a bit odd. For example, in Kotlin, Swift, and Rust, ranges do not have a step. Instead, some languages have a separate concept for progression, e.g. Kotlin has Maybe we can start with a simple |
Are we planning ranges to be Values? If so we also need to discuss about the API they will have. |
@bluesign Yes, see the description |
Kotlin ranges only have two member functions:
They inherit a lot of functions from Iterable such as |
I think we need to expose somehow; |
@turbolent Actually val r = 1..10 // this creates a IntRange
val s = 1..10 step 10 // this creates a IntProgression
println(r.step) // prints 1 May be we can also do the same i.e |
Add a new built-in type
Range
:Add support for iterating over
Range
in for-in statementsTasks
smallIntegerValueCache
added during InclusiveRange implementation #2871The text was updated successfully, but these errors were encountered: