Jump to page: 1 211  
Page
Thread overview
Another idiom I wish were gone from phobos/druntime
Feb 05, 2015
Rikki Cattermole
Feb 05, 2015
bearophile
Feb 05, 2015
Adam D. Ruppe
Feb 05, 2015
deadalnix
Feb 05, 2015
Zach the Mystic
Feb 05, 2015
bearophile
Feb 05, 2015
Zach the Mystic
Feb 05, 2015
deadalnix
Feb 05, 2015
Zach the Mystic
Feb 08, 2015
Marc Schütz
Feb 08, 2015
Zach the Mystic
Feb 08, 2015
Daniel Murphy
Feb 08, 2015
Zach the Mystic
Feb 05, 2015
ketmar
Feb 08, 2015
Marc Schütz
Feb 05, 2015
Tobias Pankrath
Feb 05, 2015
Jonathan Marler
Feb 05, 2015
deadalnix
Feb 05, 2015
Jonathan Marler
Feb 05, 2015
David Nadlinger
Feb 05, 2015
Jonathan M Davis
Feb 05, 2015
deadalnix
Feb 05, 2015
Dragos Carp
Feb 05, 2015
deadalnix
Feb 05, 2015
Jonathan Marler
Feb 05, 2015
deadalnix
Feb 05, 2015
Meta
Feb 05, 2015
H. S. Teoh
Feb 05, 2015
Meta
Feb 05, 2015
Vlad Levenfeld
Feb 05, 2015
Vlad Levenfeld
Feb 05, 2015
eles
Feb 05, 2015
Jonathan M Davis
Feb 05, 2015
Vlad Levenfeld
Feb 05, 2015
Mike Parker
Feb 05, 2015
Walter Bright
Feb 05, 2015
H. S. Teoh
Feb 06, 2015
Jonathan M Davis
Feb 05, 2015
Paulo Pinto
Feb 05, 2015
deadalnix
Feb 05, 2015
Paulo Pinto
Feb 05, 2015
deadalnix
Feb 05, 2015
Paulo Pinto
Feb 05, 2015
Paulo Pinto
Feb 06, 2015
bearophile
Feb 05, 2015
deadalnix
Feb 05, 2015
Paulo Pinto
Feb 05, 2015
deadalnix
Feb 05, 2015
eles
Feb 05, 2015
eles
Feb 05, 2015
Daniel Murphy
Feb 05, 2015
Daniel Murphy
Feb 05, 2015
Meta
Feb 05, 2015
Daniel Murphy
Feb 05, 2015
Daniel Murphy
Feb 05, 2015
Jonathan M Davis
Feb 05, 2015
Jonathan M Davis
Feb 05, 2015
Kagamin
Feb 05, 2015
Jonathan M Davis
Feb 05, 2015
Daniel Murphy
Feb 05, 2015
Jonathan M Davis
Feb 05, 2015
Daniel Murphy
Feb 05, 2015
Atila Neves
Feb 05, 2015
Walter Bright
Feb 05, 2015
deadalnix
Feb 05, 2015
H. S. Teoh
Feb 05, 2015
Jonathan Marler
Feb 05, 2015
Nick Treleaven
Feb 05, 2015
Kagamin
Feb 05, 2015
Nick Treleaven
Feb 05, 2015
H. S. Teoh
Feb 05, 2015
Jonathan M Davis
Feb 05, 2015
Jonathan M Davis
Feb 05, 2015
ketmar
Feb 06, 2015
Chris
Feb 06, 2015
ketmar
Feb 06, 2015
Chris
Feb 06, 2015
ketmar
Feb 06, 2015
ketmar
Feb 06, 2015
Chris
Feb 06, 2015
ketmar
Feb 06, 2015
Chris
Feb 07, 2015
ketmar
February 05, 2015
I'm seeing another idiom that seems to become fashionable. Consider this excerpt from https://github.com/D-Programming-Language/phobos/blob/master/std/algorithm/iteration.d#L407:

            auto opSlice(size_t low, size_t high)
            in
            {
                assert(low <= high);
            }
            body
            {
                import std.range : take;
                return this[low .. $].take(high - low);
            }

which of course trivially boils down to:

            auto opSlice(size_t low, size_t high)
            {
                assert(low <= high);
                import std.range : take;
                return this[low .. $].take(high - low);
            }

What advantage could possibly be in transforming a 5-liner into a 9-liner? Are we really aiming at writing the maximum possible lines of code here and using as many language features as possible everywhere?

I think we should dust off that Phobos contributors' guide. Sadly, there is little we can do against the more subtle issues.


