digitalmars.D.learn - local const functions - bug ?
- Basile B. (15/15) Jul 07 2016 this compiles without error:
- ag0aep6g (2/3) Jul 07 2016 Yes.
- Edwin van Leeuwen (3/19) Jul 07 2016 Is this related to:
- Basile B. (2/24) Jul 07 2016 I've opened a separate issue, it seems to be another case.
- Jonathan M Davis via Digitalmars-d-learn (12/27) Jul 07 2016 It makes no sense for const to be used on foo. foo is not a member funct...
- Marc =?UTF-8?B?U2Now7x0eg==?= (3/26) Jul 08 2016 `foo()` is effectively a delegate, therefore `const` applies to
- Meta (3/5) Jul 10 2016 AFAIK const on a function can only ever refer to the `this`
- Basile B. (16/21) Jul 10 2016 When there's no "this" pointer DMD emitts a message. If to your
- Marc =?UTF-8?B?U2Now7x0eg==?= (10/15) Jul 12 2016 To the `this` pointer, or the context, in case of delegates.
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
On 07/07/2016 12:33 PM, Basile B. wrote:Do you think it's a bug ?Yes.
Jul 07 2016
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
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:I've opened a separate issue, it seems to be another case.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
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
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:`foo()` is effectively a delegate, therefore `const` applies to the context.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.
Jul 08 2016
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
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: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).`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
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: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`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 12 2016