How to fake a const method ?

Basile B. b2.temp at gmx.com
Fri Mar 9 13:47:34 UTC 2018


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.


More information about the Digitalmars-d-learn mailing list