www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - if Condition expression can't have function?

reply " FrankLike" <1150015857 qq.com> writes:
Hi,everyone,
Here has a error after run: " main.exe 11 " or " main.exe 10 ":

module main;
import std.stdio,std.conv;

template find(T)
{
     size_t find(T[] Array,T Element)
	{
		size_t i;
         foreach(T ArrayElement; Array)
		{
             if(Element==ArrayElement)
			{
				return i;
			}	
			i++;
         }
     return -1;
     }
}

void main(string[] args)
{
     int[] stuff=[0,1,2,3,4,5,6,7,8,9,10];
	try{
		
      if(auto m =(stuff.find(to!int(args[1]))>=0))
     {
         writeln(stuff," contains ",args[1]);
     }
	else
	{
		writeln(stuff," not contains ",args[1]);
	}
}

the exe result: is  contains 11 ← Error

  but this is ok:

void main(string[] args)
{
     int[] stuff=[0,1,2,3,4,5,6,7,8,9,10];
	try{
		int i =stuff.find(to!int(args[1]));
		auto x = stuff.find(to!int(args[1]))>=0;
		writeln("typeof(x) is :",x);
     if (i>=0)
		
	//if(auto m =(stuff.find(to!int(args[1]))>=0))
     {
         writeln(stuff," contains ",args[1]);
     }
	else
	{
		writeln(stuff," not contains ",args[1]);
	}
}
Why?

Thank you.

Frank
Apr 25 2014
next sibling parent reply Matthias Walter via Digitalmars-d <digitalmars-d puremagic.com> writes:
On 04/25/2014 09:14 AM, FrankLike via Digitalmars-d wrote:
 Hi,everyone,
 Here has a error after run: " main.exe 11 " or " main.exe 10 ":
 template find(T)
 {
     size_t find(T[] Array,T Element)
     {
find returns a size_t being a nonnegativ number.
     return -1;
But here it returns -1. In your main code you check find() >= 0, so maybe the compiler simplifies it in some situations because a size_t value is always >= 0. Matthias
Apr 25 2014
parent reply Nick Treleaven <ntrel-public yahoo.co.uk> writes:
On 25/04/2014 08:29, Matthias Walter via Digitalmars-d wrote:
      size_t find(T[] Array,T Element)
     {
find returns a size_t being a nonnegativ number.
     return -1;
But here it returns -1.
For size_t and uint 'dmd -w' seems to allow returning literal -1, but with ubyte I get: Error: cannot implicitly convert expression (-1) of type int to ubyte Is this inconsistent or is there a reason for this?
Apr 25 2014
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 25 Apr 2014 07:52:42 -0400, Nick Treleaven  
<ntrel-public yahoo.co.uk> wrote:

 On 25/04/2014 08:29, Matthias Walter via Digitalmars-d wrote:
      size_t find(T[] Array,T Element)
     {
find returns a size_t being a nonnegativ number.
     return -1;
But here it returns -1.
For size_t and uint 'dmd -w' seems to allow returning literal -1, but with ubyte I get: Error: cannot implicitly convert expression (-1) of type int to ubyte Is this inconsistent or is there a reason for this?
It is inconsistent, but it's well known legacy based on C rules. See Integer promotion and arithmetic conversions here: http://dlang.org/type "Integer values cannot be implicitly converted to another type that cannot represent the integer bit pattern after integral promotion." -Steve
Apr 25 2014
parent " FrankLike" <1150015857 qq.com> writes:
Thank you,everyone.

I should use the 'int'.

I use the 'size_t',that lets the range changed by the 
platform(X86 or X64),but forgot 'size_t' is another 'uint'.

Thank you everyone again.

Frank.
Apr 25 2014
prev sibling parent "David Nadlinger" <code klickverbot.at> writes:
On Friday, 25 April 2014 at 07:14:48 UTC, FrankLike wrote:
 Hi,everyone,
 Here has a error after run: " main.exe 11 " or " main.exe 10 ":
 […]
 Why?
size_t (the return type of your find()) is always non-negative, hence the if condition is never false. In the future, you might want to consider posting questions such as this to the digitalmars.D.learn mailing list/forums. David
Apr 25 2014