the best language I have ever met(?)

Artur Skawina via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Nov 25 09:20:11 PST 2016


On 11/25/16 17:30, Jonathan M Davis via Digitalmars-d-learn wrote:
> On Friday, November 25, 2016 17:03:32 Artur Skawina via Digitalmars-d-learn 
> wrote:
>> On 11/25/16 15:51, Jonathan M Davis via Digitalmars-d-learn wrote:
>>> On Friday, November 25, 2016 14:27:39 Igor Shirkalin via
>>> Digitalmars-d-learn>
>>> wrote:
>>>> I think you may write it (I mean actual D) with using some
>>>> template like this:
>>>>
>>>> auto array = static_array!uint(1, 2, 3, 4)
>>>>
>>>> Could you please help to writeown this template in the best and
>>>> clear manner?
>>>
>>> That's easy. The problem is if you want it to have the same semantics as
>>>
>>> uint[4] arr = [1, 2, 3, 4];
>>>
>>> In particular, VRP (Value Range Propagation) is a problem. This compiles
>>>
>>> ubyte[4] arr = [1, 2, 3, 4];
>>>
>>> because each of the arguments is known to fit in a ubyte. However,
>>> making
>>>
>>> auto arr = staticArray!ubyte(1, 2, 3, 4);
>>>
>>> do the same without forcing a cast is difficult. [...]
>>
>>    enum T[N] staticArray(T, alias ELS, size_t N=ELS.length) = ELS;
>>    auto arr = staticArray!(ubyte, [1, 2, 3, 4]);
> 
> That won't work with variables. e.g.
> 
> ubyte a;
> auto arr = staticArray!(ubyte, [1, 2, 3, 4, a]);
> 
> would fail to compile. It only works when all of the values are known at
> compile time, whereas
> 
> ubyte a;
> ubyte[5] arr = [1, 2, 3, 4, a];
> 
> would compile just fine.

Now you're trying to change the scope. Of course this is a hack,
that's only useful in certain contexts, such as initializing static
arrays with known values, which this subthread is about.

It actually makes the source code (look) worse; having to use lots of 
such module- or project-local hacks doesn't scale, and is a symptom
of language problems.

The point, however, was that that is the only way to get VRP - the
values must be available at CT for VRP to work effectively.
Your suggestion to "fix" VRP would break CTFE (different implicit
conversions would be allowed at CT and RT) and could result in
code compiling or not depending on the function arguments used,
possibly in a very unintuitive way (eg depending on if the function
args values happened to be CTFE-able).

artur



More information about the Digitalmars-d-learn mailing list