On Wed, 5 Jun 2019 17:51:34 -0400, Steven Schveighoffer wrote:

I have a bunch of routes that look like this:

item/:id
item/:id/edit
item/:id/delete

etc.

and for each one, I have to put in a path UDA to designate how to map
the id to the _id parameter.

I realized that actually, I can delegate this to a different object!

...Or so I thought, but I get this assertion:

Instances may only be returned from parameter-less functions

What I had is something like this:

@path(":id")
ItemHandler get(int _id)
{

itemHandler.id = _id;
return itemHandler;

}

Why does this limitation exist? I'm not even certain it's necessary,
even in the general case where the parameters come from the form data.

It would make writing all my handling code more modular and less
verbose. Is this something that should be supported, even if it's only
for route parameters?

-Steve

I think what you need is Collection!I.

The example above would look something like this:

interface Item {
    // defines the chain of fixed parameters that identify a single item
    struct CollectionIndices {
        int _id;
    }

    void get(int _id);
    void edit(int _id, ...);
    void delete(int _id, ...);
}

interface API {
    @property Collection!Item item();
}

Apart from unifying handling of the "path" parameters, it also enables a nicer, index based, access syntax from D: api.item[id].edit(...)

Collection!I can also be nested freely by extending the entries in the CollectionIndices struct (see the example in the API docs).