Bug 21169 – make checkedint as a drop-in replacement of native int/long

Status
NEW
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-08-17T06:06:19Z
Last change time
2024-12-01T16:37:30Z
Assigned to
No Owner
Creator
mw
Depends on
18024, 21758, 21759, 21760, 21761
Moved to GitHub: phobos#10435 →

Comments

Comment #0 by mingwu — 2020-08-17T06:06:19Z
This is follow up of this discussion: https://forum.dlang.org/thread/[email protected] We need to improve the following 8 points to make checkedint as a drop-in replacement of native int/long, i.e minimal or 0 modification of user's existing code that use native integers. (This is a follow-up from the other thread: https://forum.dlang.org/thread/[email protected]) I start this new thread to discuss and improve the usage of std.experimental.checkedint as a drop-in replacement of native long. https://dlang.org/phobos/std_experimental_checkedint.html `Checked` is a struct, try to use it as a `long` require some substantial change of existing code that previously use native long. I list 8 things I've found below. Suggestions or PR welcome, e.g. even on how to improve the current implementation of `struct Checked`, or help from the compiler. Thanks. https://github.com/mingwugmail/dlang_tour/blob/master/divbug.d#L38 ------------------------------------------------------------------------ alias Long = Checked!long; void use_checked() { long la, lb, lc; // native Long ca, cb, cc; // checkedint // 0. as array literal long[] larr = [1, 2, 3]; //Long[] carr = [1, 2, 3]; // Error: cannot implicitly convert expression [1, 2, 3] of type int[] to Checked!(long, Abort)[] Long[] carr = [checked(1L), checked(2L), checked(3L)]; // more verbose // 1. checkedint cannot be chain assigned la = lb = lc = 0; //ca = cb = cc = 0; // Error: expression cc.opAssign(0) is void and has no value ca = 0; // have to do separately cb = 0; cc = 0; // 2. have to use .get to call math func writeln(std.math.sgn(la)); writeln(std.math.abs(la)); //writeln(std.math.sgn(ca)); // Error: template std.math.sgn cannot deduce function from argument types !()(Checked!(long, Abort)), candidates are: //writeln(std.math.abs(ca)); // Error: template instance std.math.abs!(Checked!(long, Abort)) error instantiating writeln(std.math.sgn(ca.get)); // need .get writeln(std.math.abs(ca.get)); // need .get // 3. no opBinaryLeft lc = la * lb; //cc = la * cb; // Error: can only * a pointer, not a int cc = cb * la; // have to switch order // 4. std.conv don't work double d = 0; lb = d.to!long; //cb = d.to!Long; // Error: template instance std.conv.to!(Checked!(long, Abort)).to!double error instantiating cb = lround(d); lb = "0".to!long; //cb = "0".to!Long; // Error: template std.conv.toImpl cannot deduce function from argument types !(Checked!(long, Abort))(string), candidates are: cb = "0".to!long; // work around ok // 5. cannot assign to shared static struct S { shared long sl; shared Long sL; } S s; s.sl = la; //s.sL = la; // Error: template std.experimental.checkedint.Checked!(long, Abort).Checked.opAssign cannot deduce function from argument types !()(long) shared, candidates are: cast()s.sL = la; // need the `cast` // 6. format %d won't work writefln("%04d", la); // output: 0000 //writefln("%04d", ca); // Expected '%s' format specifier for type 'Checked!(long, Abort)' writefln("%s", ca); // output: Checked!(long, Abort)(0), can this be just be 0? writefln("%04d", ca.get); // output: 0000 // 7. atomic won't work core.atomic.atomicOp!"+="(s.sl, lb); //core.atomic.atomicOp!"+="(s.sL, cb); // Error: template instance core.atomic.atomicOp!("+=", Checked!(long, Abort), Checked!(long, Abort)) error instantiating //core.atomic.atomicFetchAdd(s.sL, cb); // Error: template core.atomic.atomicFetchAdd cannot deduce function from argument types !()(shared(Checked!(long, Abort)), Checked!(long, Abort)), candidates are: } ------------------------------------------------------------------------
Comment #1 by moonlightsentinel — 2020-08-17T09:43:43Z
Note that 5. is invalid, the native version errors with `-preview=nosharedaccess`
Comment #2 by dlang-bot — 2020-08-17T17:21:56Z
@quickfur created dlang/phobos pull request #7599 "std.experimental.checkedint should support chain assignment." mentioning this issue: - std.experimental.checkedint should support chain assignment. Cf. bug #21169. https://github.com/dlang/phobos/pull/7599
Comment #3 by hsteoh — 2020-08-17T17:22:25Z
Comment #4 by mingwu — 2020-08-17T17:33:28Z
(In reply to moonlightsentinel from comment #1) > Note that 5. is invalid, the native version errors with > `-preview=nosharedaccess` No matter which compiler options are needed, to be able to use as an *drop-in* replacement, both checkedint and native need to behave the same under the same flag.
Comment #5 by mingwu — 2020-08-24T19:59:52Z
more more: 8) Warning: struct Checked has method toHash, however it cannot be called with shared(const(Checked!(long, Abort))) this.
Comment #6 by dlang-bot — 2020-09-10T04:06:07Z
dlang/phobos pull request #7599 "std.experimental.checkedint should support chain assignment." was merged into master: - 25493144f517b5afed630f4907fc23fe9dfc7e1e by H. S. Teoh: std.experimental.checkedint should support chain assignment. Cf. bug #21169. https://github.com/dlang/phobos/pull/7599
Comment #7 by robert.schadek — 2024-12-01T16:37:30Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10435 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB