Bug 9603 – Signals must work with closure delegates

Status
RESOLVED
Resolution
WONTFIX
Severity
major
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-02-27T02:37:47Z
Last change time
2019-11-12T12:56:24Z
Keywords
wrong-code
Assigned to
No Owner
Creator
Denis Shelomovskii

Comments

Comment #0 by verylonglogin.reg — 2013-02-27T02:37:47Z
Without this ability signals are very inconvenient to use as closures are very common delegates and are an official way to reorder/ignore function parameters. Also current implementation is very dangerous as passing a closure results in undefined behavior.
Comment #1 by verylonglogin.reg — 2013-02-27T02:41:07Z
This issue will be automatically solved e.g. once Issue 9601 and Issue 9602 are implemented.
Comment #2 by verylonglogin.reg — 2013-02-27T09:08:09Z
This code will work once the issue will be fixed: --- import std.signals; extern(C) void rt_finalize2(void* p, bool det = true, bool resetMemory = true); class MyObject { mixin Signal!int actionDone; // ... } class Observer { void watch(int i) { /* ... */ } void watchNo() { /* ... */ } void watchSq(int i) { /* ... */ } void connectSignals(MyObject obj) { obj.actionDone.connect(&watch); obj.actionDone.connect(i => watch(i)); // should behave as previous one obj.actionDone.connect(i => watchNo()); obj.actionDone.connect(i => watchSq(i * i)); } } void main() { auto obj = new MyObject; auto o = new Observer(); o.connectSignals(obj); // ... rt_finalize2(cast(void*) o); assert(obj.actionDone.slots_idx == 0); } ---
Comment #3 by bugzilla — 2019-11-12T12:56:24Z
I don't see a possibility to fix this inside of phobos. A complete rewrite of the module would probably be needed. For a fix outside of phobos see the two issues mentioned above. I started a PR [1] though, which adds closures to the warning and marks the warning in red. If anyone cares: The problem is in a call to _d_toObject, which is to be found inside of the runtime [2]. The docs for this function allready tell, that it will crash if called the wrong way. Adding an exception inside phobos before the crash seems not to be possible, because it's not possible to distinguish a "normal" delegate from a closure. [1] https://github.com/dlang/phobos/pull/7268 [2] https://dlang.org/library/rt/cast_/_d_to_object.html