Thread overview
Cannot have properties on const references?
Sep 24, 2011
simendsjo
Sep 24, 2011
Andrej Mitrovic
Sep 24, 2011
Jonathan M Davis
Sep 25, 2011
Tobias Pankrath
Sep 25, 2011
Jonathan M Davis
September 24, 2011
Sorry about the possible double post. I cannot see my previous..

struct S {
    @property int B() {
        return 1;
    }
}

void main() {
    S s1;
    auto a1 = s1.B; // ok
    const(S) s2;
    auto a2 = s2.B; // Error: function t.S.B () is not callable using argument types ()
}
September 24, 2011
On 9/24/11, simendsjo <simendsjo@gmail.com> wrote:
> struct S {
>      @property int B() {
>          return 1;
>      }
> }

I've reported the error message just recently because it's very uninformative. But the solution is to add const to your property function:

struct S {
     @property int B() const {
         return 1;
     }
}
September 24, 2011
On Saturday, September 24, 2011 20:19:43 Andrej Mitrovic wrote:
> On 9/24/11, simendsjo <simendsjo@gmail.com> wrote:
> > struct S {
> > 
> >      @property int B() {
> > 
> >          return 1;
> > 
> >      }
> > 
> > }
> 
> I've reported the error message just recently because it's very uninformative. But the solution is to add const to your property function:
> 
> struct S {
>      @property int B() const {
>          return 1;
>      }
> }

Property functions are still functions. A member function must be const for it to be callable on a const object. But that error message is horrible.

- Jonathan M Davis
September 25, 2011
> 
> Property functions are still functions. A member function must be const for it to be callable on a const object.

Since we have transitive const, why can't the compiler deduce which methods can be called on const instances?
September 25, 2011
On Sunday, September 25, 2011 12:10:42 Tobias Pankrath wrote:
> > Property functions are still functions. A member function must be const for it to be callable on a const object.
> 
> Since we have transitive const, why can't the compiler deduce which methods can be called on const instances?

You mean, why can't it just figure out whether a particular function can be const or not? Well technically, it probably could, but that complicates the compiler, and there are cases where you want to enforce that a particular function be const. If it were always inferred, you couldn't do that.

D _does_ now have inferrence for pure, nothrow, and @safe for templated functions, because it has to have that in order to be able to reasonably use pure, nothrow, or @safe with templated functions (since whether a templated function can be pure, nothrow, or @safe often depends on the template's arguments). But even with that added to the language, the compiler doesn't infer any of those attributes for non-templated functions. The programmer should be able to figure that out and decide whether they want them to be const or pure or whatever in those cases. The inference is only done when it's done, because it's needed..

- Jonathan M Davis
September 26, 2011
On Sat, 24 Sep 2011 14:10:48 -0400, simendsjo <simendsjo@gmail.com> wrote:

> Sorry about the possible double post. I cannot see my previous..
>
> struct S {
>      @property int B() {
>          return 1;
>      }
> }
>
> void main() {
>      S s1;
>      auto a1 = s1.B; // ok
>      const(S) s2;
>      auto a2 = s2.B; // Error: function t.S.B () is not callable using argument types ()
> }

If this is the error message it is a bug.

It should say:

function t.S.B () is not callable using argument types () const

I believe this is the message you'd get if it were a member function.

-Steve