How to fake a const method ?

Basile B. b2.temp at gmx.com
Fri Mar 9 14:12:10 UTC 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:
>
>> 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.

Solved...The error message helped much actually... problem is 
`this` is `const`

https://run.dlang.io/is/UOR2i2


More information about the Digitalmars-d-learn mailing list