array of elements of various sybtypes

spir denis.spir at gmail.com
Thu Jan 27 05:33:05 PST 2011


On 01/27/2011 05:05 AM, Steven Schveighoffer wrote:
> On Wed, 26 Jan 2011 22:10:58 -0500, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
>
>> On Wednesday 26 January 2011 18:59:50 Steven Schveighoffer wrote:
>>> On Wed, 26 Jan 2011 18:28:12 -0500, Jonathan M Davis <jmdavisProg at gmx.com>
>>>
>>>
>>> I'd like to see cast(T0[])[...] work, I think that should solve the
>>> problem.
>>
>> It probably doesn't for the exact same reason that the assignment didn't do it.
>> The expression is evaluated and _then_ it's cast. So, if the expression isn't
>> valid in and of itself, it fails.
>
> This works:
>
> cast(ubyte[])[1,2,3] // creates an array of 3 ubytes
>
> So clearly cast has an effect on the type of the array literal in that case.
> I'm not sure why this works and the other doesn't,
[1,2,3] is valid! [t1,t2] is not if one of the elements' type is not 
implicitely convertible to the other. In your example cast applies to an 
already constructed array. (Hope you see what I mean)

> but we definitely need
> something that allows one to control the array type of a literal.

Yop! But this hint has to belong to the literal notation syntax itself. Not 
anything (like cast ot to!) that applies afterwards.

> In D1, the array could be typed by casting the first element (the first element
> was always used as the type of the array). In D2 we no longer can control the
> type of the array that way, we need some way to do it.

Works in D2. If any element is of the inteded common type, then all goes fine.
But I'm not satisfied with that trick. We cast an element instead of typing the 
array literal. It's just a side-effect. (But I'll go with it as of now.)

> It probably can be done in the case of classes by casting an element to the
> desired type, but it seems rather hoaky to require two different methods of
> typing an array literal depending on what kinds of data are in the array. Plus,
> casting one element does not mean another element doesn't override it. It's
> better IMO to say "I want this array to be T0[]" and let the compiler fail if
> it can't cast all the elements rather then have the compiler do "oh, one of
> these is Object, so I'm actually going to re-type this as Object[]".

Agreed.

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d-learn mailing list