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

ag0aep6g anonymous at example.com
Mon Jan 11 19:28:56 UTC 2021


On Monday, 11 January 2021 at 18:51:04 UTC, Jack wrote:
> Here's what I'm trying to make to work:
>
> import std.container : SList;
>
> class C
> {
>     static immutable Foo = new C();
>    // ....
> }
>
> alias Callback = void function(const C, int);
>
> void main()
> {
>     auto l = SList!Callback();
>     auto a = (C c, int d) { };
>     auto b = (C c, int d) { };
>     auto c = (const C c, int d) { };
>     l.insert(a);
>     l.insert(b);
>     l.insert(c);
> }

I'm assuming that you then want to call the callbacks on mutable 
and immutable `C`s like `C.Foo`.

You have to add `const` to the `a` and `b` functions, too:

     auto a = (const C c, int d) { };
     auto b = (const C c, int d) { };

Without those `const`s, you have callbacks with mutable 
parameters being called on an immutable object. That cannot work.


More information about the Digitalmars-d-learn mailing list