November 10, 2005 Re: Request: Evaluate constant ~ and [] before instantiating templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Don Clugston | In article <dkvq4q$1l21$1@digitaldaemon.com>, Don Clugston says... >>>>Allowing compatibility with user defined collection classes: >>>> >>>>Set!(int)(1,2,4) >>>>new Set!(double)(4,2,1.5) >>> >>>It ought to be possible for the UDT to support >>> >>> Set!(int) a = [1, 2, 4]; >>> >>>or at least >>> Set!(int)( [1, 2, 4] ); >>>? >> >> >> What is the type of [1,2,4]?. Is it int[] or (const) int[3] or ...? >> How would you specify for instance a ushort[]? What would get passed to the >> constructor of Set!(int)?. > >You're right, it might be too difficult. I was thinking that there could be a requirement that only one array constructor is permissible. One would want a syntax that felt consistent and worked with built in arrays, UDTs, built in associative arrays and combinations. Ideally, the syntax should also be similar for initilization and anonymous literals. [1,2,3] and ["jan":31, "feb":28] looks nice as syntax for arrays and aa, but both need to be tagged with a type to work as expressions. Or, they could be made implicitly castable to compatible types, which could work when only one applicable constructor is available. >The compiler manages to work it out in this example, so all kinds of implicit casting must be going on: >---------------------- >template whoknows(X) >{ > const X f[] = [1, 2, 3, 4]; >} Well... As far as I understand it, [1, 2, 3, 4] is parsed as an Initializer and not an Expression, and the entire declaration statement is not evaluated until the template is instantiated and X known. /Oskar |
November 10, 2005 Re: Request: Evaluate constant ~ and [] before instantiating templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | In article <dkvqj2$1lft$1@digitaldaemon.com>, Ivan Senji says... > >Don Clugston wrote: >> You're right, it might be too difficult. I was thinking that there could >> be a requirement that only one array constructor is permissible. >> The compiler manages to work it out in this example, so all kinds of >> implicit casting must be going on: > >But it wouldn't be difficult if array litterals had a type. >Now you can write a template that does this: >int[] a = array!(int)[1,2,3,4,5]; >Object[] b = array!(Object)[new Object, new Object]; > >and i wouldn't mind having to write: > >int[] x = new int[][1,2,3,4,5]; >or >int[] x = int[][1,2,3,4,5]; or something like this >instead of: >int[] x = [1,2,3,4,5]; Neither would I. >Or am i missing something? It is unfortunately ambiguous: new int[1][1]; Is this a 1 element array initialized with a 1, or an uninitialized 1x1 array? I guess this is why java and c# and others use {} instead of [] for array literals. /Oskar |
November 11, 2005 Re: Request: Evaluate constant ~ and [] before instantiating templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Oskar Linde | Oskar Linde wrote:
> In article <dkvq4q$1l21$1@digitaldaemon.com>, Don Clugston says...
>
>
>>>>>Allowing compatibility with user defined collection classes:
>>>>>
>>>>>Set!(int)(1,2,4)
>>>>>new Set!(double)(4,2,1.5)
>>>>
>>>>It ought to be possible for the UDT to support
>>>>
>>>>Set!(int) a = [1, 2, 4];
>>>>
>>>>or at least
>>>> Set!(int)( [1, 2, 4] );
>>>>?
>>>
>>>
>>>What is the type of [1,2,4]?. Is it int[] or (const) int[3] or ...?
>>>How would you specify for instance a ushort[]? What would get passed to the
>>>constructor of Set!(int)?.
>>
>>You're right, it might be too difficult. I was thinking that there could be a requirement that only one array constructor is permissible.
>
>
> One would want a syntax that felt consistent and worked with
> built in arrays, UDTs, built in associative arrays and combinations. Ideally, the syntax should also be similar for initilization and anonymous
> literals.
>
> [1,2,3] and ["jan":31, "feb":28] looks nice as syntax for arrays and aa,
> but both need to be tagged with a type to work as expressions. Or,
> they could be made implicitly castable to compatible types, which could
> work when only one applicable constructor is available.
>
>
>>The compiler manages to work it out in this example, so all kinds of implicit casting must be going on:
>>----------------------
>>template whoknows(X)
>>{
>> const X f[] = [1, 2, 3, 4];
>>}
>
>
> Well... As far as I understand it, [1, 2, 3, 4] is parsed as an Initializer and not an Expression, and the entire declaration statement is not evaluated
> until the template is instantiated and X known.
Ah, OK. Looking at Declaration.html, I see that each member of the array initialiser is an expression but the
initialiser itself is not an expression. But char [] initialisers are
expressions. No expressions involving array literals exist, but there ARE expressions involving const arrays. So it ought to be possible to support
const int [] c = a ~ b;
even without array literals as primary expressions. In fact, by my reading of the spec, this ought to work already.
The following already works:
template square(int c)
{
const int square = c * c;
}
template whoknows(X)
{
const X f[] = [square!(1), square!(2), square!(3), square!(4)];
}
Even when assigned to an short [], which is interesting (it checks each constant individually to see if it can fit into a short, even if it's declared as a long).
|
Copyright © 1999-2021 by the D Language Foundation