<div dir="ltr"><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Mon, 9 Dec 2024 at 05:21, Steven Schveighoffer via Digitalmars-d <<a href="mailto:digitalmars-d@puremagic.com">digitalmars-d@puremagic.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Sunday, 8 December 2024 at 05:54:40 UTC, Manu wrote:<br>
> I've been trying out an API strategy to use sink functions a <br>
> lot more to hoist the item allocation/management logic to the <br>
> user and out from the work routines. This idea has received a <br>
> lot of attention, and I heard a lot of people saying this was a <br>
> planned direction to move in phobos and friends.<br>
><br>
> ```d<br>
> void parseThings(string s, void delegate(Thing) sink)<br>
> {<br>
> //...<br>
> sink(Thing());<br>
> //...<br>
> }<br>
> ```<br>
><br>
> We have a serious problem though; a function that receives a <br>
> sink function<br>
> must transfer the attributes from the sink function to itself, <br>
> and we have<br>
> no language to do that...<br>
> What are the leading strategies that have been discussed to <br>
> date? Is there<br>
> a general 'plan'?<br>
<br>
I've proposed something like the following in the past, but I <br>
don't know how much appetite there is for it:<br>
<br>
```d<br>
// assuming Thing() is @nogc nothrow @safe, but not pure<br>
void parseThings(string s, @called void delegate(Thing) sink) <br>
@nogc nothrow @safe<br>
{<br>
sink(Thing());<br>
}<br>
```<br>
<br>
Essentially, how this would work is that the compiler would <br>
consider all code inside the function except the call of the <br>
delegate to be attributed with the provided attributes. However, <br>
the delegate call will *always work*. All that will happen is <br>
that the call to `parseThings` will have any restrictions removed <br>
that aren't on the delegate.<br>
<br>
So e.g. a delegate of `@nogc pure` but not `nothrow` or `@safe` <br>
will mean `parseThings(dg)` will be treated like it's `@nogc`.<br>
<br>
Because the list of attributes `@nogc nothrow @safe` is <br>
logic-anded with `@nogc pure`.<br>
<br>
No recompile is necessary.<br>
<br>
If you want to insist that some attribute must be on the <br>
delegate, you can add the attribute to the delegate parameter. So <br>
if I wanted to insist that you must pass in at least a `@safe` <br>
delegate:<br>
<br>
```d<br>
void parseThings(string s, @called void delegate(Thing) @safe <br>
sink) { ... }<br>
```<br>
<br>
As far as I know, there is no plan to make something like this <br>
happen.<br>
<br>
-Steve<br></blockquote><div><br></div><div>Interesting idea. It seems like a novel solution. Approaching it from this perspective feels hard to reason about though... it feels like it would add serious friction to introspection activities?</div></div></div>