http://d.puremagic.com/issues/show_bug.cgi?id=3690

## Advertising

Summary: Folding Complex!(Complex!T) to Complex!T
Product: D
Version: future
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: Phobos
AssignedTo: nob...@puremagic.com
ReportedBy: bugzi...@kyllingen.net
--- Comment #0 from Lars T. Kyllingstad <bugzi...@kyllingen.net> 2010-01-08
05:22:54 PST ---
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;
}
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------