www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Overload issue between char[] and char*, inwhich the postfix should solve

reply David L. Davis <SpottedTiger yahoo.com> writes:
Hi Walter,

Please note the comments were placed in the source code below, to
point out the believed to be bug.

// OverLord.d
private import std.stdio;
alias std.stdio io;

int main()
{
    char[] sd = "char[] dynamic var works!";
    char * sp = "char * dynamic var works!\0";
    printValue(sd);  // Works fine.
    printValue(sp);  // Works fine.

    //uses the cast(char[])
    printValue(cast(char[])"cast(char[]) literal works!");

    //uses the cast(char *)
    printValue(cast(char *)"cast(char *) literal works!\0");

    // ** Bug **
    // Now a problem without a cast(), but when using the
    // 'c' postfix, it should make it clear that it's a char[]
    // parameter. There shouldn't be a matching error here.
    // ------------------------------------------------------------
    // The error parameter matchs both char[] and char*
    // overlord.d(19): function overlord.printValue called
    //    with argument types: (char[56])
    // matches both:
    //    overlord.printValue(char[])
    // and:
    //    overlord.printValue(char*)
    printValue("char[] literal with postfix \'c\' should work, but
doesn\'t"c);

    // ** Not a Bug **
    // Problem without a cast(), but that's ok...
    // since a literal is not typed at this point,
    // thus it doesn't know if it is a char[], wchar[], or dchar[]
    //printValue("char[] literal, would never work");

    return 0;
}

void printValue( in char[] s )
{
    io.writefln("printValue(in char[]) called, value=%s", s);
}

void printValue( in char* s )
{
    io.printf("printValue(in char *) called, value=%.*s\n", &s);
}

Thanks,
   David L.
Nov 28 2006
parent reply Walter Bright <newshound digitalmars.com> writes:
David L. Davis wrote:
     // Now a problem without a cast(), but when using the
     // 'c' postfix, it should make it clear that it's a char[]
     // parameter.

Why? A 'c' postfix means it's a char[56], and a static array can be implicitly converted to both char* and char[], hence the ambiguity error.
Nov 30 2006
parent David L. Davis <SpottedTiger yahoo.com> writes:
== Quote from Walter Bright (newshound digitalmars.com)'s article
 David L. Davis wrote:
     // Now a problem without a cast(), but when using the
     // 'c' postfix, it should make it clear that it's a char[]
     // parameter.

be implicitly converted to both char* and char[], hence the ambiguity error.

Walter thanks for the reply. Yeah I know what you're saying here. But, I guess I was hoping you would see the value of not forcing a cast() everytime you pass a static array into a function that overloads both a char[], and a char * parameter. (Myself, it would be nice to have it default to one or other, and to only use a cast() to force the other). Anyway it works as designed and thus not a bug...inwhich I'll just have to live with the cast() statements then. (The cast statements are so ugly in code, as they are meant to be...as to discourage their use whenever possible.) Thanks again for all the hardwork you're been putting into D (you're a real super lean and mean, bug killing machine). :) I'm really looking forward to the D v1.0 release on the 1st of Jan 2007. David L.
Nov 30 2006