Bug 11148 – Can't implicitly convert const(BigInt) or immutable(BigInt) to BigInt

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-09-30T10:45:00Z
Last change time
2014-02-05T17:31:35Z
Keywords
pull
Assigned to
nobody
Creator
joseph.wakeling

Attachments

IDFilenameSummaryContent-TypeSize
1253bigconst.dCan't pass const(BigInt) to a function taking a BigInt argument.text/x-dsrc134
1254bigimm.dCan't pass immutable(BigInt) to function taking a BigInt argument.text/x-dsrc138
1260bigconv.dThird example, which tests implicit, explicit and cast-based conversiontext/x-dsrc310

Comments

Comment #0 by joseph.wakeling — 2013-09-30T10:45:06Z
Created attachment 1253 Can't pass const(BigInt) to a function taking a BigInt argument. const(BigInt) or immutable(BigInt) objects can't be assigned/implicitly converted to mutable BigInt values. The attached code gives examples where a const or immutable BigInt is passed to a function taking a (mutable) BigInt argument.
Comment #1 by joseph.wakeling — 2013-09-30T10:45:39Z
Created attachment 1254 Can't pass immutable(BigInt) to function taking a BigInt argument.
Comment #2 by joseph.wakeling — 2013-10-08T09:04:03Z
Created attachment 1260 Third example, which tests implicit, explicit and cast-based conversion In fact, there is no reliable way to convert from a qualified BigInt type to an unqualified one. The 3rd code example attached shows attempted conversion through both implicit conversion (simple assignment) and explicit (trying both std.conv.to and cast).
Comment #3 by hsteoh — 2013-10-10T11:15:45Z
Is this really a bug? BigInt is implemented as a reference type, so allowing a cast from const(BigInt) to BigInt would be a bug.
Comment #4 by joseph.wakeling — 2013-10-10T11:27:44Z
I think it's an issue that you can't pass an immutable(BigInt) to a function that accepts a BigInt as input. This makes it difficult to e.g. write generic functions that work with any integer type. I'm not sure what the appropriate way to deal with that is, though.
Comment #5 by hsteoh — 2013-10-10T11:39:18Z
You can't pass an immutable(BigInt) to a function that takes unqual BigInt, because that breaks the type system (and risks breaking immutability). The function needs to take const(BigInt) if it doesn't need to propagate qualifiers to its return type, or inout(BigInt) if it does. If the function needs to modify the BigInt, then it should rightly reject any attempt to pass in an immutable BigInt.
Comment #6 by joseph.wakeling — 2013-10-10T11:55:30Z
(In reply to comment #5) > If the function needs to modify the BigInt, then it should rightly reject any > attempt to pass in an immutable BigInt. Well, suppose you have some function of the form, T foo(T)(T n) { Unqual!T a = n; // ... do mutable stuff with a return a; } That still won't work if T is an immutable(BigInt), because the first line will fail: you can't copy from the immutable to mutable.
Comment #7 by braddr — 2013-10-10T16:28:11Z
BigInt must behave essentially exactly like every other integral type (except for having a larger range of possible values), which includes being a value type. Any internal COW implementation must be invisible to users.
Comment #8 by simen.kjaras — 2013-11-01T18:29:34Z
Comment #9 by bearophile_hugs — 2013-11-18T02:49:26Z
Comment #10 by safety0ff.bugz — 2014-02-05T17:31:35Z