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

Assignment silently truncating tuples #837

Closed
dmbates opened this issue May 14, 2012 · 13 comments
Closed

Assignment silently truncating tuples #837

dmbates opened this issue May 14, 2012 · 13 comments
Labels
needs decision A decision on this change is needed

Comments

@dmbates
Copy link
Member

dmbates commented May 14, 2012

This came up in the thread on qr/qrp on julia-dev. At present the methods for the qr generic return a 3-tuple, (Q, R, p) and it is not an error to assign the result as

Q, R = qr(A)

Matlab users expect this to have different semantics from

Q, R, p = qr(A)

which, I believe, is very difficult if not impossible in Julia. Should an attempt to truncate a returned tuple like this be flagged as an error?

@JeffBezanson
Copy link
Member

Matlab also allows ignoring unused return values, so this could cause problems elsewhere.

@pao
Copy link
Member

pao commented May 14, 2012

Sometimes you don't really need the extra values, though. MATLAB provides "~" to make this explicit; we could use the standard "_" from many functional languages (and I believe supported by julia's anonymous functions?)

@StefanKarpinski
Copy link
Member

_ is just a normal identifier so far. I'm a little reluctant to make it special (I used to be itching to make it special left and right but along the way got "cured" — like a ham).

@pao
Copy link
Member

pao commented May 14, 2012

Having a name that doesn't create a binding means you can explicitly throw stuff away and not create a binding you could accidentally use down the road. I'm not sure if that name should be _, but it is a popular choice (Prologs, Erlang, Haskell, Scala, etc.)

@StefanKarpinski
Copy link
Member

yeah, I could be convinced to do this.

@JeffBezanson
Copy link
Member

Although overloading on nargout does give you some nice flexibility, our behavior here is one advantage of NOT having it. You know the left-hand side doesn't affect the call, so you don't need syntax for asking for values which are ignored. And the error checking part of it is just more code to generate --- and to be able to optimize away --- just because you did not access some bit of data. That feels strange to me. "Error: I took the trouble to make this tuple for you; why didn't you eat the whole thing?"

@pao
Copy link
Member

pao commented May 15, 2012

You do need syntax for asking for ignored values if you want to throw away the first element of the tuple rather than the last one.

@JeffBezanson
Copy link
Member

Ok, so put an underscore in there. Nobody's stopping you. I don't get what the big deal is.

@pao
Copy link
Member

pao commented May 15, 2012

There's nothing preventing you--or without namespaces, anyone else--from using the underscore later on with the potential of accidentally introducing a value you didn't want, instead of being an error. As you state it's just a store that could be optimized away anyways. But I'm not hurt either way. (I'm sorry I used the phrase "need syntax", which may have set this off...)

@StefanKarpinski
Copy link
Member

There's one case where narg-style overloading would be a big benefit: the size function. The reason that's of interest is because of the whole vector/matrix thing. If you call size on a vector that's being used as a matrix and expect a pair of return values, it will blow up. If size could return the expected number of values, padding with ones, then we would get greater polymorphism.

One argument for having assignment to _ discard values is that in the case where a function call is inlined, the computation of the value that's discarded could be avoided entirely. Not sure how realistic that scenario is.

@JeffBezanson
Copy link
Member

Where we can lower code enough that LLVM can prove a computation unnecessary, it will already do that. To do more than that we need to model function purity.

@JeffBezanson
Copy link
Member

We discussed this and didn't really get anywhere. Both matlab and common lisp multiple-value-bind allow ignoring trailing values (lisp even allows more variables on the left than values!), so I didn't expect this to be a controversial feature.

@StefanKarpinski
Copy link
Member

I say just leave it as-is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs decision A decision on this change is needed
Projects
None yet
Development

No branches or pull requests

4 participants