www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Using typedefed types as covariant return types

reply Rick Mann <rmann-d-lang latencyzero.com> writes:
I didn't get any responses in D.learn for this question, so I decided maybe it
was advanced enough for this group.

I was hoping I could do this:

typedef void* CFTypeRef;
typedef CFTypeRef CFStringRef;

class DCFObject
{
  CFTypeRef getRef() {}
}

class DString : DCFObject
{
  CFStringRef getRef() {}
}

But the compiler gives me:

src/d/darbon/corefoundation/DString.d:32: function
darbon.corefoundation.DString.DString.getRef of type CFStringRef() overrides
but is not covariant with darbon.corefoundation.DCFObject.DCFObject.getRef of
type CFTypeRef()

From what I gather in the docs, CFStringRef is derived from CFTypeRef, so this
should be acceptable. Am I missing something?

TIA,
Rick
Feb 07 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Rick Mann wrote:
 I didn't get any responses in D.learn for this question, so I decided maybe it
was advanced enough for this group.
 
 I was hoping I could do this:
 
 typedef void* CFTypeRef;
 typedef CFTypeRef CFStringRef;
 
 class DCFObject
 {
   CFTypeRef getRef() {}
 }
 
 class DString : DCFObject
 {
   CFStringRef getRef() {}
 }
 
 But the compiler gives me:
 
 src/d/darbon/corefoundation/DString.d:32: function
darbon.corefoundation.DString.DString.getRef of type CFStringRef() overrides
but is not covariant with darbon.corefoundation.DCFObject.DCFObject.getRef of
type CFTypeRef()
 
 From what I gather in the docs, CFStringRef is derived from CFTypeRef, so this
should be acceptable. Am I missing something?

That's not the message I get from reading the docs. What I see: "Strong types are semantically a distinct type to the type checking system, for function overloading, and for the debugger." (http://www.digitalmars.com/d/declaration.html) Basically CFStringRef becomes something completely new and different from CTypeRef as far as the compiler is concerned. What are you reading that makes you think CFStringRef is "derived" from CFTypeRef? --bb
Feb 07 2007
parent Stewart Gordon <smjg iname.com> writes:
Bill Baxter Wrote:

 Rick Mann wrote:
 I didn't get any responses in D.learn for this question, so I 
 decided maybe it was advanced enough for this group.
 
 I was hoping I could do this:
 
 typedef void* CFTypeRef;
 typedef CFTypeRef CFStringRef;


 From what I gather in the docs, CFStringRef is derived from 
 CFTypeRef, so this should be acceptable.  Am I missing something?

That's not the message I get from reading the docs. What I see: "Strong types are semantically a distinct type to the type checking system, for function overloading, and for the debugger." (http://www.digitalmars.com/d/declaration.html)

Are you taking this to mean distinct from each other or distinct from their underlying types? If the latter, this is much the same a class being different from its base class.
 Basically CFStringRef becomes something completely new and 
 different from CTypeRef as far as the compiler is concerned.

What are you reading that makes you think it's _completely_ different? This compiles: ---------- typedef void* CFTypeRef; typedef CFTypeRef CFStringRef; void func(CFTypeRef p) {} void main() { CFStringRef s; func(s); } ----------
 What are you reading that makes you think CFStringRef is "derived" 
 from CFTypeRef?

Probably that typedefs implicitly convert to their underlying types. Moreover, typedefs behave in a number of respects very similarly to enums, for which the notation enum Enum : int suggests type derivation as with classes. Stewart.
Feb 08 2007