On 18 June 2014 19:52, Tim Holy <tim.h...@gmail.com> wrote: > > Pontus, if your function call is sometimes returning a range and sometimes > returning an integer, then you may have a type-stability issue anyway. To make > it type-stable, you may want to have it return 17:17.
I think I formulated myself rather badly, what I meant was a function call for the end of the range as in `for i in 1:size(x, 1)`, which I would more frequently than I would like to admit mess up as `for i in size(x, 1)`. > Regarding integers-as-iterables, I think the issue is that it makes it much > easier to write generic code. For example: > > function twoloops(index1, index2) > for i2 in index2 > for i1 in index1 > println(i1, " ", i2) > end > end > end > > This code works for all of these: > twoloops(1:2, 1:3) > twoloops(5, 1:3) > twoloops(6:8, 4) > > If integers weren't iterables, either one would force the user to call this as > twoloops(1:2, 1:3) > twoloops(5:5, 1:3) > twoloops(6:8, 4:4) > > or define functions of different signatures > twoloops(index1::AbstractVector, index2::AbstractVector) > twoloops(index1::Real, index2::AbstractVector) > twoloops(index1::AbstractVector, index2::Real) Indeed, that is rather neat. While I would favour safety over brevity in this case (maybe even for most cases, there is still a bit of a Pythonista somewhere in there), I can certainly see the other side of the argument here. Thanks to both Tim and Ivar for helping me out with this one. Pontus