Thread overview | ||||||
---|---|---|---|---|---|---|
|
September 17, 2017 Propagating constness through function results | ||||
---|---|---|---|---|
| ||||
Hi, I have a class `Image`, and I have a function called `getSubImage(Rect bounds)`. What I can't figure out is how to get the result of `getSubImage()` to take on the constness of the backing image. ie. //The Image class is really just a view over a buffer that's managed elsewhere const(Image).getSubImage(...) -> const(Image) Image.getSubImage(...) -> Image I tried to do this with `inout`, but that requires that a parameter in the function be `inout`. I don't suppose I could somehow declare `this` to be inout? Thanks |
September 17, 2017 Re: Propagating constness through function results | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Zhang | On Sunday, 17 September 2017 at 21:18:08 UTC, David Zhang wrote:
> Hi,
>
> I have a class `Image`, and I have a function called `getSubImage(Rect bounds)`. What I can't figure out is how to get the result of `getSubImage()` to take on the constness of the backing image.
>
> ie.
> //The Image class is really just a view over a buffer that's managed elsewhere
> const(Image).getSubImage(...) -> const(Image)
>
> Image.getSubImage(...) -> Image
>
> I tried to do this with `inout`, but that requires that a parameter in the function be `inout`. I don't suppose I could somehow declare `this` to be inout?
>
> Thanks
I am aware that you can duplicate the method, but it seems a bit unwieldy and excessive.
|
September 17, 2017 Re: Propagating constness through function results | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Zhang | Nevermind! I rediscovered the `inout`attribute. Though if I may say so, I have no idea how `inout` is supposed to indicate "whatever the constness of a". |
September 17, 2017 Re: Propagating constness through function results | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Zhang | On 9/17/17 5:37 PM, David Zhang wrote: > Nevermind! I rediscovered the `inout`attribute. Correct, inout applied to the function is actually applying to the 'this' parameter. Same as const or immutable functions as well. > > Though if I may say so, I have no idea how `inout` is supposed to indicate "whatever the constness of a". > What inout does is look at the mutability of the parameter and transfer it to the mutability of the return type. -Steve |
Copyright © 1999-2021 by the D Language Foundation