study: use checkedint as a drop-in replacement of native long

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Aug 15 05:10:01 UTC 2020


On Sat, Aug 15, 2020 at 04:28:05AM +0000, mw via Digitalmars-d wrote:
[...]
>   // 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;

This one should be easy to fix. Just change opAssign to return `this`
instead of void, and that should solve the problem.


[...]
>   // 3. no opBinaryLeft
>   lc = la * lb;
> //cc = la * cb;  // Error: can only * a pointer, not a int
>   cc = cb * la;  // have to switch order

You mean lacking opBinaryRight? That should be simple to implement?


>   // 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);

Checked should implement `opCast(T : double)()`.


>   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

Add a ctor to Checked that takes a string argument and parses it into a
Checked value.


[...]
>   // 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

Add an overload of toString that takes a FormatSpec argument:

	void toString(W,Char)(W sink, FormatSpec!Char fmt)
		if (isOutputRange!(W, Char))
	{
		if (fmt.spec == "d") { ... /* do the right thing here */ }
		... // etc.
	}

These should probably be done as a series of Phobos PRs to improve the
usability of Checked.


T

-- 
What's a "hot crossed bun"? An angry rabbit.


More information about the Digitalmars-d mailing list