digitalmars.D - just an idea (!! operator)
- "akaz" <nemo utopia.com> Jul 11 2012
- =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <alex lycus.org> Jul 11 2012
- Timon Gehr <timon.gehr gmx.ch> Jul 11 2012
- "monarch_dodra" <monarch_dodra gmail.com> Jul 11 2012
- Don Clugston <dac nospam.com> Jul 11 2012
- deadalnix <deadalnix gmail.com> Jul 11 2012
- "akaz" <nemo utopia.com> Jul 11 2012
- "David Piepgrass" <qwertie256 gmail.com> Jul 11 2012
- "Jonas Drewsen" <jdrewsen nospam.com> Jul 12 2012
- travert phare.normalesup.org (Christophe Travert) Jul 12 2012
- Jacob Carlborg <doob me.com> Jul 12 2012
- "Daniel Murphy" <yebblies nospamgmail.com> Jul 12 2012
- Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> Jul 12 2012
- =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <alex lycus.org> Jul 12 2012
- deadalnix <deadalnix gmail.com> Jul 12 2012
- Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> Jul 12 2012
- Timon Gehr <timon.gehr gmx.ch> Jul 12 2012
- Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> Jul 12 2012
- travert phare.normalesup.org (Christophe Travert) Jul 13 2012
- deadalnix <deadalnix gmail.com> Jul 13 2012
- Jacob Carlborg <doob me.com> Jul 13 2012
- deadalnix <deadalnix gmail.com> Jul 12 2012
- travert phare.normalesup.org (Christophe Travert) Jul 13 2012
- Jacob Carlborg <doob me.com> Jul 12 2012
- "Roman D. Boiko" <rb d-coding.com> Jul 12 2012
- "Araq" <rumpf_a web.de> Jul 12 2012
- "Roman D. Boiko" <rb d-coding.com> Jul 12 2012
- "Jonathan M Davis" <jmdavisProg gmx.com> Jul 12 2012
- "David Piepgrass" <qwertie256 gmail.com> Jul 12 2012
- "Jonas Drewsen" <jdrewsen nospam.com> Jul 13 2012
- "monarch_dodra" <monarch_dodra gmail.com> Jul 13 2012
- "Jonas Drewsen" <jdrewsen nospam.com> Jul 13 2012
- "David Nadlinger" <see klickverbot.at> Jul 13 2012
- "Roman D. Boiko" <rb d-coding.com> Jul 13 2012
- "David Piepgrass" <qwertie256 gmail.com> Jul 13 2012
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: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.
Making the operator overloadable does not result 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: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.
Making the operator overloadable does not result in a Phobos dependency.
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.org
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.
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
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 11:18:21 UTC, akaz wrote:if needed, the operator !! (double exclamation mark) could be defined. ...
Or the operator?? could be borrowed from c# 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:if needed, the operator !! (double exclamation mark) could be defined. ...
Or the operator?? could be borrowed from c# auto a = foo ?? new Foo(); is short for: auto a = foo is null ? new Foo() : foo;
or maybe: auto a = ! ! foo ? foo : new Foo(); || could be redifined to have this a behavior, but it would break code.
Jul 12 2012
Christophe Travert, dans le message (digitalmars.D:172047), a écrit :"Jonas Drewsen" , dans le message (digitalmars.D:172039), a écrit :On Wednesday, 11 July 2012 at 11:18:21 UTC, akaz wrote:if needed, the operator !! (double exclamation mark) could be defined. ...
Or the operator?? could be borrowed from c# auto a = foo ?? new Foo(); is short for: auto a = foo is null ? new Foo() : foo;
or maybe: auto a = ! ! foo ? foo : new Foo();
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).
Jul 12 2012
On 2012-07-12 13:35, Jonas Drewsen wrote:Or the operator?? could be borrowed from c# auto a = foo ?? new Foo(); is short for: auto a = foo is null ? new Foo() : foo; /Jonas
I 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
Jacob Carlborg , dans le message (digitalmars.D:172056), a écrit :On 2012-07-12 13:35, Jonas Drewsen wrote:Or the operator?? could be borrowed from c# auto a = foo ?? new Foo(); is short for: auto a = foo is null ? new Foo() : foo; /Jonas
I 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
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)
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; /Jonas
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)
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...auto a = foo ?? new Foo(); is short for: auto a = foo is null ? new Foo() : foo; /Jonas
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 12-07-2012 19:32, Andrei Alexandrescu wrote:On 7/12/12 12:24 PM, Daniel Murphy wrote:"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; /Jonas
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
But on the other hand, C# has had it from day one and it's still widely 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
Jul 12 2012
On 12/07/2012 19:32, Andrei Alexandrescu wrote:On 7/12/12 12:24 PM, Daniel Murphy wrote:"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; /Jonas
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
Do you know why ? It have been useful to me in languages that support it.
Jul 12 2012
On 7/12/12 2:37 PM, deadalnix wrote:On 12/07/2012 19:32, Andrei Alexandrescu wrote:On 7/12/12 12:24 PM, 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)
gcc used to have that extension and they dropped it... Andrei
Do you know why ? It have been useful to me in languages that support it.
I apparently misspoke. Here's the search: http://goo.gl/zjKiJ Andrei
Jul 12 2012
On 07/12/2012 09:03 PM, Andrei Alexandrescu wrote:On 7/12/12 2:37 PM, deadalnix wrote:On 12/07/2012 19:32, Andrei Alexandrescu wrote:On 7/12/12 12:24 PM, 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)
gcc used to have that extension and they dropped it... Andrei
Do you know why ? It have been useful to me in languages that support it.
I apparently misspoke. Here's the search: http://goo.gl/zjKiJ Andrei
You might have had in mind the <? and >? operators.
Jul 12 2012
On 7/12/12 4:49 PM, David Piepgrass 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)
gcc used to have that extension and they dropped it...
But GCC can't control the C++ language spec. Naturally there is a reluctance to add nonstandard features. It's a successful feature in C#, however, and a lot of people (including me) have also been pestering the C# crew for "null dot" (for safely calling methods on object references that might be null.) I don't see why you would use ?: instead of ??, though.
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. Andrei
Jul 12 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:I don't know much about C#, but in C#, isn't EVERYTHING a 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.
Due to lazy being broken, this cannot be safe, pure or nothrow.
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
"Roman D. Boiko" , dans le message (digitalmars.D:172259), a écrit :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. David
It might mean identity (return the argument unchanged) for value types.
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 :anonymous
Jul 13 2012
On 12/07/2012 22:49, David Piepgrass 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)
gcc used to have that extension and they dropped it...
But GCC can't control the C++ language spec. Naturally there is a reluctance to add nonstandard features. It's a successful feature in C#, however, and a lot of people (including me) have also been pestering the C# crew for "null dot" (for safely calling methods on object references that might be null.) I don't see why you would use ?: instead of ??, though.
Well C# does use ?? but ?: seems to me used in more languages.
Jul 12 2012
"David Piepgrass" , dans le message (digitalmars.D:172164), a écrit :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...
But GCC can't control the C++ language spec. Naturally there is a reluctance to add nonstandard features. It's a successful feature in C#, however, and a lot of people (including me) have also been pestering the C# crew for "null dot" (for safely calling methods on object references that might be null.) I don't see why you would use ?: instead of ??, though.
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).
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
On Thursday, 12 July 2012 at 12:51:38 UTC, Jacob Carlborg wrote:On 2012-07-12 13:35, Jonas Drewsen wrote:Or the operator?? could be borrowed from c# auto a = foo ?? new Foo(); is short for: auto a = foo is null ? new Foo() : foo; /Jonas
I really like that operator. The existential operator, also known as the Elvis operator :) . It's available in many languages with slightly different semantics.
+1
Jul 12 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)
ML, Haskell, Clean, Prolog, Mercury, Scala, F# ...
Jul 12 2012
On Thursday, 12 July 2012 at 17:35:05 UTC, Alex Rønne Petersen wrote:But on the other hand, C# has had it from day one and it's still widely used and encouraged today:
It has been introduced in C# 2.0 and quickly gained high popularity.
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 is with C#), then a lot more programmers know about it and are more comfortable with using it, so it gets used a lot more. - Jonathan M Davis
Jul 12 2012
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...
But GCC can't control the C++ language spec. Naturally there is a reluctance to add nonstandard features. It's a successful feature in C#, however, and a lot of people (including me) have also been pestering the C# crew for "null dot" (for safely calling methods on object references that might be null.) I don't see why you would use ?: instead of ??, though.
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: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...
But GCC can't control the C++ language spec. Naturally there is a reluctance to add nonstandard features. It's a successful feature in C#, however, and a lot of people (including me) have also been pestering the C# crew for "null dot" (for safely calling methods on object references that might be null.) I don't see why you would use ?: instead of ??, though.
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.
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. /Jonas
Jul 13 2012
I don't know much about C#, but in C#, isn't EVERYTHING a
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:I don't know much about C#, but in C#, isn't EVERYTHING a 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
On Friday, 13 July 2012 at 12:51:07 UTC, Jacob Carlborg wrote:On 2012-07-13 11:24, Jonas Drewsen wrote:// 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 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
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. David
It might mean identity (return the argument unchanged) for value types.
Jul 13 2012
On Friday, 13 July 2012 at 09:49:22 UTC, monarch_dodra wrote:I don't know much about C#, but in C#, isn't EVERYTHING a reference type? Meaning it always makes sense to check if "myobject is null".
No, C# has value types (enums, primitives, and user-defined 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









=?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <alex lycus.org> 