On 2010-12-03 21:25:01 -0500, "Steven Schveighoffer" <[email protected]> said:

On Fri, 03 Dec 2010 21:19:14 -0500, Michel Fortin <[email protected]> wrote:

On 2010-12-03 21:02:10 -0500, "Steven Schveighoffer" <[email protected]> said:

On Fri, 03 Dec 2010 20:40:23 -0500, Michel Fortin <[email protected]> wrote:

I have an idea that would fix those: make a template struct/class instance implicitly convertible to another instance of that same template if all members share the same memory layout and each member is implicitly convertible to the same member of the other template.
I had thought of that too, a long time ago, but I wasn't sure if it could work. I'd go two steps further:
 1. all the member variable names must be identical.
2. you need to identify that implicit conversion is allowed.
 1 is for sanity ;)
 struct Pair(T)
{
    T x;
    T y;
}
 struct ConstPair(T)
{
    const(T) y;
    const(T) x;
}
2 is to ensure you are not able to incorrectly morph data into things it should not be. i.e.:
 struct Point
{
   int x;
   int y;
}
I don't think you should be able to implicitly cast Pair!int to Point, sometimes you want to define different APIs for the same data.

Just like you, I don't think you should be able to implicitly cast Pair!int to Point.

What I was suggesting is that the implicit cast would work only as long as the struct/class instance comes from the same template definition. That'd actually make Pair!int and Pair!uint convertible between each other (because int and uint are implicitly converted from one another) but ConstPair, which originate from a different template definition, wouldn't be convertible from Pair. Instead of defining ConstPair, you'd use Pair!(const(T)) to denote a pair of const elements, and because T is convertible to const(T), Pair!T is also convertible to Pair!(const(T))... as long as the memory layout is preserved, of course.

Oh, I misread your original idea, sorry.  I like the idea.

I just think it should be explicit that the 'same memory layout' means the names of the variables are the same. I just think of bizarre cases where static ifs are used to confuse things.

Yes, by "same memory layout" I mean the same variables, with the same names, occupying the same bytes.


--
Michel Fortin
[email protected]
http://michelf.com/

Reply via email to