D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2495 - const syntax for member functions needs better description
Summary: const syntax for member functions needs better description
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dlang.org (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: spec
Depends on:
Blocks:
 
Reported: 2008-12-06 13:23 UTC by Jerry Quinn
Modified: 2015-06-09 01:20 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jerry Quinn 2008-12-06 13:23:58 UTC
The syntax for declaring const member functions is not very clear.  There is no grammar.  You need to infer from various examples what to do.  For instance:

class B {}
class A {
  B x;
  const B getB() { return B; }
}

doesn't compile, because this is a const function returning a nonconst value.   I had to find the answer on a forum.  It's challenging to figure this out from the docs:

class B {}
class A {
  B x;
  const const(B) getB() { return B; }
}

Examples covering more use cases would probably help, as well as more formally describing how const is specified.  Perhaps modifying the example of an invariant member function to return a value (other than string) would help.