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

Why does indexing a DispatchedTuple always return a tuple? #12

Closed
ali-ramadhan opened this issue Mar 12, 2021 · 4 comments
Closed

Why does indexing a DispatchedTuple always return a tuple? #12

ali-ramadhan opened this issue Mar 12, 2021 · 4 comments

Comments

@ali-ramadhan
Copy link
Collaborator

I noticed that indexing a DispatchedTuple always returns a tuple:

julia> dispatch(dt, Center())
("c",)

which I thought might introduce lots of [1] boilerplate if I used it since I usually want the contents of the 1-tuple

julia> dispatch(dt, Center())[1]
"c"

but maybe this is because I'm focusing on the case where each object maps to just one value.

Is it always returning a tuple because you want the type of the output to always be <:Tuple? I guess returning [1] in the case of a 1-tuple might introduce type instability?

@charleskawczynski
Copy link
Owner

Is it always returning a tuple because you want the type of the output to always be <:Tuple?

I think so, yes. DispatchedSets return the value:

using DispatchedTuples
struct Foo end;
struct Bar end;
struct Baz end;

tup = (
   Pair(Foo(), 1),
   Pair(Bar(), 3),
)

dtup = DispatchedSet(tup);

dispatch(dtup, Foo()) # returns 1
dispatch(dtup, Bar()) # returns 3
dispatch(dtup, Baz()) # error

dtup = DispatchedSet(tup, 0);

dispatch(dtup, Foo()) # returns 1
dispatch(dtup, Bar()) # returns 3
dispatch(dtup, Baz()) # returns 0

@charleskawczynski
Copy link
Owner

I guess returning [1] in the case of a 1-tuple might introduce type instability?

Yeah, probably

@ali-ramadhan
Copy link
Collaborator Author

Ah thanks for the clarification. Indeed DispatchedSet is probably what I want in this case.

I guess this suggests that you want to be iterating over the result of indexing a DispatchedTuple, e.g.

for v in dt[Face()]
    # do something with v...
end

which will always do the right thing.

@charleskawczynski
Copy link
Owner

I guess this suggests that you want to be iterating over the result of indexing a DispatchedTuple,

Yes, exactly

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

2 participants