Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclamation operator?

Tags:

operators

d

I'm learning D and have seen a lot of code like this:

ushort x = to!ushort(args[1]); 

I assume this casts args[1] to ushort, but what's the difference between this and cast(ushort)?

EDIT: And what other uses does the exclamation mark operator have?

like image 278
thwd Avatar asked Dec 24 '11 09:12

thwd


People also ask

What is the exclamation point operator?

The logical complement operator, also known as the NOT operator in Java, is represented by an exclamation mark '! '. This operator changes true values to false and false to true. This operator only works with boolean .

What are the 3 Uses of exclamation mark?

Exclamation marks are used at the end of statements when a strong emotion is being expressed (good and bad – surprise, excitement or delight, but also anger, fear or shock), and tell a reader to add emphasis to a sentence. They might also suggest that a speaker is shouting.

What is exclamation operator in TypeScript?

The exclamation mark (non-null assertion) operator removes null and undefined from the type of an expression. It is used when we we know that a variable that TypeScript thinks could be null or undefined actually isn't.

What do exclamation marks mean?

Definition of exclamation point 1 : a mark ! used especially after an interjection or exclamation to indicate forceful utterance or strong feeling. 2 : a distinctive indication of major significance, interest, or contrast the game put an exclamation point on the season. — called also exclamation mark.


2 Answers

In D,

to!ushort(args[1]) 

is shorthand for the template instantiation

to!(ushort)(args[1]) 

and is similar to

to<ushort>(args[1]) 

in languages like C++/Java/C#.

The exclamation point is to note the fact that it's not a regular argument, but a template argument.

The notation does not use angle brackets because those are ridiculously difficult to parse correctly for a compiler (they make the grammar very context-sensitive), which makes it that much more difficult to implement a correct compiler. See here for more info.

The only other use I know about is just the unary 'not' operation (e.g. false == !true)... I can't think of any other uses at the moment.


Regarding the cast:

cast(ushort) is an unchecked cast, so it won't throw an exception if the value is out of range.

to!ushort() is a checked cast, so it throws an exception if the value is out of range.

like image 137
user541686 Avatar answered Sep 20 '22 17:09

user541686


The exclamation mark here is not an operator, it is just a token part of the explicit template instantiation syntax (described in detail here).

std.conv.to (docs) is a function template for converting between arbitrary types. It is implemented entirely in the library and has no special support in the language. It has a broader and different scope compared to the cast operator.

The to template takes two type parameters; a "to" type and a "from" type, in that order. In your example, the template is explicitly instantiated with the single type argument ushort for the "to" parameter, and a second type argument string (assuming args comes from the first parameter to main) is automatically inferred from the regular function argument passed to the function (args[1]) as the "from" parameter.

The resulting function takes a string parameter and returns a ushort parsed from that string, or throws an exception if it failed. The cast operator will not attempt this kind of high-level conversion.

Note that if there is more than one explicit template parameter, or that parameter has more than one token in it (ushort is a single keyword token), you must wrap the template parameter list in parentheses:

ushort result; result = to!(typeof(result))(args[1]); 

In this example, typeof, (, result and ) are four separate tokens and the parentheses are thus required.

To answer your last question, the ! token is also used for the unary not operator, unrelated to template instantiations:

bool yes = true; bool no = !yes; // 'no' is false 
like image 28
jA_cOp Avatar answered Sep 21 '22 17:09

jA_cOp