September 28, 2009 Re: Interesting GCC extensions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Mon, 28 Sep 2009 13:27:13 -0400, Steven Schveighoffer thusly wrote:
> On Mon, 28 Sep 2009 12:58:57 -0400, bearophile <bearophileHUGS@lycos.com> wrote:
>
>> Jarrett Billingsley:
>>
>>> Which do you want? Do you want new features, or do you want things to be fixed?<
>>
>> The ideas shown in that post are low priority, so they can wait. But
>> things can be planned for the future too, you can start
>> thinking/planning about them even years before they are implemented
>> (see vector operations of D).
>> (The triple point syntax is probably out of time).
>
> Just so you know. In 5 months, (i.e. the future) nobody is going to remember that you posted this. I've noticed you replied to many a thread with "well, that's exactly what I suggested X months ago, but nobody listened to me." When you make 2 posts a week suggesting changes, you can't expect them all to be considered, most are lost in the noise.
You can filter out all posts not written by bearophile + the ones that are not roots of the discussion threads. :) They are mostly proposals. I do not know why he does not use the bugzilla - unintuitive interface perhaps? I have always tried to avoid bugzilla because of the horrible UI. More often, when I search for something, the search seems to be broken so I delegate the search to google.
|
September 28, 2009 Re: Interesting GCC extensions | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | bearophile wrote:
> Lutger:
>
>> We don't need an extension for this! Look:
>>
>> template Eval(string exp)
>> {
>> enum Eval = mixin(exp);
>> }
>>
>> template IsConstant(string exp)
>> {
>> enum IsConstant = __traits(compiles, Eval!exp);
>> }
>
>
> From what I see I think your code is useless for my purposes:
>
> // D2 code
> import std.stdio: writeln;
>
> template Eval(string exp) {
> enum Eval = mixin(exp);
> }
>
> template IsConstant(string exp) {
> enum IsConstant = __traits(compiles, Eval!exp);
> }
>
> template Tuple(T...) { alias T Tuple; }
>
> template Range(int stop) {
> static if (stop <= 0)
> alias Tuple!() Range;
> else
> alias Tuple!(Range!(stop-1), stop-1) Range;
> }
>
> long ipow(long x, int n) {
> long result = 1;
>
> static if (IsConstant!("n")) {
> pragma(msg, "constant");
> static if (n < 5) {
> foreach (i; Range!(n))
> result *= x;
> } else {
> for (int i; i < n; i++)
> result *= x;
> }
> } else {
> pragma(msg, "not constant");
> for (int i; i < n; i++)
> result *= x;
> }
>
> return result;
> }
>
> enum int w = 5;
> void main() {
> int x = 2;
> const int y = 3;
> enum int z = 4;
> writeln(IsConstant!("x"), " ", IsConstant!("y"),
> " ", IsConstant!("z"), " ", IsConstant!("w"));
> // prints: false false false true
>
> ipow(10, x);
> ipow(10, y);
> ipow(10, z);
> }
>
> Even "z" is seen as not constant?
>
> Bye,
> bearophile
They are not compile time constants. Have you tried it with GCC? It gives the exact same results.
|
September 28, 2009 Re: Interesting GCC extensions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lutger | Lutger wrote:
> bearophile wrote:
>
>> Lutger:
>>
>>> We don't need an extension for this! Look:
>>>
>>> template Eval(string exp)
>>> {
>>> enum Eval = mixin(exp);
>>> }
>>>
>>> template IsConstant(string exp)
>>> {
>>> enum IsConstant = __traits(compiles, Eval!exp);
>>> }
>>
>>
>> From what I see I think your code is useless for my purposes:
>>
>> // D2 code
>> import std.stdio: writeln;
>>
>> template Eval(string exp) {
>> enum Eval = mixin(exp);
>> }
>>
>> template IsConstant(string exp) {
>> enum IsConstant = __traits(compiles, Eval!exp);
>> }
>>
>> template Tuple(T...) { alias T Tuple; }
>>
>> template Range(int stop) {
>> static if (stop <= 0)
>> alias Tuple!() Range;
>> else
>> alias Tuple!(Range!(stop-1), stop-1) Range;
>> }
>>
>> long ipow(long x, int n) {
>> long result = 1;
>>
>> static if (IsConstant!("n")) {
>> pragma(msg, "constant");
>> static if (n < 5) {
>> foreach (i; Range!(n))
>> result *= x;
>> } else {
>> for (int i; i < n; i++)
>> result *= x;
>> }
>> } else {
>> pragma(msg, "not constant");
>> for (int i; i < n; i++)
>> result *= x;
>> }
>>
>> return result;
>> }
>>
>> enum int w = 5;
>> void main() {
>> int x = 2;
>> const int y = 3;
>> enum int z = 4;
>> writeln(IsConstant!("x"), " ", IsConstant!("y"),
>> " ", IsConstant!("z"), " ", IsConstant!("w"));
>> // prints: false false false true
>>
>> ipow(10, x);
>> ipow(10, y);
>> ipow(10, z);
>> }
>>
>> Even "z" is seen as not constant?
>>
>> Bye,
>> bearophile
>
> They are not compile time constants. Have you tried it with GCC? It gives the exact same results.
Actually not for z, I don't know why this doesn't work with dmd. It does say it is a constant.
|
September 29, 2009 Re: Interesting GCC extensions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | Adam D. Ruppe wrote: > Trying it, I see it doesn't actually work in initialization, but you can do it after the fact easily enough: > > int[101] a; > a[0..9] = 1; > a[10..99] = 2; > a[100] = 3; This leaves elements 9 and 99 uninitialized. I assume the gcc version does not. -- Rainer Deyke - rainerd@eldwood.com |
September 29, 2009 Re: Interesting GCC extensions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rainer Deyke | On Mon, Sep 28, 2009 at 09:04:27PM -0600, Rainer Deyke wrote: > > int[101] a; > > a[0..9] = 1; > > a[10..99] = 2; > > a[100] = 3; > > This leaves elements 9 and 99 uninitialized. I assume the gcc version does not. That makes sense. I copied it literally without thinking. Easy enough fix. I think this makes D's syntax superior. We can say a[0..$] where gcc would have to use the more annoying a[0...$-1]. (pretending the $ worked of course) -- Adam D. Ruppe http://arsdnet.net |
September 29, 2009 Half-open versus closed ranges, was Re: Interesting GCC extensions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | Adam D. Ruppe wrote: > I think this makes D's syntax superior. We can say a[0..$] where > gcc would have to use the more annoying a[0...$-1]. > (pretending the $ worked of course) Although I generally prefer half-open ranges over closed ranges, both have their advantages and disadvantages. Advantages of half-open ranges: - Empty ranges are valid: [0 .. 0] - Easy for subranges to go to the end of the original: [x .. $] - Easy to split ranges: [0 .. x] and [x .. $] Advantages of closed ranges: - Symmetry. - Arguably easier to read. - ['a' ... 'z'] does not require awkward '+ 1' after 'z'. - [0 ... uint.max] is possible. -- Rainer Deyke - rainerd@eldwood.com |
Copyright © 1999-2021 by the D Language Foundation