为什么 JsArrayString 不实现可迭代?
这里有一个简单的问题——我刚刚第一次使用 JsArrayString,并且很惊讶这不起作用:
JsArrayString object = ...;
for (String s : object)
所以我只是编写了一个 C 风格的 for 循环:
JsArrayString object = ...;
for (int i=0; i<object.length(); i++) {
s = object.get(i)
...
没什么大不了的,但对于GWT 团队让 JSArrayString 实现可迭代,所以我想检查并确保我没有遗漏一些东西......
Quick question here -- I just used JsArrayString for the first time, and was surprised that this didn't work:
JsArrayString object = ...;
for (String s : object)
So I just wrote a C-style for loop:
JsArrayString object = ...;
for (int i=0; i<object.length(); i++) {
s = object.get(i)
...
Not a big deal, but seems like it would have been simple for the GWT team to have JSArrayString implement iterable, so I wanted to check and make sure I wasn't missing something...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑这是代码膨胀和代码大小的问题。如果他们让
JsArray
实现Iterable
,它可能会为其他并不总是有用的添加打开大门。JsArray
的目的是绝对尽可能简单和准系统。此外,如果您想要这种行为,您可以编写自己的 JsIterable 类来执行此操作,正如您所说,实现起来应该非常简单。
轻量级集合设计文档解决了有关使用 JRE 的一些问题集合和相关概念,并讨论可以不支持哪些功能以确保绝对最小的代码大小,包括:
I suspect it's a matter of code bloat and for that matter code size. If they make
JsArray
implementIterable
, it might open the door for other additions that wouldn't always be useful.JsArray
s are meant to be absolutely as simple and barebones as possible.Additionally, you could write your own
JsIterable
class that does this if you want this behavior, as you said it should be pretty trivial to implement.The Lightweight Collections design doc addresses some of the issues around using JRE collections and related concepts and discusses which features could be left unsupported to ensure absolute minimum code size, including:
你必须用老派的方式来做:)。查看 其来源。
You have to do it the old-school way :). Check out the source of it.