A potential solution to callable parameter attributes #22905
Herringway
started this conversation in
Ideas
Replies: 2 comments 6 replies
-
|
Instead of assignment, this should probably use function pointer creation (&fun, lambdas, etc) instead of assignment as the point to check attributes. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
I might be misunderstanding this, but doesn't this completely change the meaning of attributes and/or break code? void throwing() { throw new Exception(""); }
void function() x = &throwing; // disallowed now?
void main() @nogc nothrow
{
x(); // allowed now?
}struct S
{
void function() callback;
void setCallback(void function() callback) @safe pure nothrow @nogc { this.callback = callback; }
}
void f() {}
void main() @safe
{
S s;
s.setCallback(&f); // disallowed now?
}That being said, I'm all for overhauling function attributes. Personally I would rip them all out of the type system entirely, but that may be an even more radical change. |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
D has long had a problem where callable parameters and variables must have compatible attributes with the function being called. The "solutions" we have to this problem all have serious drawbacks:
On top of that, these solutions only cover the parameters. What about, for example, function pointers in global variables? Struct/class fields?
There have been a few proposals over the years (though the only DIP I'm aware of is DIP1032) and all have been quite complex and/or ugly. But what if there's been a much simpler solution right under our noses this entire time?
Have attribute checks ignore calling function pointers. Call a
@systemdelegate in a@safefunction? a function pointer doing I/O in pure functions? Yes! Let's allow it!Instead, we do the attribute checks at assignment time. When the pointer is assigned to a variable or function parameter, the checks are done. For example:
Although these function pointers aren't being called in bar/baz, by treating them as if they are, we still get the benefits of the attribute checks. This can even extend to CTFE, now that we have
@systemvariables.Function pointers that have attributes still have their attribute checks performed at assignment as they currently do.
void delegate() @safe foo = &bar.systemFunction;will still fail to compile.What do you think? Does this proposal have any major holes? Rip and tear!
Beta Was this translation helpful? Give feedback.
All reactions