The 2-frames-problem boilerplate solution
Elmar
chrehme at gmx.de
Mon Sep 27 18:00:42 UTC 2021
Hello D people,
from time to time I stumble upon the problem that methods don't
support `alias` parameters. The compiler will emit a complaint
which is probably also cryptic for beginners. (Including
references/links for further reading or explanation in compiler
error messages would be good, but offtopic.)
I remember the reason for deprecation was that GCC and LDC have
problems supporting more than one frame for a function because of
their code base.
I solve this conflict for myself this way:
```D
private int myStaticMethod(T, alias var)()
{
with(T)
{
// do something
}
}
struct S
{
// other things
static alias myStaticMethod(alias var) =
.myStaticMethod!(typeof(this), var);
}
```
This is pretty much boilerplate stuff and not intuitive to
understand. I catched myself mis-refactoring it two times.
D doesn't support this at the moment, right? Couldn't the
compiler narrow this snippet
```D
struct S {
static int myStaticMethod(alias var)() { ... }
}
```
to the above? Likewise for non-static methods.
What problems would this solution incur compared to deprecating
it?
Or is it rather, that UDAs for methods are so unimportant for
many cases so that we shouldn't care?
I admit I only use it for safety reasons as a double check to
only allow explicitly attributed variables as method arguments
(which also should be better to read).
But there are advantages of having it as method instead of global
function.
More information about the Digitalmars-d
mailing list