Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
January 17, 2012 [Issue 7301] New: RegexMatch opCast!bool not working | ||||
---|---|---|---|---|
| ||||
http://d.puremagic.com/issues/show_bug.cgi?id=7301 Summary: RegexMatch opCast!bool not working Product: D Version: D2 Platform: x86_64 OS/Version: Linux Status: NEW Severity: normal Priority: P2 Component: Phobos AssignedTo: nobody@puremagic.com ReportedBy: jlquinn@optonline.net --- Comment #0 from Jerry Quinn <jlquinn@optonline.net> 2012-01-16 19:12:04 PST --- Ubuntu 11.10 x86_64, dmd 2.057 bug2.d contains: import std.regex; bool foo() { auto re = regex("test"); return match("test", re); } ~/dmd2/linux/bin32/dmd -c bug2.d bug2.d(4): Error: cannot implicitly convert expression (match("test",re)) of type RegexMatch!(string,ThompsonMatcher) to bool It works if I use match("test", re).empty -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
January 17, 2012 [Issue 7301] RegexMatch opCast!bool not working | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jerry Quinn | http://d.puremagic.com/issues/show_bug.cgi?id=7301 --- Comment #1 from Jerry Quinn <jlquinn@optonline.net> 2012-01-16 19:12:59 PST --- Actually I think this might be a DMD bug. !match() compiles too. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
January 17, 2012 [Issue 7301] RegexMatch opCast!bool not working | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jerry Quinn | http://d.puremagic.com/issues/show_bug.cgi?id=7301 timon.gehr@gmx.ch changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |timon.gehr@gmx.ch --- Comment #2 from timon.gehr@gmx.ch 2012-01-16 19:40:52 PST --- This is the same for basic types. bool foo(){return 2;} // error bool bar(){return !2;} // fine I don't think we are looking at a bug here. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
January 17, 2012 [Issue 7301] RegexMatch opCast!bool not working | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jerry Quinn | http://d.puremagic.com/issues/show_bug.cgi?id=7301 --- Comment #3 from Jerry Quinn <jlquinn@optonline.net> 2012-01-16 21:34:51 PST --- (In reply to comment #2) > This is the same for basic types. > > bool foo(){return 2;} // error > bool bar(){return !2;} // fine > > I don't think we are looking at a bug here. Are you saying that type conversion isn't supposed to happen when a value is returned? http://www.d-programming-language.org/statement.html#ReturnStatement says: "The Expression is implicitly converted to the function return type." -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
January 17, 2012 [Issue 7301] RegexMatch opCast!bool not working | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jerry Quinn | http://d.puremagic.com/issues/show_bug.cgi?id=7301 --- Comment #4 from timon.gehr@gmx.ch 2012-01-16 23:33:27 PST --- Neither int (outside the range [0,1]) nor RegexMatch implicitly convert to bool. Negating counts as an explicit cast to bool. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
January 17, 2012 [Issue 7301] RegexMatch opCast!bool not working | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jerry Quinn | http://d.puremagic.com/issues/show_bug.cgi?id=7301 --- Comment #5 from Jerry Quinn <jlquinn@optonline.net> 2012-01-17 06:42:58 PST --- (In reply to comment #4) > Neither int (outside the range [0,1]) nor RegexMatch implicitly convert to bool. Negating counts as an explicit cast to bool. As far as I can tell, RegexMatch does implicitly cast to bool. struct RegexMatch { T opCast(T : bool); } I could be missing something, but this effectively declares an opCast!bool operator. http://www.d-programming-language.org/operatoroverloading.html#Cast claims that both ! and bare references will get rewritten to opCast!bool: if (e) => if (e.opCast!(bool)) if (!e) => if (!e.opCast!(bool)) That should take care of the return, no? Note that the same compile failure happens if I write: auto re = regex("test"); if (match("test", re)) { ... } It strikes me that the language docs are rather sparse on defining what can and cannot be implicitly converted. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
January 17, 2012 [Issue 7301] RegexMatch opCast!bool not working | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jerry Quinn | http://d.puremagic.com/issues/show_bug.cgi?id=7301 --- Comment #6 from timon.gehr@gmx.ch 2012-01-17 10:19:29 PST --- (In reply to comment #5) > (In reply to comment #4) > > Neither int (outside the range [0,1]) nor RegexMatch implicitly convert to bool. Negating counts as an explicit cast to bool. > > As far as I can tell, RegexMatch does implicitly cast to bool. > > struct RegexMatch { > T opCast(T : bool); > } > > I could be missing something, but this effectively declares an opCast!bool operator. > opCast means explicit cast, as in cast(bool)regexMatch or !regexMatch or regexMatch && 2. > http://www.d-programming-language.org/operatoroverloading.html#Cast claims that both ! and bare references will get rewritten to opCast!bool: > > if (e) => if (e.opCast!(bool)) > if (!e) => if (!e.opCast!(bool)) > > That should take care of the return, no? That only works for conditions and arguments to boolean operators. If you think it should also work for return values where the enclosing function is declared to return bool, then that is an enhancement request. It might be a reasonable one. > > Note that the same compile failure happens if I write: > > auto re = regex("test"); > if (match("test", re)) { ... } > Works for me. > > It strikes me that the language docs are rather sparse on defining what can and cannot be implicitly converted. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
January 17, 2012 [Issue 7301] RegexMatch opCast!bool not working | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jerry Quinn | http://d.puremagic.com/issues/show_bug.cgi?id=7301 --- Comment #7 from Jerry Quinn <jlquinn@optonline.net> 2012-01-17 12:57:07 PST --- (In reply to comment #6) > (In reply to comment #5) > > (In reply to comment #4) > > > Neither int (outside the range [0,1]) nor RegexMatch implicitly convert to bool. Negating counts as an explicit cast to bool. > > > opCast means explicit cast, as in cast(bool)regexMatch or !regexMatch or > regexMatch && 2. I think this is the crux of what we're arguing about. To me, explicit casting happens when you write cast(T) in the code. When the compiler generates a cast for you, that's implicit casting. Is this not correct? It seems to me that opCast is pretty useless unless the compiler can decide to use it to convert an object for you. > > http://www.d-programming-language.org/operatoroverloading.html#Cast claims that both ! and bare references will get rewritten to opCast!bool: > > > > if (e) => if (e.opCast!(bool)) > > if (!e) => if (!e.opCast!(bool)) > > > > That should take care of the return, no? > > That only works for conditions and arguments to boolean operators. If you think it should also work for return values where the enclosing function is declared to return bool, then that is an enhancement request. It might be a reasonable one. Why should it only work in those cases? I can't find anything in the language docs that says this. The docs on functions say that when a type is passed in, implicit conversion is used as one of the match possibilities. That's opCast, no? So if parameter passing is using opCast for implicit conversion, the return statement should be too. Hence my labeling this as a bug. > > Note that the same compile failure happens if I write: > > > > auto re = regex("test"); > > if (match("test", re)) { ... } > > > > Works for me. My mistake - this case does work. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
January 17, 2012 [Issue 7301] RegexMatch opCast!bool not working | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jerry Quinn | http://d.puremagic.com/issues/show_bug.cgi?id=7301 --- Comment #8 from timon.gehr@gmx.ch 2012-01-17 13:13:19 PST --- (In reply to comment #7) > (In reply to comment #6) > > (In reply to comment #5) > > > (In reply to comment #4) > > > > Neither int (outside the range [0,1]) nor RegexMatch implicitly convert to bool. Negating counts as an explicit cast to bool. > > > > > opCast means explicit cast, as in cast(bool)regexMatch or !regexMatch or > > regexMatch && 2. > > I think this is the crux of what we're arguing about. To me, explicit casting happens when you write cast(T) in the code. When the compiler generates a cast for you, that's implicit casting. > > Is this not correct? In D, cast(T) is an explicit cast as is using an object in a boolean context such as if(...) !... ...||... ...&&... etc. Those are presumably the cases referred to on the Operator Overloading page. > > It seems to me that opCast is pretty useless unless the compiler can decide to use it to convert an object for you. > It cannot, except in the cases where it inserts cast(bool). > > > http://www.d-programming-language.org/operatoroverloading.html#Cast claims that both ! and bare references will get rewritten to opCast!bool: > > > > > > if (e) => if (e.opCast!(bool)) > > > if (!e) => if (!e.opCast!(bool)) > > > > > > That should take care of the return, no? > > > > That only works for conditions and arguments to boolean operators. If you think it should also work for return values where the enclosing function is declared to return bool, then that is an enhancement request. It might be a reasonable one. > > Why should it only work in those cases? It is about disabling some narrowing implicit conversions to bool while still allowing them were it really makes sense. bool b = 2; // error, int does not implicitly convert to bool if(2){...} // fine, because explicit cast(bool)2 works. > I can't find anything in the language docs that says this. > The docs on functions say that when a type is passed in, > implicit conversion is used as one of the match possibilities. That's opCast, > no? No. opCast cannot be used for implicit conversions. At some point there was even the idea of introducing opImplicitCast. > So if parameter passing is using opCast for implicit conversion, the return statement should be too. Hence my labeling this as a bug. > Parameter passing does _not_ use opCast for implicit conversions. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
Copyright © 1999-2021 by the D Language Foundation