www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - UFCS and error messages

reply "Andrea Fontana" <nospam example.com> writes:
Check this simple code:

import std.stdio;
import std.conv;

bool is_zero(T)(T i) { return to!int(i) == 0; }

void main() { "0".is_zero.writeln; }

This code print "true" of course.

If you replace "to!int(i) == 0" with "i == 0" compiler gives this 
error:

"Error: no property 'is_zero' for type 'string'"

But:

is_zero("0")

instead gives this:

Error: incompatible types for ((i) == (0)): 'string' and 'int'

Shoudn't "0".is_zero give this error too?
Oct 31 2013
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Andrea Fontana:

 Shoudn't "0".is_zero give this error too?
Possibly yes. I put this enhancement request in Bugzilla few months ago, but I don't remember its number now. Bye, bearophile
Oct 31 2013
prev sibling parent Marco Leise <Marco.Leise gmx.de> writes:
Am Thu, 31 Oct 2013 11:58:18 +0100
schrieb "Andrea Fontana" <nospam example.com>:

 Check this simple code:
 
 import std.stdio;
 import std.conv;
 
 bool is_zero(T)(T i) { return to!int(i) == 0; }
 
 void main() { "0".is_zero.writeln; }
 
 This code print "true" of course.
 
 If you replace "to!int(i) == 0" with "i == 0" compiler gives this 
 error:
 
 "Error: no property 'is_zero' for type 'string'"
 
 But:
 
 is_zero("0")
 
 instead gives this:
 
 Error: incompatible types for ((i) == (0)): 'string' and 'int'
 
 Shoudn't "0".is_zero give this error too?
If you see it as a universal function call, yes. But if you see it as an augmented property of string, then no. -- Marco
Oct 31 2013