Hi, Classes have a default or zero value of null. Read about haszero and default/zero values in the language spec: http://dist.codehaus.org/x10/documentation/languagespec/x10-latest.pdf
4.5 Default Values Some types have default values, and some do not. Default values are used in situations where variables can legitimately be used without having been initialized; types without default values cannot be used in such situations. For example, a field of an object var x:T can be left uninitialized if T has a default value; it cannot be if T does not. Similarly, a transient (x8.2.3) field transient val x:T is only allowed if T has a default value. 2We call them Position to avoid confusion with the built-in class Point. Also, Position is a struct rather than a class so that the non-equality test start != end compares the coordinates. If Position were a class, start != end would check for different Position objects, which might have the same coordinates.48 CHAPTER 4. TYPES Default values, or lack of them, is defined thus: The fundamental numeric types (Int, UInt, Long, ULong, Float, Double) all have default value 0. Boolean has default value false. Char has default value '\0'. Struct types other than those listed above have no default value. A function type has a default value of null. A class type has a default value of null. The constrained type T{c} has the same default value as T if that default value satisfies c. If the default value of T doesn’t satisfy c, then T{c} has no default value. For example, var x: Int{x != 4} has default value 0, which is allowed because 0 != 4 satisfies the constraint on x. var y : Int{y==4} has no default value, because 0 does not satisfy y==4. The fact that Int{y==4} has precisely one value, viz. 4, doesn’t matter; the only candidate for its default value, as for any subtype of Int, is 0. y must be initialized before it is used. The predicate T haszero tells if the type T has a default value. haszero may be used in constraints. Example: The following code defines a sort of cell holding a single value of type T. The cell is initially empty – that is, has T’s zero value – but may be filled later. class Cell0[T]{T haszero} { public var contents : T; public def put(t:T) { contents = t; } { The built-in type Zero has the method get[T]() which returns the default value of type T. Example: A variant Cell1[T] which can be initialized with a value of an arbitrary type T, or, if T has a default value, can be created with the default value, is given below. Note that T haszero is a constraint on one of the constructors, not the whole type:4.6. FUNCTION TYPES 49 class Cell1[T] { public var contents: T; def this(t:T) { contents = t; } def this(){T haszero} { contents = Zero.get[T](); } public def put(t:T) {contents = t;} { 2011/4/5 Hai Chuan Wang <wangh...@cn.ibm.com> > David, Thank you! > > Suppose I will put class Edge into the Array. > How to define Edge hasZero constraint and provide a zeroValue for this > type? > > class Edge { > var first: int; > var second: int; > public def this (f: int, s: int) { > first = f; > second = s; > } > } > > Best Regards, > ----------------------------------------------------------------- > Hai Chuan(Haichuan) Wang, 王海川 > Research Staff Member, System Software, IBM Research - China > TEL: 86-21-60924401 FAX: 86-21-60923522 > > David P Grove <gro...@us.ibm.com> wrote on 2011-04-05 20:22:19: > > > From: David P Grove <gro...@us.ibm.com> > > To: Mailing list for users of the X10 programming language <x10- > > us...@lists.sourceforge.net> > > Date: 2011-04-05 20:27 > > Subject: Re: [X10-users] Create an generics Array[ET] with null as > > initial value. > > > > > > Hai Chuan Wang <wangh...@cn.ibm.com> wrote on 04/05/2011 05:11:39 AM: > > > > > > I'm writing a data structure with generics, and needs an internal array > > to > > > store data. > > > But the array should be initialized with null value at the beginning. > > > > > > public class Seq[ET] { > > > public var S:Array[ET](1){rail}; > > > public var sz:int; > > > > > > public def this(s:int) { > > > sz = s; > > > S = new Array[ET](sz, null); //<==== problem here > > > } > > > } > > > > > > Because ET has no "hasZero" attribute. I don't know how to create the > > array > > > with null as initial value. > > > > > > new Array[ET](sz, null), will treat "null" as "(int)=>ET", and throw > NPE. > > > null cannot be casted to ET type, either. > > > > > > Anyone can give some suggestions? > > > > Array is intended to be type safe, and in X10 an unconstrained T could be > > instantiated on some type S such that sizeof(S) bytes of zero's is not a > > valid value of type S. So, Array has been written to prevent you from > > doing this. > > > > What you can do is: > > (a) add the {ET haszero} constraint to Seq which will prevent such > > instantiations and thus allow you to call the Array constructor that > > zero-initializes the backing storage. > > (b) force the user to provide an "initial value" in the constructor > > and use that to initialize the array. > > (c) instead of using Array, use ArrayList or IndexedMemoryChunk for > > your backing storage. Both of these sidestep this piece of the X10 type > > system. ArrayList does it by using an IMC under the covers, while not > > allowing clients to see any uninitialized values. So ArrayList is type > > safe, but has some constraints on how it can be used (in particular, you > > always have to densely use the indices elements from 0...size-1; you > can't > > have gaps). IndexedMemoryChunk is not typesafe. > > (d) You can make your backing array be an Array[Cell[T]]. This > > injects an extra-level of indirection, and will not perform as well as > the > > other solutions. > > > > Personally, I think (a) is the most viable solution. I think it is a > > fairly natural outcome of the design point taken by the X10 type system > > that many generic collection types should have the {T haszero} constraint > > on them to allow efficient implementation. Arguably, this change should > be > > made in the x10.util collection classes as well. This then forces clients > > that need to put types that don't satisfy the haszero constraint into > > Cells/Boxes themselves before inserting them into the collection. This > is > > cumbersome, but I would suggest is putting the programming burden where > it > > most likely belongs. > > > > --dave > > > > ------------------------------------------------------------------------------ > > > Xperia(TM) PLAY > > It's a major breakthrough. An authentic gaming > > smartphone on the nation's most reliable network. > > And it wants your games. > > http://p.sf.net/sfu/verizon-sfdev > > _______________________________________________ > > X10-users mailing list > > X10-users@lists.sourceforge.net > > https://lists.sourceforge.net/lists/listinfo/x10-users > > ------------------------------------------------------------------------------ > Xperia(TM) PLAY > It's a major breakthrough. An authentic gaming > smartphone on the nation's most reliable network. > And it wants your games. > http://p.sf.net/sfu/verizon-sfdev > _______________________________________________ > X10-users mailing list > X10-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/x10-users > ------------------------------------------------------------------------------ Xperia(TM) PLAY It's a major breakthrough. An authentic gaming smartphone on the nation's most reliable network. And it wants your games. http://p.sf.net/sfu/verizon-sfdev _______________________________________________ X10-users mailing list X10-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/x10-users