Reputation: 8064
I'd like to write D bindings. For the life of the class A
its someStruct *
member variable will never change, so I guess it is natural to declare it as immutable
. But then its constructor wouldn't compile unless I manage somehow to declare the APICall
function's return value as const
. How to do that?
struct someStruct;
const someStruct* APICall();
class A
{
this()
{
this.ptr = myfunc();
}
private:
immutable someStruct* ptr;
}
Error: function app.APICall without 'this' cannot be const
Upvotes: 2
Views: 120
Reputation: 25595
You want to use parenthesis around the return value:
const(someStruct*) APICall();
or if it never changes, immutable
is better. (const
is mostly useful on function parameters rather than return values)
const or immutable without parenthesis before or after a function declaration applies to the this
parameter, which is why the error says what it says: you are trying to apply it to a this
which isn't there.
But, before doing this, make sure it is actually immutable - that the pointer never changes AND that the data it points to never changes either. If there's any mutability through the pointer, you should just make it mutable.
Upvotes: 2