digitalmars.D.learn - How to fake a const method ?
- Basile B. (26/28) Mar 09 2018 I would need this to work:
- Basile B. (4/7) Mar 09 2018 And the most annoying is that the non reduced Foo has a custom
- Basile B. (4/11) Mar 09 2018 Other annoying fact:
- Basile B. (4/32) Mar 09 2018 Solved...The error message helped much actually... problem is
- Jonathan M Davis (10/56) Mar 09 2018 Yes. That's what happens when you mark the function as const. Making the
I would need this to work: ``` struct Foo { TypeInfo typeCache; TypeInfo getTypeCache() { alias NotThePoint = ubyte; if (typeCache is null) typeCache = typeid(NotThePoint); return typeCache; } TypeInfo type() const { alias NothrowType = TypeInfo delegate() const; return (cast(NothrowType) &getTypeCache)(); } } void main(){} ``` But get:Error: mutable method `Foo.getTypeCache` is not callable using a `const` `this`- `type()` really has to be const because it's used in an opEquals that's used by an AA (key is const so opEquals has to as well) - `typeCache` is not used to compare 2 Foos so it really doesn't matter if it get assigned.
Mar 09 2018
On Friday, 9 March 2018 at 13:47:34 UTC, Basile B. wrote:- `type()` really has to be const because it's used in an opEquals that's used by an AA (key is const so opEquals has to as well)And the most annoying is that the non reduced Foo has a custom toHash that hashes a payload so the cast to const would really be perfectly valid.
Mar 09 2018
On Friday, 9 March 2018 at 13:54:07 UTC, Basile B. wrote:On Friday, 9 March 2018 at 13:47:34 UTC, Basile B. wrote:Other annoying fact: https://run.dlang.io/is/pmPSGr It used to be accepted from 2.061 to 2.071.2- `type()` really has to be const because it's used in an opEquals that's used by an AA (key is const so opEquals has to as well)And the most annoying is that the non reduced Foo has a custom toHash that hashes a payload so the cast to const would really be perfectly valid.
Mar 09 2018
On Friday, 9 March 2018 at 13:47:34 UTC, Basile B. wrote:I would need this to work: ``` struct Foo { TypeInfo typeCache; TypeInfo getTypeCache() { alias NotThePoint = ubyte; if (typeCache is null) typeCache = typeid(NotThePoint); return typeCache; } TypeInfo type() const { alias NothrowType = TypeInfo delegate() const; return (cast(NothrowType) &getTypeCache)(); } } void main(){} ``` But get:Solved...The error message helped much actually... problem is `this` is `const` https://run.dlang.io/is/UOR2i2Error: mutable method `Foo.getTypeCache` is not callable using a `const` `this`- `type()` really has to be const because it's used in an opEquals that's used by an AA (key is const so opEquals has to as well) - `typeCache` is not used to compare 2 Foos so it really doesn't matter if it get assigned.
Mar 09 2018
On Friday, March 09, 2018 14:12:10 Basile B. via Digitalmars-d-learn wrote:On Friday, 9 March 2018 at 13:47:34 UTC, Basile B. wrote:Yes. That's what happens when you mark the function as const. Making the this pointer/reference const is exactly what marking a member function const does. However, if your solution involves casting away const, you had better be 100% sure that the result is never mutated, because if it is mutated, you're violating the type system and could get weird and subtle bugs. It would be better if you could just remove const from the equation entirely, but if you're dealing with AA keys, that's a bit difficult, since they're immutable (forcing you to deal with either immutable or const). - Jonathan M DavisI would need this to work: ``` struct Foo { TypeInfo typeCache; TypeInfo getTypeCache() { alias NotThePoint = ubyte; if (typeCache is null) typeCache = typeid(NotThePoint); return typeCache; } TypeInfo type() const { alias NothrowType = TypeInfo delegate() const; return (cast(NothrowType) &getTypeCache)(); } } void main(){} ``` But get:Solved...The error message helped much actually... problem is `this` is `const` https://run.dlang.io/is/UOR2i2Error: mutable method `Foo.getTypeCache` is not callable using a `const` `this`- `type()` really has to be const because it's used in an opEquals that's used by an AA (key is const so opEquals has to as well) - `typeCache` is not used to compare 2 Foos so it really doesn't matter if it get assigned.
Mar 09 2018