Easy way to accept X and immutable X in parameters without overloading?

ag0aep6g anonymous at example.com
Mon Jan 11 18:37:58 UTC 2021


On Monday, 11 January 2021 at 18:12:17 UTC, Jack wrote:
> thanks! now, how would I add const here?
>
> import std.container : SList;
> auto l = SList!Callabck();
>
> doesn't work:
>
> auto l = SList!(const(Callabck()));
> auto l = SList!(const Callabck());

You said you want the callbacks to accept both mutable and 
immutable. So make the parameter `const` in the callback type:

     alias Callabck = void function(const X foo);

If you wanted an `SList` of `const Callabck`s, you'd write that 
like so:

     auto l = SList!(const Callabck)();

But it seems like `SList` doesn't support const elements. And I 
don't think that's what you actually want anyways.

(By the way, you've got a typo there in "Callabck".)


More information about the Digitalmars-d-learn mailing list