www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - just an idea (!! operator)

reply "akaz" <nemo utopia.com> writes:
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
next sibling parent reply =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <alex lycus.org> writes:
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
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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
parent reply =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <alex lycus.org> writes:
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
parent reply "David Piepgrass" <qwertie256 gmail.com> writes:
 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
parent "Araq" <rumpf_a web.de> writes:
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
prev sibling next sibling parent reply "monarch_dodra" <monarch_dodra gmail.com> writes:
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
parent Don Clugston <dac nospam.com> writes:
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
prev sibling next sibling parent reply deadalnix <deadalnix gmail.com> writes:
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
parent "akaz" <nemo utopia.com> writes:
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
prev sibling parent reply "Jonas Drewsen" <jdrewsen nospam.com> writes:
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
next sibling parent reply travert phare.normalesup.org (Christophe Travert) writes:
"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.

 ...
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
parent travert phare.normalesup.org (Christophe Travert) writes:
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.

 ...
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
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
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;

 /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
next sibling parent "Roman D. Boiko" <rb d-coding.com> writes:
On Thursday, 12 July 2012 at 12:51:38 UTC, Jacob Carlborg wrote:
 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;

 /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
prev sibling parent travert phare.normalesup.org (Christophe Travert) writes:
Jacob Carlborg , dans le message (digitalmars.D:172056), a écrit :
 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;

 /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
prev sibling parent reply "Daniel Murphy" <yebblies nospamgmail.com> writes:
"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
next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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
next sibling parent reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <alex lycus.org> writes:
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
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
parent "Roman D. Boiko" <rb d-coding.com> writes:
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
prev sibling next sibling parent reply deadalnix <deadalnix gmail.com> writes:
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
next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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
parent Timon Gehr <timon.gehr gmx.ch> writes:
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
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
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
prev sibling parent reply "David Piepgrass" <qwertie256 gmail.com> writes:
 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 on object references that might be null.) I don't see why you would use ?: instead of ??, though.
Jul 12 2012
next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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 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.
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
parent reply "Jonas Drewsen" <jdrewsen nospam.com> writes:
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 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.
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
next sibling parent reply "monarch_dodra" <monarch_dodra gmail.com> writes:

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
next sibling parent reply "Jonas Drewsen" <jdrewsen nospam.com> writes:
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
next sibling parent travert phare.normalesup.org (Christophe Travert) writes:
"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
prev sibling parent deadalnix <deadalnix gmail.com> writes:
On 13/07/2012 13:31, Jonas Drewsen wrote:
 On Friday, 13 July 2012 at 09:49:22 UTC, monarch_dodra wrote:

 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
prev sibling parent "David Piepgrass" <qwertie256 gmail.com> writes:
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
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
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
parent reply "David Nadlinger" <see klickverbot.at> writes:
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
parent reply "Roman D. Boiko" <rb d-coding.com> writes:
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
parent travert phare.normalesup.org (Christophe Travert) writes:
"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
prev sibling next sibling parent deadalnix <deadalnix gmail.com> writes:
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 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.
Jul 12 2012
prev sibling parent travert phare.normalesup.org (Christophe Travert) writes:
"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 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
prev sibling parent Jacob Carlborg <doob me.com> writes:
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