digitalmars.D - just an idea (!! operator)
- akaz (8/8) Jul 11 2012 if needed, the operator !! (double exclamation mark) could be
- =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= (7/14) Jul 11 2012 This is not something important enough to warrant a language feature in
- Timon Gehr (2/16) Jul 11 2012 Making the operator overloadable does not result in a Phobos dependency.
- =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= (8/26) Jul 11 2012 Yes, I know.
- David Piepgrass (2/6) Jul 11 2012 Actually, we could! Great idea, nimrod! (inside joke)
- Araq (2/8) Jul 12 2012 ML, Haskell, Clean, Prolog, Mercury, Scala, F# ...
- monarch_dodra (36/38) Jul 11 2012 Problem is that operator"!!" is already used asa twin
- Don Clugston (2/8) Jul 11 2012 This is about binary x!!y, not unary !!x.
- deadalnix (2/4) Jul 11 2012 So why do you do a proposal if this is not needed ?
- akaz (6/8) Jul 11 2012 I did not ask for it, I only reminded that it is possible to
- Jonas Drewsen (6/9) Jul 12 2012 Or the operator?? could be borrowed from c#
- travert phare.normalesup.org (Christophe Travert) (4/17) Jul 12 2012 or maybe:
- travert phare.normalesup.org (Christophe Travert) (5/22) Jul 12 2012 I forgot to mention that foo would be evaluated only once (and the
- Jacob Carlborg (6/11) Jul 12 2012 I really like that operator. The existential operator, also known as the...
- Roman D. Boiko (2/16) Jul 12 2012 +1
- travert phare.normalesup.org (Christophe Travert) (16/35) Jul 12 2012 Sweet.
- Daniel Murphy (5/9) Jul 12 2012 Yeah, I've been planning to try and get this into D one day. Probably
- Andrei Alexandrescu (3/17) Jul 12 2012 gcc used to have that extension and they dropped it...
- =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= (10/28) Jul 12 2012 But on the other hand, C# has had it from day one and it's still widely
- Roman D. Boiko (4/6) Jul 12 2012 It has been introduced in C# 2.0 and quickly gained high
- deadalnix (2/20) Jul 12 2012 Do you know why ? It have been useful to me in languages that support it...
- Andrei Alexandrescu (3/14) Jul 12 2012 I apparently misspoke. Here's the search: http://goo.gl/zjKiJ
- Timon Gehr (2/17) Jul 12 2012 You might have had in mind the and >? operators.
- Jonathan M Davis (8/9) Jul 12 2012 My guess would be that it didn't get used much precisely because it was ...
- David Piepgrass (6/11) Jul 12 2012 But GCC can't control the C++ language spec. Naturally there is a
- Andrei Alexandrescu (5/16) Jul 12 2012 I think we can add coalesce() with any number of lazy arguments that
- Jonas Drewsen (12/34) Jul 13 2012 I Agree. Looking at the Groovy link from Christophe I noticed
- monarch_dodra (14/14) Jul 13 2012 I don't know much about C#, but in C#, isn't EVERYTHING a
- Jonas Drewsen (5/19) Jul 13 2012 Regarding the ?: operator I agree with Andrei that is should be
- travert phare.normalesup.org (Christophe Travert) (6/7) Jul 13 2012 ? could be the begining of a ternary operator, and . the module scope
- deadalnix (2/20) Jul 13 2012 Due to lazy being broken, this cannot be @safe, pure or nothrow.
- David Piepgrass (6/9) Jul 13 2012 No, C# has value types (enums, primitives, and user-defined
- Jacob Carlborg (13/19) Jul 13 2012 But what would the static type of "a" be? I tried this:
- David Nadlinger (8/13) Jul 13 2012 I don't think there is much doubt in this regard: the type that
- Roman D. Boiko (3/6) Jul 13 2012 It might mean identity (return the argument unchanged) for value
- travert phare.normalesup.org (Christophe Travert) (10/18) Jul 13 2012 It might mean: give me the default I provide as an extra argument:
- deadalnix (2/13) Jul 12 2012 Well C# does use ?? but ?: seems to me used in more languages.
- travert phare.normalesup.org (Christophe Travert) (7/21) Jul 13 2012 Because ?: is the ternary conditionnal operator with missing
- Jacob Carlborg (6/9) Jul 12 2012 We need to combine it with the assignment operator as well:
if needed, the operator !! (double exclamation mark) could be defined. it is just an idea, i do not have any specific use in mind. i encountered the operator in RT operating systems book: c!!e sends the message e along channel c c?x assigns to variable x the value from c maybe this could be integrated with the concurrency somehow or used in some other area.
Jul 11 2012
On 11-07-2012 13:18, akaz wrote:if needed, the operator !! (double exclamation mark) could be defined. it is just an idea, i do not have any specific use in mind. i encountered the operator in RT operating systems book: c!!e sends the message e along channel c c?x assigns to variable x the value from c maybe this could be integrated with the concurrency somehow or used in some other area.This is not something important enough to warrant a language feature in my opinion. And certainly not one that results in a Phobos dependency. -- Alex Rønne Petersen alex lycus.org http://lycus.org
Jul 11 2012
On 07/11/2012 01:33 PM, Alex Rønne Petersen wrote:On 11-07-2012 13:18, akaz wrote:Making the operator overloadable does not result in a Phobos dependency.if needed, the operator !! (double exclamation mark) could be defined. it is just an idea, i do not have any specific use in mind. i encountered the operator in RT operating systems book: c!!e sends the message e along channel c c?x assigns to variable x the value from c maybe this could be integrated with the concurrency somehow or used in some other area.This is not something important enough to warrant a language feature in my opinion. And certainly not one that results in a Phobos dependency.
Jul 11 2012
On 11-07-2012 15:42, Timon Gehr wrote:On 07/11/2012 01:33 PM, Alex Rønne Petersen wrote:Yes, I know. But we can't base the decision solely on this fact. Then we could add a million operators to the language just because they seem neat. -- Alex Rønne Petersen alex lycus.org http://lycus.orgOn 11-07-2012 13:18, akaz wrote:Making the operator overloadable does not result in a Phobos dependency.if needed, the operator !! (double exclamation mark) could be defined. it is just an idea, i do not have any specific use in mind. i encountered the operator in RT operating systems book: c!!e sends the message e along channel c c?x assigns to variable x the value from c maybe this could be integrated with the concurrency somehow or used in some other area.This is not something important enough to warrant a language feature in my opinion. And certainly not one that results in a Phobos dependency.
Jul 11 2012
...it is just an idea, i do not have any specific use in mind.But we can't base the decision solely on this fact. Then we could add a million operators to the language just because they seem neat.Actually, we could! Great idea, nimrod! (inside joke)
Jul 11 2012
On Wednesday, 11 July 2012 at 18:21:33 UTC, David Piepgrass wrote:...it is just an idea, i do not have any specific use in mind.But we can't base the decision solely on this fact. Then we could add a million operators to the language just because they seem neat.Actually, we could! Great idea, nimrod! (inside joke)
Jul 12 2012
On Wednesday, 11 July 2012 at 11:18:21 UTC, akaz wrote:if needed, the operator !! (double exclamation mark) could be defined.Problem is that operator"!!" is already used asa twin operator"!". This is shorthand for "is valid as bool": When a type can be casted to bool, it is quicker to write "!!val" than "cast(bool)val". This is only moderately useful, as 90% of the time, the cast occurs in a if/while/for, where implicit casts to bool are legal, but still: ---- import std.stdio; struct S { int v; bool opCast() {return cast(bool)v;} } void foo(bool b){} void main() { S s = S(5); //bool b = s; //Error: cannot implicitly convert expression (s) of type S to bool bool b = !!s; //This is valid though, and shorter than //bool b = cast(bool)s; if(s) //But it works inside a if anyways ... foo(!!s); //Call foo with s as boolean } ---- I've seen this used a lot in c++. explicit casts did not exist prior to c++11. To allow casting to bool while avoiding the dangers of implicit casts, one design patter was to define "only" operator"!", and use !!val as an alternative to casting to bool. After you've seen it a few times, it feels natural, as if it was an operator of itself. I wouldn't be surprised if this is happening in D, so I don't think "!!" can be taken for anything.
Jul 11 2012
On 11/07/12 13:47, monarch_dodra wrote:On Wednesday, 11 July 2012 at 11:18:21 UTC, akaz wrote:if needed, the operator !! (double exclamation mark) could be defined.Problem is that operator"!!" is already used asa twin operator"!". This is shorthand for "is valid as bool":I wouldn't be surprised if this is happening in D, so I don't think "!!" can be taken for anything.This is about binary x!!y, not unary !!x.
Jul 11 2012
On 11/07/2012 13:18, akaz wrote:if needed, the operator !! (double exclamation mark) could be defined. it is just an idea, i do not have any specific use in mind.So why do you do a proposal if this is not needed ?
Jul 11 2012
On Wednesday, 11 July 2012 at 14:08:55 UTC, deadalnix wrote:On 11/07/2012 13:18, akaz wrote: So why do you do a proposal if this is not needed ?I did not ask for it, I only reminded that it is possible to define it "if needed". It's merely a suggestion. I was reading a book and told myself: "look, a nice operator! maybe could be useful if the D community knows about it!". Do you really see in my original post a request for defining it?
Jul 11 2012
On Wednesday, 11 July 2012 at 11:18:21 UTC, akaz wrote:if needed, the operator !! (double exclamation mark) could be defined. ...auto a = foo ?? new Foo(); is short for: auto a = foo is null ? new Foo() : foo; /Jonas
Jul 12 2012
"Jonas Drewsen" , dans le message (digitalmars.D:172039), a écrit :On Wednesday, 11 July 2012 at 11:18:21 UTC, akaz wrote:or maybe: auto a = ! ! foo ? foo : new Foo(); || could be redifined to have this a behavior, but it would break code.if needed, the operator !! (double exclamation mark) could be defined. ...auto a = foo ?? new Foo(); is short for: auto a = foo is null ? new Foo() : foo;
Jul 12 2012
Christophe Travert, dans le message (digitalmars.D:172047), a écrit :"Jonas Drewsen" , dans le message (digitalmars.D:172039), a écrit :I forgot to mention that foo would be evaluated only once (and the second operand would be evaluated lazily). This is the main point of this syntax, and it is not easily emulable (as long a lazy is not fixed).On Wednesday, 11 July 2012 at 11:18:21 UTC, akaz wrote:or maybe: auto a = ! ! foo ? foo : new Foo();if needed, the operator !! (double exclamation mark) could be defined. ...auto a = foo ?? new Foo(); is short for: auto a = foo is null ? new Foo() : foo;
Jul 12 2012
On 2012-07-12 13:35, Jonas Drewsen wrote:auto a = foo ?? new Foo(); is short for: auto a = foo is null ? new Foo() : foo; /JonasI really like that operator. The existential operator, also known as the Elvis operator :) . It's available in many languages with slightly different semantics. -- /Jacob Carlborg
Jul 12 2012
On Thursday, 12 July 2012 at 12:51:38 UTC, Jacob Carlborg wrote:On 2012-07-12 13:35, Jonas Drewsen wrote:+1auto a = foo ?? new Foo(); is short for: auto a = foo is null ? new Foo() : foo; /JonasI really like that operator. The existential operator, also known as the Elvis operator :) . It's available in many languages with slightly different semantics.
Jul 12 2012
Jacob Carlborg , dans le message (digitalmars.D:172056), a écrit :On 2012-07-12 13:35, Jonas Drewsen wrote:Sweet. | Elvis Operator (?: ) | | The "Elvis operator" is a shortening of Java's ternary operator. One | instance of where this is handy is for returning a 'sensible default' | value if an expression resolves to false or null. A simple example | might look like this: | | def displayName = user.name ? user.name : "Anonymous" //traditional | ternary operator usage | | def displayName = user.name ?: "Anonymous" // more compact Elvis | operator - does same as above (taken from http://groovy.codehaus.org/Operators#Operators-ElvisOperator)auto a = foo ?? new Foo(); is short for: auto a = foo is null ? new Foo() : foo; /JonasI really like that operator. The existential operator, also known as the Elvis operator :) . It's available in many languages with slightly different semantics. -- /Jacob Carlborg
Jul 12 2012
"Jonas Drewsen" <jdrewsen nospam.com> wrote in message news:zwtvliaunccmtwmabxfz forum.dlang.org...auto a = foo ?? new Foo(); is short for: auto a = foo is null ? new Foo() : foo; /JonasYeah, I've been planning to try and get this into D one day. Probably something like: (a ?: b) -> (auto __tmp = a, __tmp ? __tmp : b)
Jul 12 2012
On 7/12/12 12:24 PM, Daniel Murphy wrote:"Jonas Drewsen"<jdrewsen nospam.com> wrote in message news:zwtvliaunccmtwmabxfz forum.dlang.org...gcc used to have that extension and they dropped it... Andreiauto a = foo ?? new Foo(); is short for: auto a = foo is null ? new Foo() : foo; /JonasYeah, I've been planning to try and get this into D one day. Probably something like: (a ?: b) -> (auto __tmp = a, __tmp ? __tmp : b)
Jul 12 2012
On 12-07-2012 19:32, Andrei Alexandrescu wrote:On 7/12/12 12:24 PM, Daniel Murphy wrote:used and encouraged today: http://msdn.microsoft.com/en-us/library/ms173224(v=vs.110).aspx I find it to be a useful little feature when I have to deal with possibly-null values. -- Alex Rønne Petersen alex lycus.org http://lycus.org"Jonas Drewsen"<jdrewsen nospam.com> wrote in message news:zwtvliaunccmtwmabxfz forum.dlang.org...gcc used to have that extension and they dropped it... Andreiauto a = foo ?? new Foo(); is short for: auto a = foo is null ? new Foo() : foo; /JonasYeah, I've been planning to try and get this into D one day. Probably something like: (a ?: b) -> (auto __tmp = a, __tmp ? __tmp : b)
Jul 12 2012
On Thursday, 12 July 2012 at 17:35:05 UTC, Alex Rønne Petersen wrote:still widely used and encouraged today:popularity.
Jul 12 2012
On 12/07/2012 19:32, Andrei Alexandrescu wrote:On 7/12/12 12:24 PM, Daniel Murphy wrote:Do you know why ? It have been useful to me in languages that support it."Jonas Drewsen"<jdrewsen nospam.com> wrote in message news:zwtvliaunccmtwmabxfz forum.dlang.org...gcc used to have that extension and they dropped it... Andreiauto a = foo ?? new Foo(); is short for: auto a = foo is null ? new Foo() : foo; /JonasYeah, I've been planning to try and get this into D one day. Probably something like: (a ?: b) -> (auto __tmp = a, __tmp ? __tmp : b)
Jul 12 2012
On 7/12/12 2:37 PM, deadalnix wrote:On 12/07/2012 19:32, Andrei Alexandrescu wrote:I apparently misspoke. Here's the search: http://goo.gl/zjKiJ AndreiOn 7/12/12 12:24 PM, Daniel Murphy wrote:Do you know why ? It have been useful to me in languages that support it.Yeah, I've been planning to try and get this into D one day. Probably something like: (a ?: b) -> (auto __tmp = a, __tmp ? __tmp : b)gcc used to have that extension and they dropped it... Andrei
Jul 12 2012
On 07/12/2012 09:03 PM, Andrei Alexandrescu wrote:On 7/12/12 2:37 PM, deadalnix wrote:You might have had in mind the <? and >? operators.On 12/07/2012 19:32, Andrei Alexandrescu wrote:I apparently misspoke. Here's the search: http://goo.gl/zjKiJ AndreiOn 7/12/12 12:24 PM, Daniel Murphy wrote:Do you know why ? It have been useful to me in languages that support it.Yeah, I've been planning to try and get this into D one day. Probably something like: (a ?: b) -> (auto __tmp = a, __tmp ? __tmp : b)gcc used to have that extension and they dropped it... Andrei
Jul 12 2012
On Thursday, July 12, 2012 20:37:14 deadalnix wrote:Do you know why ? It have been useful to me in languages that support it.My guess would be that it didn't get used much precisely because it was an extension. Most programmers don't use language extensions. They probably don't even know that they exist in most cases, and they're often a bad idea to use, because they aren't cross platform. But if it's built into the language (as it comfortable with using it, so it gets used a lot more. - Jonathan M Davis
Jul 12 2012
But GCC can't control the C++ language spec. Naturally there is a reluctance to add nonstandard features. It's a successful feature on object references that might be null.) I don't see why you would use ?: instead of ??, though.Yeah, I've been planning to try and get this into D one day. Probably something like: (a ?: b) -> (auto __tmp = a, __tmp ? __tmp : b)gcc used to have that extension and they dropped it...
Jul 12 2012
On 7/12/12 4:49 PM, David Piepgrass wrote:I think we can add coalesce() with any number of lazy arguments that returns the leftmost argument that is nonzero. It's a useful function but not frequent enough to warrant an operator. AndreiBut GCC can't control the C++ language spec. Naturally there is a however, and a lot of people (including me) have also been pestering the that might be null.) I don't see why you would use ?: instead of ??, though.Yeah, I've been planning to try and get this into D one day. Probably something like: (a ?: b) -> (auto __tmp = a, __tmp ? __tmp : b)gcc used to have that extension and they dropped it...
Jul 12 2012
On Thursday, 12 July 2012 at 22:36:19 UTC, Andrei Alexandrescu wrote:On 7/12/12 4:49 PM, David Piepgrass wrote:I Agree. Looking at the Groovy link from Christophe I noticed their Safe Navigation Operator ?. Basicly it returns the final value in a dereference chain or null if any objects in the chain are null: // a is null if user or address or street is null. No exception thrown. auto a = user?.address?.street; This is a very nice operator that would be tricky to implement as a function or template. /JonasI think we can add coalesce() with any number of lazy arguments that returns the leftmost argument that is nonzero. It's a useful function but not frequent enough to warrant an operator.But GCC can't control the C++ language spec. Naturally there is a reluctance to add nonstandard features. It's a successful however, and a lot of people (including me) have also been pestering the references that might be null.) I don't see why you would use ?: instead of ??, though.Yeah, I've been planning to try and get this into D one day. Probably something like: (a ?: b) -> (auto __tmp = a, __tmp ? __tmp : b)gcc used to have that extension and they dropped it...
Jul 13 2012
reference type? Meaning it always makes sense to check if "myobject is null". I'm still no expert in D either, but what would(should) happen if you tried to call ?: or ?. on a value type? Writing "s is null" gives "Error: incompatible types for ((s) is (null)): 'S' and 'typeof(null)'" I'd guess that: *"s ?: S(5)" would give a compile error, since it the call makes no sense? *"s?.someFunction" would simply resolve as "s.somFunction"? - This could avoid problems in templates that want to use ?. The operators *look* convenient, but isn't there a risk of ambiguity for D? But again, I'm not expert in either languages.
Jul 13 2012
On Friday, 13 July 2012 at 09:49:22 UTC, monarch_dodra wrote:reference type? Meaning it always makes sense to check if "myobject is null". I'm still no expert in D either, but what would(should) happen if you tried to call ?: or ?. on a value type? Writing "s is null" gives "Error: incompatible types for ((s) is (null)): 'S' and 'typeof(null)'" I'd guess that: *"s ?: S(5)" would give a compile error, since it the call makes no sense? *"s?.someFunction" would simply resolve as "s.somFunction"? - This could avoid problems in templates that want to use ?. The operators *look* convenient, but isn't there a risk of ambiguity for D? But again, I'm not expert in either languages.Regarding the ?: operator I agree with Andrei that is should be handled by a coalesce() function instead. Can you identify any ambiguity with an ?. operator. /Jonas
Jul 13 2012
"Jonas Drewsen" , dans le message (digitalmars.D:172242), a écrit :Can you identify any ambiguity with an ?. operator.? could be the begining of a ternary operator, and . the module scope indicator, or the beginning of a (badly) written float number. Both case can be disambiguated by the presence of the ':' in the case of a ternary operator. I don't think '?' has currently any other meaning in D.
Jul 13 2012
On 13/07/2012 13:31, Jonas Drewsen wrote:On Friday, 13 July 2012 at 09:49:22 UTC, monarch_dodra wrote:Due to lazy being broken, this cannot be safe, pure or nothrow.type? Meaning it always makes sense to check if "myobject is null". I'm still no expert in D either, but what would(should) happen if you tried to call ?: or ?. on a value type? Writing "s is null" gives "Error: incompatible types for ((s) is (null)): 'S' and 'typeof(null)'" I'd guess that: *"s ?: S(5)" would give a compile error, since it the call makes no sense? *"s?.someFunction" would simply resolve as "s.somFunction"? - This could avoid problems in templates that want to use ?. The operators *look* convenient, but isn't there a risk of ambiguity for D? But again, I'm not expert in either languages.Regarding the ?: operator I agree with Andrei that is should be handled by a coalesce() function instead.
Jul 13 2012
On Friday, 13 July 2012 at 09:49:22 UTC, monarch_dodra wrote:reference type? Meaning it always makes sense to check if "myobject is null".types) which are not nullable. The null coalescing operator (and null?.dot, if it existed) is still useful for nullable types of course; plus, any value type has a nullable counterpart (e.g. int? = nullable int).
Jul 13 2012
On 2012-07-13 11:24, Jonas Drewsen wrote:I Agree. Looking at the Groovy link from Christophe I noticed their Safe Navigation Operator ?. Basicly it returns the final value in a dereference chain or null if any objects in the chain are null: // a is null if user or address or street is null. No exception thrown. auto a = user?.address?.street;But what would the static type of "a" be? I tried this: class Foo {} void main () { auto a = true ? new Foo : new Object; static assert (is(typeof(a) == Object)); } And it passes. You loose static typing. How would the type of "a" be resolved if there's a field/method that returns a value type in the chain? Compile time error? -- /Jacob Carlborg
Jul 13 2012
On Friday, 13 July 2012 at 12:51:07 UTC, Jacob Carlborg wrote:On 2012-07-13 11:24, Jonas Drewsen wrote:I don't think there is much doubt in this regard: the type that you get when none of the »intermediates« evaluate to null, i.e. typeof(street). Anything else doesn't make much sense in my regard. I guess that this operator is only really worth it in languages where every type is nullable, though. David// a is null if user or address or street is null. No exception thrown. auto a = user?.address?.street;But what would the static type of "a" be?
Jul 13 2012
On Friday, 13 July 2012 at 13:46:10 UTC, David Nadlinger wrote:I guess that this operator is only really worth it in languages where every type is nullable, though. DavidIt might mean identity (return the argument unchanged) for value types.
Jul 13 2012
"Roman D. Boiko" , dans le message (digitalmars.D:172259), a écrit :On Friday, 13 July 2012 at 13:46:10 UTC, David Nadlinger wrote:It might mean: give me the default I provide as an extra argument: Example: car?.driver?.name ?: "anonymous"; rewrites: car? car.driver? car.driver.name? car.driver.name? car.driver.name :anonymous :anonymous :anonymous :anonymousI guess that this operator is only really worth it in languages where every type is nullable, though. DavidIt might mean identity (return the argument unchanged) for value types.
Jul 13 2012
On 12/07/2012 22:49, David Piepgrass wrote:But GCC can't control the C++ language spec. Naturally there is a however, and a lot of people (including me) have also been pestering the that might be null.) I don't see why you would use ?: instead of ??, though.Yeah, I've been planning to try and get this into D one day. Probably something like: (a ?: b) -> (auto __tmp = a, __tmp ? __tmp : b)gcc used to have that extension and they dropped it...
Jul 12 2012
"David Piepgrass" , dans le message (digitalmars.D:172164), a écrit :Because ?: is the ternary conditionnal operator with missing second operand. a ?: b // or maybe a ? : b is just a short hand for a ? a : b (except a is evaluated only once).But GCC can't control the C++ language spec. Naturally there is a reluctance to add nonstandard features. It's a successful feature on object references that might be null.) I don't see why you would use ?: instead of ??, though.Yeah, I've been planning to try and get this into D one day. Probably something like: (a ?: b) -> (auto __tmp = a, __tmp ? __tmp : b)gcc used to have that extension and they dropped it...
Jul 13 2012
On 2012-07-12 18:24, Daniel Murphy wrote:Yeah, I've been planning to try and get this into D one day. Probably something like: (a ?: b) -> (auto __tmp = a, __tmp ? __tmp : b)We need to combine it with the assignment operator as well: Object foo; foo ?:= new Object; // only assign if "foo" is null -- /Jacob Carlborg
Jul 12 2012