Thread overview
const, auto and struct/class methods
Nov 25, 2013
Andrea Fontana
Nov 25, 2013
bearophile
Nov 26, 2013
Meta
November 25, 2013
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
November 25, 2013
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()));

?
November 25, 2013
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!

November 25, 2013
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
November 25, 2013
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.
November 25, 2013
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.
November 26, 2013
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.