Re: local const functions - bug ?

2016-07-12 Thread Marc Schütz via Digitalmars-d-learn

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


Re: local const functions - bug ?

2016-07-10 Thread Basile B. via Digitalmars-d-learn

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).


Re: local const functions - bug ?

2016-07-10 Thread Meta via Digitalmars-d-learn

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.


Re: local const functions - bug ?

2016-07-08 Thread Marc Schütz via Digitalmars-d-learn

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.


Re: local const functions - bug ?

2016-07-07 Thread Jonathan M Davis via Digitalmars-d-learn
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



Re: local const functions - bug ?

2016-07-07 Thread Basile B. via Digitalmars-d-learn

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.


Re: local const functions - bug ?

2016-07-07 Thread Edwin van Leeuwen via Digitalmars-d-learn

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


Re: local const functions - bug ?

2016-07-07 Thread ag0aep6g via Digitalmars-d-learn

On 07/07/2016 12:33 PM, Basile B. wrote:

Do you think it's a bug ?


Yes.