Bug 3690 – Folding Complex!(Complex!T) to Complex!T
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-01-08T05:22:00Z
Last change time
2015-06-09T05:13:45Z
Assigned to
nobody
Creator
bugzilla
Comments
Comment #0 by bugzilla — 2010-01-08T05:22:54Z
This is a suggestion for a minor change to std.complex that can be implemented with the current DMD, by simply copy-pasting the small code snippet at the bottom.
The idea is to make Complex!(Complex!T) evaluate to just Complex!T. The rationale is:
1. Complex!(Complex!T) doesn't make sense at all.
2. Just like the real line is a subspace of the complex plane, the complex plane is also, strictly speaking, a subspace of itself.
3. It would make it possible to write functions like:
Complex!T sqrt(T)(T x) { ... }
auto i = sqrt(-1);
auto z = sqrt(i);
For such a simple example it is of course better to write ordinary function overloads, but more complicated cases quickly cause trouble for DMD's IFTI.
The real-life case that inspired me to write this request is the eigenvalues() function in SciD that calculates the eigenvalues of a matrix. It would make life simpler for me (and for the compiler) if I could write it like
Complex!T[] eigenvalues(T)(Matrix!T m, Complex!T[] buffer=null) { ... }
and test for the complex-ness of T using static ifs inside the function body, thus reducing the need for complicated IFTI with multiple function declarations. (The problem here is when the user does not give the optional parameter.)
Here's the code to implement the feature:
// Fold Complex!(Complex!T) to Complex!T
template Complex(Num : Complex!(U, Representation.cartesian),
Representation rep=Representation.cartesian, U)
{
alias Complex!(U, rep) Complex;
}
template Complex(Num : Complex!(U, Representation.polar),
Representation rep=Representation.cartesian, U)
{
alias Complex!(U, rep) Complex;
}