Skip to content

Why is length defined on numbers? #11769

Closed
@samuela

Description

@samuela

I discovered from a typo today that the length method is defined on numbers:

julia> length(5)
1

julia> length(pi)
1

It's defined here: https://github.com/JuliaLang/julia/blob/master/base/number.jl#L12. Why is length defined on numbers? This seems counterintuitive. Numbers aren't iterable in any way after all.

Activity

joehuchette

joehuchette commented on Jun 19, 2015

@joehuchette
Member

They are in Julia:

julia> for a in 1
       @show a
       end
a = 1
samuela

samuela commented on Jun 19, 2015

@samuela
ContributorAuthor

AHH, BUT WHYYY?

nalimilan

nalimilan commented on Jun 19, 2015

@nalimilan
Member

It's considered practical by some people. The question is rather whether this has drawbacks or not. Else there's no reason to remove that.

FWIW, please use the mailing list for questions in the future, and keep GitHub for bug reports.

Keno

Keno commented on Jun 19, 2015

@Keno
Member

FWIW, I consider this a mistake, it has bitten me way too many times.

carnaval

carnaval commented on Jun 19, 2015

@carnaval
Contributor

I also find it a bad idea, but github may not be the place to revive the debate

samuela

samuela commented on Jun 19, 2015

@samuela
ContributorAuthor

This just reeks of MATLAB to me.

jakebolewski

jakebolewski commented on Jun 19, 2015

@jakebolewski
Member

@samuela the supported iteration / indexing interface is to basically treat scalars as zero dimensional arrays.

Keno

Keno commented on Jun 19, 2015

@Keno
Member

It is way too easy to miss a colon in a for loop and only do one iteration - I remember vividly how I spent a week debugging why my locality sensitive hash wasn't local - I had for i = x instead of for i = 1:x for the number of rounds boom.

nalimilan

nalimilan commented on Jun 19, 2015

@nalimilan
Member

Yeah, I'm not a great fan of this either. :-/

JeffBezanson

JeffBezanson commented on Jun 19, 2015

@JeffBezanson
SponsorMember

Yes I really think we should experiment with removing iteration of numbers. I think the array indexing redesign might make this possible.

johnmyleswhite

johnmyleswhite commented on Jun 19, 2015

@johnmyleswhite
Member

It is way too easy to miss a colon in a for loop and only do one iteration

This is the bug I most often hit these days.

mbauman

mbauman commented on Jun 19, 2015

@mbauman
SponsorMember

Here was my last 15-minute attempt at making numbers non-iterable: #10331 (comment)

carlobaldassi

carlobaldassi commented on Jun 19, 2015

@carlobaldassi
Member

Just to balance the opinions here, I'll mention I use this feature a lot and find it quite handy.

joehuchette

joehuchette commented on Jun 19, 2015

@joehuchette
Member

@carlobaldassi what does a typical use look like?

27 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Why is `length` defined on numbers? · Issue #11769 · JuliaLang/julia