Forcing inlining of delegates and lazy
ABrightLight
example at example.com
Mon Aug 31 17:36:55 UTC 2026
Hello. I am trying to figure out some way to get delegates or
lazy parameters to get inlined into the function they are called
with. For example, if we take this starting code:
```d
void foo () {
bar.blah(args);
bar.otherBlah;
foreach (item; bar.collection) {
item.action;
}
bar.moreBlah;
}
```
I like to rewrite this as:
```d
void withBoilerplate (lazy void expr) {
bar.blah(args);
bar.otherBlah;
expr;
bar.moreBlah;
}
void foo () {
withBoilerplate(
foreach (item; bar.collection) {
item.action;
}
);
}
```
You may notice that these sorts of rewrites are very common in
languages with AST macros, such as lisp. For example the
equivalent would be:
```lisp
(defmacro with-boilerplate (expr)
`(progn
(blah bar args)
(other-blah bar)
,expr
(more-blah bar)))
(defun foo ()
(with-boilerplate
(loop for item across (collection bar) do
(action item))))
```
These sorts of macros greatly aid in readability and
maintainability.
The drawback to the lazy/delegate approach so far is the extra
indirection of the function call. And the use of `pragma(inline)`
hasn't helped. Is there some way to get it to inline so that I
can use this sort of pattern in hot-code paths without the worry
that I'm introducing slowdowns in the name of better readability
and maintainability?
More information about the Digitalmars-d
mailing list