www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - const, auto and struct/class methods

reply Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
Hello all,

If I mark a struct or class method as const, this is assumed to apply to the 
entire method, i.e. that nothing in it will modify any internal data of the 
struct/class.

     struct Foo
     {
         const auto bar()
         {
             // I can't modify any of the
             // internal data of Foo here
         }
     }

Suppose instead that I want a method that _may_ modify internal data, but will 
return an entity that is itself const.  Is there any way to do this while
having 
the return type being declared via auto?

i.e. what I want to be able to do is something like,

      struct Foo
      {
           const(auto) bar()
           {
               // modifies internal data of Foo
               // but returns a const type
           }
      }

(Note that const(auto) of course is not valid D, I use it for illustrative 
purposes:-)

Is something like this possible, and if so, what's the correct notation?

Thanks and best wishes,

     -- Joe
Nov 25 2013
next sibling parent reply "Andrea Fontana" <nospam example.com> writes:
On Monday, 25 November 2013 at 09:05:39 UTC, Joseph Rushton 
Wakeling wrote:
 Hello all,

 If I mark a struct or class method as const, this is assumed to 
 apply to the entire method, i.e. that nothing in it will modify 
 any internal data of the struct/class.

     struct Foo
     {
         const auto bar()
         {
             // I can't modify any of the
             // internal data of Foo here
         }
     }

 Suppose instead that I want a method that _may_ modify internal 
 data, but will return an entity that is itself const.  Is there 
 any way to do this while having the return type being declared 
 via auto?

 i.e. what I want to be able to do is something like,

      struct Foo
      {
           const(auto) bar()
           {
               // modifies internal data of Foo
               // but returns a const type
           }
      }

 (Note that const(auto) of course is not valid D, I use it for 
 illustrative purposes:-)

 Is something like this possible, and if so, what's the correct 
 notation?

 Thanks and best wishes,

     -- Joe
auto bar() { return cast(const int) 10; } writeln(typeid(bar())); ?
Nov 25 2013
parent Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
On 25/11/13 10:13, Andrea Fontana wrote:
 auto bar() { return cast(const int) 10; }

 writeln(typeid(bar()));
Yup, I should have added that I would prefer to avoid a cast in the return statement :-) Thanks anyway!
Nov 25 2013
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Joseph Rushton Wakeling:

      struct Foo
      {
           const(auto) bar()
           {
               // modifies internal data of Foo
               // but returns a const type
           }
      }
Is this acceptable? struct Foo { auto bar() { const result = ...; return result; } } Bye, bearophile
Nov 25 2013
next sibling parent Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
On 25/11/13 12:00, bearophile wrote:
 Is this acceptable?

 struct Foo {
      auto bar() {
          const result = ...;
          return result;
      }
 }
Could work, nice thought :-) I was hoping for something in the function signature rather than internally, though.
Nov 25 2013
prev sibling parent reply Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
On 25/11/13 12:00, bearophile wrote:
 Is this acceptable?
Actually, your suggestion made me realize I could do even better -- here's the patch I came up with in the end: https://github.com/WebDrake/Dgraph/commit/34d6cfacee928b74d084cff7c2f6c438f5144436 The arrays in question are only ever assigned as slices of underlying data, so they can be const(size_t)[][] from the start. So, thank you! :-) I would still like to know if there's a way of enforcing const-ness in an auto return type, though. You never know when it could be useful.
Nov 25 2013
parent "Meta" <jared771 gmail.com> writes:
On Monday, 25 November 2013 at 12:13:42 UTC, Joseph Rushton 
Wakeling wrote:
 Actually, your suggestion made me realize I could do even 
 better -- here's the patch I came up with in the end:
 https://github.com/WebDrake/Dgraph/commit/34d6cfacee928b74d084cff7c2f6c438f5144436

 The arrays in question are only ever assigned as slices of 
 underlying data, so they can be const(size_t)[][] from the 
 start.

 So, thank you! :-)

 I would still like to know if there's a way of enforcing 
 const-ness in an auto return type, though.  You never know when 
 it could be useful.
This might be worth filing a bug report over. It definitely seems like a useful feature.
Nov 26 2013