Unfortunately, since Array is invariant, the inferred type may be
too precise as well, and thus this syntax is also used to force a
less precise type where needed. A prime example of this is
String.format(), which expects an Array[Any]. The inferred type of
[1,2,3,4] is Array[Int], which is not assignable to Array[Any]. So,
the choices are to force the inference to go the way you want by
explicitly casting one of the elements, namely "[1 as Any, 2, 3, 4]",
or to use this syntax: "new Array[Any] [1, 2, 3, 4]".
Igor
Bard Bloom/Watson/i...@ibmus wrote on 11/19/2010 07:36:16 AM:
> Well, the short-and-simple array constructor is like this:
> [1,2,3,4]
> which produces an Array[Int](1). That type, btw, means "An array of
Ints
> with 1 dimension."
>
> But suppose that you want an Array[Int{self!=0}]'s? -- an array of
> non-zero Ints?
> [1,2,3,4] looks like that, since they're all nonzero, but the type rules
> make it just Array[Int](1).
>
> So, by analogy with (1) Java array constants, and (2) object
construction,
> we let you tell what
> type you actually want in the array, by sticking on a prefix "new
Array[Int
> {self!=0}]" -- or more
> generally "new Array[T]".
> new Array[Int{self != 0}] [1,2,3,4]
> is an array of non-zero Ints, by type.
>
> I recommend using the simple syntax when you can. I believe that it
> usually does the right thing, and
> in particular does so in this case:
>
> val test2 <: Array[Place] = [host];
>
> (Apologies for sending you untested code.)
>
> If that doesn't give you the type you want, you can use the more
detailed
> constructor. But we hope
> that the simple constructor does what is wanted most of the time.
>
> See the spec: section 11.26
>
> -- Bard
>
>
> > Re: [X10-users] Array constructor?
> >
> > humm.....
> >
> > val test2 : Array[Place] = new Array[Place](host);
> >
> > *does not* compile... actually.
> >
> > I've tried this too:
> >
> > for (host in Place.places()) {
> > val test1 : Array[Place] = new Array[Place][host,host,host,host];
> > }
> >
> > .. which leads me to think that:
> >
> > 1. I'm declaring an Array with parametric type Place :: Array[Place]
> > 2. I'm initializing this Array[Place] with 4 elements of value "host"
> >
> > If I'm correct, the conclusion is that the syntax is pretty confused
:(
> >
> > Thanks
> >
> > Richard Gomes
> > M: +44(77)9955-6813
> > http://tinyurl.com/frgomes
> > twitter: frgomes
> >
> > JQuantLib is a library for Quantitative Finance written in Java.
> > http://www.jquantlib.org/
> > twitter: jquantlib
> >
> > On 19/11/10 08:40, Richard Gomes wrote:
> > > Hi guys,
> > >
> > > I've seen code more or less like this:
> > >
> > > for (host in Place.places()) {
> > > val workers : Array[Place] = new Array[Place][host];
> > > ...
> > > }
> > >
> > > What it means, exactly?
> > > I'm confused with text "[host]" which looks strange.
> > >
> > >
> > > I've changed "[host]" by "(host)" like shown below and it compiles.
> > >
> > > for (host in Place.places()) {
> > > val test1 : Array[Place] = new Array[Place][host]; //
> compiles
> > > val test2 : Array[Place] = new Array[Place](host); //
> compiles
> > > ...
> > > }
> > >
> > > So... looks like the compiler is automagically interpreting "[host]"
as
> > > "(host)" ???
> > >
> > >
> > > Thanks a lot
> > >
> > > Cheers :)
--
Igor Peshansky (note the spelling change!)
IBM T.J. Watson Research Center
X10: Parallel Productivity and Performance (http://x10-lang.org/)
XJ: No More Pain for XML's Gain (http://www.research.ibm.com/xj/)
"I hear and I forget. I see and I remember. I do and I understand" --
Xun Zi
------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1, ECMAScript5, and DOM L2 & L3.
Spend less time writing and rewriting code and more time creating great
experiences on the web. Be a part of the beta today
http://p.sf.net/sfu/msIE9-sfdev2dev
_______________________________________________
X10-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/x10-users