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

Better syntax for Java array #70

Closed
slavchev opened this issue May 20, 2015 · 11 comments
Closed

Better syntax for Java array #70

slavchev opened this issue May 20, 2015 · 11 comments
Assignees
Milestone

Comments

@slavchev
Copy link

NativeScript for Android does not provide syntax to create Java array from JavaScript. The current way is quite verbose and not intuitive. For example

var arr = java.lang.reflect.Array.newInstance(java.lang.Byte.class.getField("TYPE").get(null), 10);
// arr = new byte[10];

There are a few suggestions.

  1. var arr = array([ "element1", "emelemt2", ...]); This will work for only scenarios where we can deduce the element type.
  2. var arr = array("byte", 10) The first parameter is type name, the second parameter is length.
  3. extends the built-in Array with fromJavaType or whatever. For example var arr = Array.fromJavaType("byte", 10);
  4. same as 3) but provide custom object. For example var arr = JavaArray.create("byte", 10);
  5. same as 4) but add type specializations as well. For example: var arr = JavaArray.createByteArray(10);
  6. reuse existing "cast" functions for primitive types. For example var arr = new byte(10);. This scenario does not work for reference types, so should be combined with another one.

Any other suggestions are welcome.

Note: suggested syntax that fits TypeScript well have advantage.

@slavchev slavchev self-assigned this May 20, 2015
@slavchev slavchev added this to the 1.1.0 milestone May 20, 2015
@vakrilov
Copy link

Suggestion 5) looks most explicit to me for primitive types. For reference types we would have to have a method that accepts type parameter.

@hshristov
Copy link

+1 for point 4 - var arr = JavaArray.create("byte", 10);
Where byte is the full name of the type. In my opinion most of the time we will use it with primitive types so it will be easier if byte, int are allowed as type specifiers.

@NathanaelA

This comment was marked as abuse.

@slavchev slavchev modified the milestones: 1.2.0, 1.1.0 Jun 11, 2015
@blagoev blagoev modified the milestones: 1.3.0, 1.2.0 Jul 22, 2015
@atanasovg atanasovg modified the milestones: 1.5.0 (Under Review), 1.3.0 Sep 22, 2015
@blagoev
Copy link
Contributor

blagoev commented Oct 16, 2015

I suggest this syntax to be only for arrays of reference types. Primitive arrays should be covered by the typed arrays support. #65

@blagoev
Copy link
Contributor

blagoev commented Oct 16, 2015

suggest using Array.of syntax similar to this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of

Array.of(android.view.View, 20)

we can monkey patch this as this:
if (!Array.of) {
Array.of = function() {
return Array.prototype.slice.call(arguments);
};
}

so it will be
Array.of = function(type, number) {
return java.lang.reflect.Array.newInstance(type.class, number);
}

actually I think I just implemented it in this comment. That's called "in comment driven development" :)

@blagoev
Copy link
Contributor

blagoev commented Oct 16, 2015

We can create globals.js or use our prepareExtends.js to include js functions like this.

@slavchev
Copy link
Author

I would be confused to use Array.of to create Java objects. If I read the docs correctly

Array.of(element0[, element1[, ...[, elementN]]])

it would create JavaScript array [element0<, element1,...>]. Let's keep it that way and don't change the standard JavaScript features.

@blagoev
Copy link
Contributor

blagoev commented Oct 16, 2015

Consider this as a better variant of Array.fromJavaType. So to take the best of both I suppose Array.ofJavaType is better. The "from" implies an iteratable objects in Javascript

@blagoev
Copy link
Contributor

blagoev commented Oct 16, 2015

Also consider this syntax
Array.create(android.view.View, 20);

@slavchev
Copy link
Author

I am fine with Array.create. The point is to avoid any breaking change. For example if you currently run

var a = Array.of(java.lang.Object, 10);
console.log("a.length=" + a.length);

it will print 2 as expected. So, in short Array.create sounds good to me.

@atanasovg
Copy link
Contributor

+1 for Array.create(type, count)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants