in reply to Re^11: Scalar context of slice ("list")
in thread Scalar context of slice
I thik we can agree, this (1,2,3) is a list;
By many definitions, it is. It is a list literal. It becomes a list operator when compiled. A list is created on the stack when executed. However, it doesn't necessarily return a list.
Now you could conclude either of:
- If a return statement with a list argument, is found, (or finds itself), in a scalar context, that it transforms the list to a scalar and returns that scalar.
- Or: The return statement returns the list, and when it (the list), finds itself in a scalar context, it transforms itself to a scalar, prior to the assignment.
- Or: The when assignment operator is given a list to assign to a scalar, it transforms the list to a scalar.
One could conclude one of those items, but they'd be wrong by doing so. All three conclusions are predicated on a list being passed to return, but that's not the case. A function's return statement never sees a list when the function is called in scalar context.
In this case, return is passed the result of a list operator. When executed in scalar context, a list operator returns a scalar.
nor does the list cease to be a list.
Saying the list ceases to be a list is interestingly correct since the list reduces itself to a scalar.
And this does not apply to map & grep in scalar contexts [...] because those functions do not attempt to return a list when they find themselves in a scalar context.
Neither does a list. We've already seen the code.
They instead choose to return something, a scalar, that is considered more useful.
So does a list.
So, I see no clearer, simpler or conceptually more accurate way of describing that than: "the results of a list in a scalar context is the last element of that list".
It's no clearer than the original disputed statement ("there is no such thing as a list in scalar context") for reasons that should be obvious from this thread (different understandings of what consists a list).
But as clear, simple and accurate as it might be, it's totally useless. The disputed statement is used to describe how operators and functions behave in scalar context. It can't be replaced with a statement that's specific to one operator.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^13: Scalar context of slice ("list")
by BrowserUk (Patriarch) on Oct 20, 2008 at 04:57 UTC | |
by ikegami (Patriarch) on Oct 20, 2008 at 06:47 UTC | |
by ikegami (Patriarch) on Oct 20, 2008 at 06:49 UTC |