-
Notifications
You must be signed in to change notification settings - Fork 103
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
alternative to 'apply' method #135
Comments
It seems good for performance, but how can we translate the star operator into JavaScript? I suppose adding |
Performance is another problem. I think that's not what we have to disscuss here (because this proposal is only a draft, and the detail of its implementation is something the developers should decide when we adopt the issue).
Maybe some optimization techniques can be used and if neccesary it is possible to convert this expression into much more efficient code such as combination of for-statements or other engine-specific ways. Regarding overloading:
The reason of No.3 is that using overloaded 'push' against an array of an array may cause serious problems. Some dangerous codes which fails to be compile by far will pass the compiler check, and the caused bug will be so hard to find. That's not a very good scenario. |
My understanding is that the issue is actually is a request for a destructive Array#concat. In other words, IMO it should better not be named as push or such. How about adding a function with a different name that gets translated into a call to Array.prototype.push.apply?
We could also introduce Array#spliceElements and Array#unshiftElements as well. |
That's right about Array#push. But I do want the alternative version of Array#splice, too.
If you add some methods to Array class such as pushElements or spliceElements, it would be better to add a new utility class called 'ArrayUtilities'. This way would also be good for the compatibility to JS.
But this cannot be generalized to other native classes, so introducing star operator is still worth considering, I think. (Of course star operator is just an example.) |
I agree with @wasabiz. Array-expand operator looks better because there are a lot of native methods which requires vararg, not only in built-in.jsx but in web.jsx. |
@wasabiz @gfx How does it sound? |
@kazuho I don't like the idea. I suppose the way of adding another version of variable-length methods automatically you suggested is not easy to understand. It is not good that something you don't explicitly write by the code is gonna happen. If you don't want to extend the language further more, why don't you introduce the new utility class? It won't solve the problem essentially but still is enough good for practical use. |
@wasabiz
I do not mind whether the function is defined as a static function of a utility class or as a member function of the Array class (although the former reminds me of the infamous System.arraycopy of Java). Regarding your proposal, what is the exact behavior of the * operator. Is it to be allowed for positions where the formal arguments are "..." (i.e. variable length)? |
@kazuho Well, I think your suggestion (extension of the native attibute) is aiming at something that can be extended for other functionalities as well as for alternatives to Unlike Java, JSX has to follow the traditional convention made by JavaScript so far. Breaking the old API from JS is not a good idea, I think. Regarding * operator: |
I agree that the merit of introducing * operator is that it would be easy to understand for developers familiar with JavaScript.
Would it be possible to support creating a concatenated array in the form: [ *a, *b ] which gets compiled into [].concat(a, b)? |
PS. IMO if we are to introduce "*" operator, then we should support [ *a, *b ] as well, since the meaning of the operator would be to splat an array into a list of homogeneously typed sequence (which is both the case of array initializer and variable length arguments of function calls). Or else, I still think it is not a good idea to introduce a syntax-level extension for the purpose (since the scope is too narrow; my belief is that it is generally a bad thing to extend the language for tiny reasons). And in this case, introducing pushArray and alike (as discussed above), or supporting expressions like: a.push.apply(b) (like JavaScript but implicitly passes this object) could be the candidates. |
In JavaScript, we can push more than 2 values into an array by writing only one line using `Array.prototype.push.apply'.
However there is no equivalent feature in JSX and we have to rewrite it into much more redundant code by using for statement and Array#push.
This is also the case with other methods in Array such as splice or unshift.
Do you have any plan to introduce new language feature to solve this problem?
For example, Python's star '*' operator is enough reasonable to be worth considering.
The text was updated successfully, but these errors were encountered: