We're #1 on Hacker News at the moment!
Adam D. Ruppe
destructionator at gmail.com
Tue Apr 28 19:08:23 UTC 2020
On Tuesday, 28 April 2020 at 18:41:19 UTC, H. S. Teoh wrote:
> Y'know, this makes me wonder if it might be worth having
> something akin to a compile-time out-contract that asserts
> certain attributes of the returned type. For example:
That's not a bad idea. You can do that with an ordinary out
contract right now and my doc generator includes it too.
auto foo(T)(T t)
out(ret; isInputRange!(typeof(return))
{
return t;
}
of course it is run at runtime which means an isInputRange symbol
can be generated and such sooo not ideal....... but we could add:
enum ctfe(bool a) = a;
to force that. Maybe even static assert like that, but don't want
to add too much syntax lest the docs get ugly again and defeats
the point.
maybe.......
```
template Typed(Conds...) {
bool Typed(R)(R r) {
static foreach(Cond; Conds)
static assert(Cond!R);
return true;
}
}
auto foo(T)(T t)
out(ret; ret.Typed!(isInputRange))
{
return t;
}
```
That syntax isn't awful, could be fairly legible in docs, does CT
checks, optimize to literal `true` in the binary. I know, I know,
a bunch of templates but it works today.
More information about the Digitalmars-d
mailing list