digitalmars.D.learn - Cannot have properties on const references?
- simendsjo (13/13) Sep 24 2011 Sorry about the possible double post. I cannot see my previous..
- Andrej Mitrovic (9/14) Sep 24 2011 I've reported the error message just recently because it's very
- Jonathan M Davis (4/24) Sep 24 2011 Property functions are still functions. A member function must be const ...
- Tobias Pankrath (2/5) Sep 25 2011 Since we have transitive const, why can't the compiler deduce which
- Jonathan M Davis (15/20) Sep 25 2011 You mean, why can't it just figure out whether a particular function can...
- Steven Schveighoffer (6/19) Sep 26 2011 If this is the error message it is a bug.
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 () }
Sep 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; } }
Sep 24 2011
On Saturday, September 24, 2011 20:19:43 Andrej Mitrovic wrote:On 9/24/11, simendsjo <simendsjo gmail.com> wrote: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 Davisstruct 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; } }
Sep 24 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?
Sep 25 2011
On Sunday, September 25, 2011 12:10:42 Tobias Pankrath wrote: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 DavisProperty 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?
Sep 25 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
Sep 26 2011