www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Value type, ref type, how about something in between?

reply The Anh Tran <trtheanh gmail.com> writes:
   According to D spec,
     structs, unions are value types.
     classes are ref types.

   But this world is not perfectly described by the above 2. There are 4 
cases:

   1. Value storage class inside value type.
       class C {};
       struct S {};

       struct Outer
       {
          C cc; // ref storage for ref type. This is the rescue for 
splitting copy problem. D language's creators wanted this behavior. If
          S ss; // value storage for value type. OK.

          void funcC(C c) {} // ref param. Correction for splitting copy.
          void funcS(S s) {} // value param. OK.
       }

   2. Value storage class inside ref type.
       class Outer
       {
          C cc; // ref storage. Correction for splitting copy.
          S ss; // value storage. OK.

          void funcC(C c) {} // ref param. Correction for splitting copy.
          void funcS(S s) {} // value param. OK.
       }

   3. Ref storage class inside a ref type.
       class Outer
       {
          C cc; // ref storage for ref type. OK.
          S ss; // How? Wanted ref, but value storage here

          void funcC(C c) {} // ref param. OK.
          void funcS(ref S s) {} // ref param for value type. OK.
       }

   4. Ref storage class inside value type.
       struct Outer
       {
          C c; // ref storage for ref type. OK.
          S s; // How? Wanted ref, but value storage here

          void funcC(C c) {} // ref param. OK.
          void funcS(ref S s) {} // ref param for value type. OK.
       }

   I can solve case 3+4 by wrapping struct S inside a class template. 
But can we have a nicer / straightforward syntax?
   Is there any problem that prevent ref storage class declaration for 
struct types, sir?
Jul 04 2009
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
The Anh Tran escribió:
   According to D spec,
     structs, unions are value types.
     classes are ref types.
 
   But this world is not perfectly described by the above 2. There are 4 
 cases:
 
   1. Value storage class inside value type.
       class C {};
       struct S {};
 
       struct Outer
       {
          C cc; // ref storage for ref type. This is the rescue for 
 splitting copy problem. D language's creators wanted this behavior. If
          S ss; // value storage for value type. OK.
 
          void funcC(C c) {} // ref param. Correction for splitting copy.
          void funcS(S s) {} // value param. OK.
       }
 
   2. Value storage class inside ref type.
       class Outer
       {
          C cc; // ref storage. Correction for splitting copy.
          S ss; // value storage. OK.
 
          void funcC(C c) {} // ref param. Correction for splitting copy.
          void funcS(S s) {} // value param. OK.
       }
 
   3. Ref storage class inside a ref type.
       class Outer
       {
          C cc; // ref storage for ref type. OK.
          S ss; // How? Wanted ref, but value storage here
 
          void funcC(C c) {} // ref param. OK.
          void funcS(ref S s) {} // ref param for value type. OK.
       }
 
   4. Ref storage class inside value type.
       struct Outer
       {
          C c; // ref storage for ref type. OK.
          S s; // How? Wanted ref, but value storage here
 
          void funcC(C c) {} // ref param. OK.
          void funcS(ref S s) {} // ref param for value type. OK.
       }
 
   I can solve case 3+4 by wrapping struct S inside a class template. But 
 can we have a nicer / straightforward syntax?
   Is there any problem that prevent ref storage class declaration for 
 struct types, sir?

Isn't pointers for that? 3. class Outer { S* ss; // Wanted ref, got ref. } 4. structOuter { S* s; // Wanted ref, got ref. } Best regards, Ary
Jul 04 2009
parent The Anh Tran <trtheanh gmail.com> writes:
 Ary Borenszweig wrote:
 Isn't pointers for that?
 3.
 class Outer {
   S* ss; // Wanted ref, got ref.
 }
 
 4.
 structOuter {
   S* s; // Wanted ref, got ref.
 }
 
 Best regards,
 Ary

Thanks. But in SafeD, no pointer is allowed. (Not a real issue right now.) This incomplete ref storage type just bug me. I would like to have something that 'complete' + nice.
Jul 06 2009