digitalmars.D - if Condition expression can't have function?
- FrankLike (57/57) Apr 25 2014 Hi,everyone,
- Matthias Walter via Digitalmars-d (6/13) Apr 25 2014 But here it returns -1.
- Nick Treleaven (5/10) Apr 25 2014 For size_t and uint 'dmd -w' seems to allow returning literal -1, but
- Steven Schveighoffer (8/19) Apr 25 2014 It is inconsistent, but it's well known legacy based on C rules.
- FrankLike (6/6) Apr 25 2014 Thank you,everyone.
- David Nadlinger (6/10) Apr 25 2014 size_t (the return type of your find()) is always non-negative,
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
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
On 25/04/2014 08:29, Matthias Walter via Digitalmars-d wrote: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?size_t find(T[] Array,T Element)find returns a size_t being a nonnegativ number.{But here it returns -1.return -1;
Apr 25 2014
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: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." -SteveFor 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?size_t find(T[] Array,T Element)find returns a size_t being a nonnegativ number.{But here it returns -1.return -1;
Apr 25 2014
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
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