Andrei
February 05, 2015
On 5/02/2015 1:24 p.m., Andrei Alexandrescu wrote:
> I'm seeing another idiom that seems to become fashionable. Consider this
> excerpt from
> https://github.com/D-Programming-Language/phobos/blob/master/std/algorithm/iteration.d#L407:
>
>
>              auto opSlice(size_t low, size_t high)
>              in
>              {
>                  assert(low <= high);
>              }
>              body
>              {
>                  import std.range : take;
>                  return this[low .. $].take(high - low);
>              }
>
> which of course trivially boils down to:
>
>              auto opSlice(size_t low, size_t high)
>              {
>                  assert(low <= high);
>                  import std.range : take;
>                  return this[low .. $].take(high - low);
>              }
>
> What advantage could possibly be in transforming a 5-liner into a
> 9-liner? Are we really aiming at writing the maximum possible lines of
> code here and using as many language features as possible everywhere?
>
> I think we should dust off that Phobos contributors' guide. Sadly, there
> is little we can do against the more subtle issues.
>
>
> Andrei

So are we recommending against contract based programming for simple getter type methods?
Personally I think those auto's are more important to get rid of in this code. They actually do harm for anyone reading the docs.
February 05, 2015
Andrei Alexandrescu:

>             auto opSlice(size_t low, size_t high)
>             in
>             {
>                 assert(low <= high);
>             }
>             body
>             {
>                 import std.range : take;
>                 return this[low .. $].take(high - low);
>             }
>
> which of course trivially boils down to:
>
>             auto opSlice(size_t low, size_t high)
>             {
>                 assert(low <= high);
>                 import std.range : take;
>                 return this[low .. $].take(high - low);
>             }
>
> What advantage could possibly be in transforming a 5-liner into a 9-liner?

Contracts can be read by tools, and they are part of the function signature. Contracts should be encouraged and increased, not discouraged.

Bye,
bearophile
February 05, 2015
On Thursday, 5 February 2015 at 00:35:50 UTC, bearophile wrote:
> Contracts can be read by tools, and they are part of the function signature. Contracts should be encouraged and increased, not discouraged.


I agree. Moreover, if the assert fails in the contract, in theory, we can point the error at the user's code. An assert inside the function is the function's responsibility. An assert in an in contract is the caller's responsibility. They're semantically different (even if dmd treats them the same way)
February 05, 2015
On Thursday, 5 February 2015 at 00:35:50 UTC, bearophile wrote:
> Andrei Alexandrescu:
>
>>            auto opSlice(size_t low, size_t high)
>>            in
>>            {
>>                assert(low <= high);
>>            }
>>            body
>>            {
>>                import std.range : take;
>>                return this[low .. $].take(high - low);
>>            }
>>
auto opSlice(size_t low, size_t high)
    in { assert(low <= high); }
body
{

}

February 05, 2015
On 2/4/15 4:30 PM, Rikki Cattermole wrote:
> So are we recommending against contract based programming for simple
> getter type methods?

The shorter version is as contract based as the other.

> Personally I think those auto's are more important to get rid of in this
> code. They actually do harm for anyone reading the docs.

That would be a separate discussion.


Andrei
February 05, 2015
On Thursday, 5 February 2015 at 00:35:50 UTC, bearophile wrote:
> Contracts can be read by tools, and they are part of the function signature. Contracts should be encouraged and increased, not discouraged.
>
> Bye,
> bearophile

Not to mention that contracts can be removed by the compiler at compile time. i.e.

x.opSlice(i, i+1);

The assert can be compiled away since i+1 is always >= i. That's one of the great benefits of contracts.
February 05, 2015
On 2/4/15 4:35 PM, bearophile wrote:
> Contracts can be read by tools, and they are part of the function
> signature.

The signature part matters only for overridable functions. I agree the part about tooling has merit. -- Andrei

February 05, 2015
On 2/4/15 4:40 PM, Jonathan Marler wrote:
> On Thursday, 5 February 2015 at 00:35:50 UTC, bearophile wrote:
>> Contracts can be read by tools, and they are part of the function
>> signature. Contracts should be encouraged and increased, not discouraged.
>>
>> Bye,
>> bearophile
>
> Not to mention that contracts can be removed by the compiler at compile
> time.

Same about asserts. -- Andrei


February 05, 2015
On 2/4/15 4:37 PM, Adam D. Ruppe wrote:
> On Thursday, 5 February 2015 at 00:35:50 UTC, bearophile wrote:
>> Contracts can be read by tools, and they are part of the function
>> signature. Contracts should be encouraged and increased, not discouraged.
>
>
> I agree. Moreover, if the assert fails in the contract, in theory, we
> can point the error at the user's code. An assert inside the function is
> the function's responsibility. An assert in an in contract is the
> caller's responsibility. They're semantically different (even if dmd
> treats them the same way)

Yah I concede this is a good point. Yet we're looking at an actual liability and are supposed to look at some vague possible future benefit. -- Andrei
« First   ‹ Prev
1 2 3 4 5 6 7 8 9 10 11