Skip to content
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

getfield with integer index argument #4806

Closed
WestleyArgentum opened this issue Nov 13, 2013 · 6 comments
Closed

getfield with integer index argument #4806

WestleyArgentum opened this issue Nov 13, 2013 · 6 comments
Assignees
Milestone

Comments

@WestleyArgentum
Copy link
Member

When I want to iterate over all the members of a type the first valid syntax that comes to mind is roughly:

members = names(thing)
for name in members
    ...
end

But this might be pretty inefficient (I only looked deep enough to see a call to collect, I'm assuming it's creating a new array each time).

Maybe this has been discussed before, but wouldn't it be nice if I could:

for (name, value) in thing  # <--- treat the type like a dictionary
    ...
end

and count on things in the background to not allocate an array?

@quinnj
Copy link
Member

quinnj commented Nov 14, 2013

Not sure if this is quite what you had in mind, but it seems pretty trivial to implement for your own type. Or are you asking this be a general feature of types in Base?

In  [1]: type Foo
    a
    b
    c
    d
end

In  [2]: importall Base
start(x::Foo) = 1
done(x::Foo, i) = length(names(Foo)) == i-1
next(x::Foo, i) = ((string(names(Foo)[i]),getfield(x,names(Foo)[i])), i+1)
bar = Foo(1,2,3,4)

Out [2]: Foo(1,2,3,4)

In  [3]: for x in bar
    println(x)
end
("a",1)
("b",2)
("c",3)
("d",4)

@JeffBezanson
Copy link
Member

I'm planning to add getfield(x, i) where i is an integer field index. This will allow efficient sequential access to all object fields. It could be wrapped with a custom iterator called something like struct2dict.

@ghost ghost assigned JeffBezanson Nov 14, 2013
@WestleyArgentum
Copy link
Member Author

That's exactly what I want, thanks for the update!
On Nov 13, 2013 9:34 PM, "Jeff Bezanson" notifications@github.com wrote:

I'm planning to add getfield(x, i) where i is an integer field index.
This will allow efficient sequential access to all object fields. It could
be wrapped with a custom iterator called something like struct2dict.


Reply to this email directly or view it on GitHubhttps://github.com//issues/4806#issuecomment-28460832
.

@WestleyArgentum
Copy link
Member Author

Woo!
On Jan 12, 2014 8:46 PM, "Jeff Bezanson" notifications@github.com wrote:

Closed #4806 #4806 via bff951ehttps://github.com/JuliaLang/julia/commit/bff951e1724c706eb74f82faae7f5025c78eb8b8
.


Reply to this email directly or view it on GitHubhttps://github.com//issues/4806
.

tknopp pushed a commit to tknopp/julia that referenced this issue Jan 13, 2014
@mathpup
Copy link
Contributor

mathpup commented Jan 14, 2014

Has this been added to the documentation? This sounds like a pretty significant and useful addition.

@StefanKarpinski
Copy link
Member

Not yet. I think this is still in "banging on the tires" mode, but it should be soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants