v icon indicating copy to clipboard operation
v copied to clipboard

for _ in [1..10] doesn't compile

Open pfefferminz132 opened this issue 7 years ago • 9 comments

per accident, range was not working, i used for _ in [1..10] { instead of for _ in 1..10 {

i was said to create an issue because i had found a bug so i did now.

pfefferminz132 avatar Apr 30 '19 13:04 pfefferminz132

There is no integer range syntax AFAIK. Instead, use C for syntax. We could have a count function returning an iterator though (if for supported iterators):

	for i in count(1, 10) {

I'll post more on iterators soon.

ntrel avatar May 02 '19 08:05 ntrel

plese look here: https://vlang.io/compare and then to the V example

pfefferminz132 avatar May 02 '19 11:05 pfefferminz132

V will have min..max soon. I actually thought it was live, but it's temporarily on a branch.

medvednikov avatar May 02 '19 11:05 medvednikov

min .. (min+length-1) -- yuck!

I don't care about 0-based or 1-based indexing. But I hate not knowing where a range ends. The simple solution, overlooked in most languages, is to use the length for the second argument.

With .. that would throw people off. But maybe a different syntax could be used. Ideas:

  • for i in min @ length
  • for i in min -> length
  • for i in min : length
  • for i in min ..@ length

cdunn2001 avatar May 03 '19 17:05 cdunn2001

V will have min..max soon

I think slice syntax for arrays and strings should be the same then, so arr[min .. max], and the max index value is excluded. Built-in range syntax would be useful in match cases, but there they need to include max so three dots could be used:

c := get_char()
n := match c {
  `0` ... `9` -> c - '0'
  `a` ... `f` -> c - 'a' + 10
  else {println('$(c.str()) is not a hex digit'); continue}
}

ntrel avatar May 15 '19 10:05 ntrel

min .. (min+length-1)

That would be min .. length - 1 to exclude the last valid index.

ntrel avatar May 15 '19 10:05 ntrel

Fixed.

[1..10] == [[1,2,3,4,5,6,7,8,9]] by the way.

medvednikov avatar May 15 '19 10:05 medvednikov

I think slice syntax for arrays and strings should be the same then

Agreed, I'll have to pick one.

medvednikov avatar May 15 '19 10:05 medvednikov

Actually, if 1..4 means an array literal, then slice syntax should be different. arr[1..4] would look like it meant arr[[1,2,3]]. So maybe arr[start:end] should be the syntax, like Go (where start and end are both optional).

ntrel avatar May 16 '19 07:05 ntrel