www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - local const functions - bug ?

reply Basile B. <b2.temp gmx.com> writes:
this compiles without error:


struct Foo
{
     int i;
     void bar()
     {
         void foo() const
         {
             i = 1;
         }
         foo;
     }
}

In this case "const" seems to be a noop. Do you think it's a bug 
? Shouldn't "const" be applied, despite of foo() inaccessibility ?
Jul 07 2016
next sibling parent ag0aep6g <anonymous example.com> writes:
On 07/07/2016 12:33 PM, Basile B. wrote:
 Do you think it's a bug ?
Yes.
Jul 07 2016
prev sibling next sibling parent reply Edwin van Leeuwen <edder tkwsping.nl> writes:
On Thursday, 7 July 2016 at 10:33:39 UTC, Basile B. wrote:
 this compiles without error:


 struct Foo
 {
     int i;
     void bar()
     {
         void foo() const
         {
             i = 1;
         }
         foo;
     }
 }

 In this case "const" seems to be a noop. Do you think it's a 
 bug ? Shouldn't "const" be applied, despite of foo() 
 inaccessibility ?
Is this related to: https://issues.dlang.org/show_bug.cgi?id=1983
Jul 07 2016
parent Basile B. <b2.temp gmx.com> writes:
On Thursday, 7 July 2016 at 12:24:08 UTC, Edwin van Leeuwen wrote:
 On Thursday, 7 July 2016 at 10:33:39 UTC, Basile B. wrote:
 this compiles without error:


 struct Foo
 {
     int i;
     void bar()
     {
         void foo() const
         {
             i = 1;
         }
         foo;
     }
 }

 In this case "const" seems to be a noop. Do you think it's a 
 bug ? Shouldn't "const" be applied, despite of foo() 
 inaccessibility ?
Is this related to: https://issues.dlang.org/show_bug.cgi?id=1983
I've opened a separate issue, it seems to be another case.
Jul 07 2016
prev sibling parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Thursday, July 07, 2016 10:33:39 Basile B. via Digitalmars-d-learn wrote:
 this compiles without error:


 struct Foo
 {
      int i;
      void bar()
      {
          void foo() const
          {
              i = 1;
          }
          foo;
      }
 }

 In this case "const" seems to be a noop. Do you think it's a bug
 ? Shouldn't "const" be applied, despite of foo() inaccessibility ?
It makes no sense for const to be used on foo. foo is not a member function, so there's nothing for the const to apply to. So, the compiler is clearly ignoring a nonsensical attribute, which it often does. For better or worse, issues like that tend to be declared to not be bugs, which can make sense in some cases with generic code and mixins but often is just confusing (and in this particular case, I don't see why it would even help with generic code, since it could never apply under any circumstances). So, I'm all for reporting it as a bug, but it wouldn't surprise me if the compiler devs decided that it wasn't one. But regardless, because foo is not a member function, the assignment is completely valid. It's the attribute that isn't. - Jonathan M Davis
Jul 07 2016
parent reply Marc =?UTF-8?B?U2Now7x0eg==?= <schuetzm gmx.net> writes:
On Thursday, 7 July 2016 at 15:02:29 UTC, Jonathan M Davis wrote:
 On Thursday, July 07, 2016 10:33:39 Basile B. via 
 Digitalmars-d-learn wrote:
 this compiles without error:


 struct Foo
 {
      int i;
      void bar()
      {
          void foo() const
          {
              i = 1;
          }
          foo;
      }
 }

 In this case "const" seems to be a noop. Do you think it's a 
 bug ? Shouldn't "const" be applied, despite of foo() 
 inaccessibility ?
It makes no sense for const to be used on foo. foo is not a member function, so there's nothing for the const to apply to.
`foo()` is effectively a delegate, therefore `const` applies to the context.
Jul 08 2016
parent reply Meta <jared771 gmail.com> writes:
On Friday, 8 July 2016 at 09:01:10 UTC, Marc Schütz wrote:
 `foo()` is effectively a delegate, therefore `const` applies to 
 the context.
AFAIK const on a function can only ever refer to the `this` pointer, but there is no `this` pointer.
Jul 10 2016
next sibling parent Basile B. <b2.temp gmx.com> writes:
On Sunday, 10 July 2016 at 07:20:29 UTC, Meta wrote:
 On Friday, 8 July 2016 at 09:01:10 UTC, Marc Schütz wrote:
 `foo()` is effectively a delegate, therefore `const` applies 
 to the context.
AFAIK const on a function can only ever refer to the `this` pointer, but there is no `this` pointer.
When there's no "this" pointer DMD emitts a message. If to your eyes there is no "this" pointer then there should be a message for this case. This is also how I see the whole thing. Initially I had the intuition that "const" in this case is pointless (but >>only<< because the local proc. cannot be called from elsewhere than the parent function). The problem with "noop attributes" is that they tend to confuse new comers. There is other cases in D, notably when one declares a static function in the global scope. Such "noop attributes" should be detected by the compiler. What if some day you, at Dlang, decide to give a semantic to those cases while users code already use them ? (please don't answer "Dfix", this is just as a matter of principle that i talk about this).
Jul 10 2016
prev sibling parent Marc =?UTF-8?B?U2Now7x0eg==?= <schuetzm gmx.net> writes:
On Sunday, 10 July 2016 at 07:20:29 UTC, Meta wrote:
 On Friday, 8 July 2016 at 09:01:10 UTC, Marc Schütz wrote:
 `foo()` is effectively a delegate, therefore `const` applies 
 to the context.
AFAIK const on a function can only ever refer to the `this` pointer, but there is no `this` pointer.
To the `this` pointer, or the context, in case of delegates. Here, `foo()` is a nested function that accesses a variable in the outer scope. This qualifies it as a delegate [1]: "When comparing with nested functions, the function form is analogous to static or non-nested functions, and the delegate form is analogous to non-static nested functions. In other words, a delegate literal can access stack variables in its enclosing function, a function literal cannot." [1] https://dlang.org/spec/expression.html#FunctionLiteral
Jul 12 2016