On Mon, Apr 30, 2012 at 11:46:09AM +0400, Dmitry Olshansky wrote: > On 30.04.2012 9:33, H. S. Teoh wrote: [...] > >I think the latter area has lots of room for improvement. From a > >theoretical standpoint, syntax like [1,2,3,4,5] really should not be > >prematurely tied to a specific type: at the most fundamental level, > >it's just specifying a list of things. How the abstract concept of a > >list of things should be implemented need not be constrained to > >concrete built-in types; I'd argue that the language should permit > >the realization of the concept in a user-defined type as well (i.e., > >the literal interpreted by a user type). > > > >Just off the top of my head, this might be achievable by introducing > >a fromList method in user-defined types that takes a compile-time > >parameter containing some representation of the list, say as a > >builtin array. This method then does whatever is needed to create an > >instance of the type accordingly. (Since the parameter is > >compile-time, this essentially just generates code to create the > >custom container directly.) [...] > *cough* initializer lists *cough* > > Seriously let AA liters be an sorted initializer list (conceptual > compile-time array) of pairs, and normal array literal just plain > initializer list of common type. > > This also nails nicely strange use case of AA literal to build array > (just the same sorted table of pairs!). The AA then is just > initialized with sorted array at run-time. (as it does anyway) [...]
+1, this is an excellent idea! It also solves the case of byte[], uint[], int[], etc., all being initializable by "[1,2,3]". Currently, subtle bugs are introduced in certain contexts by the compiler interpreting [1,2,3] to be int[] by default. What we really should do is to pass it in as a compile-time initializer list to the relevant ctors. So byte[].this() will interpret [1,2,3] as bytes, uint[].this() will interpret it as uints, etc.. Then you can write: uint[] a = [ 2: 100, 10: 200 ] and it will be equivalent to: uint[] a = [0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 200]; T -- The most powerful one-line C program: #include "/dev/tty" -- IOCCC