What are 'is' statements in D? - traits

Inside the std.traits module, I can find a line similar to:
assert(is(Unqual!(int) == int));
I know that Unqual removes any type modifiers like immutable, but what does the 'is' do? How is it different from an if statement and when should it be used?

is(Unqual!(int) == int) is an expression, not a statement. The line you posted does not exist in std.traits.
I assume you mean the following line:
static assert(is(Unqual!int == int));
See the documentation for IsExpression.

is is an expression that allows for example to check if types are equal, if 1 type is subtype of another or if T a class/enum/struct etc.
The code you posted checks if specified type does not have any modifiers.
For more information se D Language Documentation about IsExpression.

Related

What do you call doing assignment in a conditional?

Is there a special name for doing assignment in a conditional? Here is an example of what I am asking about in C:
// Assume a and b have been previously defined and are compatible types
if( (a = b) ) { // <- What do you call that?
//do something
}
A couple friends of mine and I are convinced that there is a name for it, and some other people have agreed that there is one, but we can't find it anywhere. Has anyone here ever heard a term for it?
Assuming that an assignment is intentional, there is no special name for this. C language specification places a very weak requirement on the controlling expression of an if statement:
6.8.4.1-1: The controlling expression of an if statement shall have scalar type.
An assignment expression satisfies this requirement, as long as a and b are scalar. An implicit comparison to zero is performed on the result of this assignment:
6.8.4.1-2: In both forms, the first substatement is executed if the expression compares unequal to 0. In the else form, the second substatement is executed if the expression compares equal to 0.
Note that compilers would issue a warning when they see an assignment like that, because missing second = is a common source of errors. You can prevent these warnings using parentheses, as described in this Q&A.
It doesn’t really have a name, though people do call it various things. If the code follows your question:
if( a = b )...
then common terms are: bug, error, etc. However if b is not a variable but an expression, e.g. as in the common C patterns:
if( (c = getchar()) != EOF )...
while( *q++ = *p++ )...
then it might be called an idiom, pattern, etc.
I don’t know if it has a name but I’d call it “useful feature that nobody understands”.
It is very useful indeed.
We can consider it a slang.
In C++ for example you can use this by declaring a variable directly, and this is useful for safety checks:
if (Object *a = takeObject()) {
// a is not nullptr
}
Or when I don’t want to repeat a statement in a loop:
while (a = next()) {
}
Instead of:
a = next();
while (a) {
a = next();
}
But commonly these are just mistakes which compilers like gcc and clang give warnings about (and they force you to put an horrible double tuple to silence the warning ew!).

Check/display the declared type of an entity (variable, expression...)

I am investigating types in VB especially in VBA. Generally, given an entity there are two types: Effective value type, I guess, is defined as value types in this part of the specification; Declared Type is defined in this part of the specification.
To do tests, I need to use some functions to check types. There are TypeName and VarType. I think they are used to check effective value type of an entity, because TypeName can return DBNull, Decimal and Nothing; VarType can return vbNull, vbEmpty, vbError and vbDecimal. These types exist in the table of effective value types, but not in the table of declared type.
So now, my question is, does anyone know how to check/display the declared type of an entity (variable, expression...)?
Edit 1: Probably for a variable, its declared type is just the type that the declaration of the variable specifies. I would like to insist that, it seems that VBA has declared type for expressions. For instance, Operator Declared Type is mentioned in this link. I think that refers to the declared type of the result of an operation. That means such entities as -i, i+5, i>6... can have a declared type. I would like to know how to display their declared type.
If,
Dim i as integer
i = 6/3
then you do, TypeName(i>3)
it returns Boolean which is the expression's Type based on the Type of the resulting value it holds not operand's declare type. And it complies with the specification given in your link for msdn 2.2 Entities and Declared Types.
Or else are you looking for a syntax/function (e.g. DType, imaginative function) that could return DType(i>3) as integer which is operand's (i) Type? Or rather it's more useful when you have multiple variables within some expression, so you can find all their Types in one go?
e.g. some String that combined all sorts of different TYPE variables in one expressoin.
Just trying to understand when and how this can be useful to you and what could be the end result you are looking for.. Seems like this is more to lanague root definitions.
PS: I do not have reps points to comment. So I added as an answer.

Exclamation operator?

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?
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.
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
You already got two excellent answers by jA_cOp and Merhdad. I just want answer directly to the OP question (what's the difference between this and cast(ushort)?) - The difference is that cast(ushort)args[1] will not work (you cannot cast from a string to an uint just like that), while the to!(type)(param) template knows what to do with the string and how to convert it to the primitive type.

What is the := operator called in VB.NET?

In VB.NET, you can invoke methods (or add attributes) with optional parameters like so:
DoSomething(FirstName:="Bob", LastName:="Smith")
...
<MyAttribute(SomeParam:=400)>
Public MyClass
...
I was trying to find out the name of the := operator, so I looked within MSDN's VB.NET 2010's Operators section and couldn't find it listed. Maybe it's not actually an operator, but instead something else?
I keep calling it the "colon equals operator" or the "parameter assignment notation" when talking to co-workers, but it would be nice to know what it is technically called. For example, I would call the << operator the "left bit-shift operator" and the <= operator the "greater-than-or-equal comparison operator." So then, what should := be called?
It's not an operator - it's just the syntax for named arguments.
Those are Named Arguments.
http://msdn.microsoft.com/en-us/library/dd264739.aspx

What is the definition of ":=" in vb

I came across some sample code in VB.Net which I have some experience with and kinda having a duh moment figuring out the meaning of :=.
RefreshNavigationImages(bForward:=True, startIndex:=-1)
The sig for this method is RefreshNavigationImages(boolean, int). Is this a default value if null? Like "bIsSomething ?? false"?
Tried to bing/google but they just don't like searching for operators especially if it's only 2 chars.
They are named parameters. They let you specify values for arguments in function calls by name rather than order.
The := indicates the use of named parameters. Rather than relying on the order of the parameters in the method declaration, named parameters allow you to specify the correlation of parameters to values by specifying the name of the parameter.