digitalmars.D.learn - const, auto and struct/class methods
- Joseph Rushton Wakeling (29/29) Nov 25 2013 Hello all,
- Andrea Fontana (5/36) Nov 25 2013 auto bar() { return cast(const int) 10; }
- Joseph Rushton Wakeling (3/5) Nov 25 2013 Yup, I should have added that I would prefer to avoid a cast in the retu...
- bearophile (10/18) Nov 25 2013 Is this acceptable?
- Joseph Rushton Wakeling (3/10) Nov 25 2013 Could work, nice thought :-) I was hoping for something in the function...
- Joseph Rushton Wakeling (9/10) Nov 25 2013 Actually, your suggestion made me realize I could do even better -- here...
- Meta (4/14) Nov 26 2013 This might be worth filing a bug report over. It definitely seems
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
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, -- Joeauto bar() { return cast(const int) 10; } writeln(typeid(bar())); ?
Nov 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!
Nov 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
Nov 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.
Nov 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.
Nov 25 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.
Nov 26 2013