What is the difference between any and any[ ]?
If we look at this TypeScript code:
let name1: any = "John";
let name2: any[] = ["Mary", "Sue", "Sally"];
name1 = name2;
let name3: any[] = ["Luke", "Paul", "Ringo"];
let name4: any = "Mark";
name3 = name4;
The Javascript that it compiles to is:
var name1 = "John";
var name2 = ["Mary", "Sue", "Sally"];
name1 = name2;
var name3 = ["Luke", "Paul", "Ringo"];
var name4 = "Mark";
name3 = name4;
JavaScript variables are dynamic and don't have a set type. I can imagine that this could be reported as a warning or error by the TypeScript compiler, but that does not stop the generation of the JavaScript or prevent the JavaScript from running.
So while there is no current compile errors or warnings for any
/any[]
assignments, any[]
can still be used to inform the developer of expectations.
Note: This is not code I would ever write or suggest that any would ever use. This is just the OPs code with values and showing what the resulting JavaScript would be and that because it compiles to JavaScript, there can be no run-time errors due to type because variables have dynamic type. This is one of the reasons we choose to use TypeScript: compile time static type checking. For this reason, one would normally avoid any
and any[]
in favor of a class or interface that better represents the data. And in this case, string
vs. string[]
(or any other type) will show a compiler error.
In this example, both of the name1 = name2
and name3 = name4
are compile time errors as the variables infer the type from the assignment and their usage is type-checked in subsequent code.
let name1 = "John";
let name2 = ["Mary", "Sue", "Sally"];
name1 = name2; //Type 'string[]' is not assignable to type 'string'.
let name3 = ["Luke", "Paul", "Ringo"];
let name4 = "Mark";
name3 = name4; //Type 'string' is not assignable to type 'string[]'.
Difference between any[ ] and any[ ] = [ ]
stations: any[];
The above denotes the stations
is of type array of any
but its NOT initialized
stations: any[] = [];
The above denotes you are initializing
stations to an empty array
.
What is the difference between .any() and .any(1)?
.any(1)
is the same as .any(axis=1)
, which means look row-wise instead of per column.
With this sample dataframe:
x1 x2 x3
0 1 1 0
1 0 0 0
2 1 0 0
See the different outcomes:
import pandas as pd
df = pd.read_csv('bool.csv')
print(df.any())
>>>
x1 True
x2 True
x3 False
dtype: bool
So .any()
checks if any value in a column is True
print(df.any(1))
>>>
0 True
1 False
2 True
dtype: bool
So .any(1)
checks if any value in a row is True
What is the difference between Any and _
In short, Any
is a class, like an object in Java. _
is like a wildcard, and it is used to abbreviate things. In most of the cases it will work very similar, but when you think in, for example, a List[Any]
you'll get a list of anything, but if you have a List[_]
, you'll get a list of you don't know what, it could be nothing.
unknown' vs. 'any'
You can read more about unknown
in the PR or the RC announcement, but the gist of it is:
[..] unknown which is the type-safe counterpart of any. Anything is assignable to unknown, but unknown isn't assignable to anything but itself and any without a type assertion or a control flow based narrowing. Likewise, no operations are permitted on an unknown without first asserting or narrowing to a more specific type.
A few examples:
let vAny: any = 10; // We can assign anything to any
let vUnknown: unknown = 10; // We can assign anything to unknown just like any
let s1: string = vAny; // Any is assignable to anything
let s2: string = vUnknown; // Invalid; we can't assign vUnknown to any other type (without an explicit assertion)
vAny.method(); // Ok; anything goes with any
vUnknown.method(); // Not ok; we don't know anything about this variable
The suggested usage is:
There are often times where we want to describe the least-capable type in TypeScript. This is useful for APIs that want to signal “this can be any value, so you must perform some type of checking before you use it”. This forces users to safely introspect returned values.
Performance difference between .where(...).Any() vs ..Any(...)
The Any()
with the predicate can perform its task without an iterator (yield return
). Using a Where()
creates an iterator, which adds has a performance impact (albeit very small).
Thus, performance-wise (by a bit), you're better off using the form of Any()
that takes the predicate (x => x.Name == "bla"
). Which, personally, I find more readable as well...
On a side note, Where()
does not necessarily enumerate over all elements, it just creates an iterator that will travel over the elements as they are requested, thus the call to Any()
after the Where()
will drive the iteration, which will stop at the first item it finds that matches the condition.
So the performance difference is not that Where()
iterates over all the items (in linq-to-objects) because it really doesn't need to (unless, of course, it doesn't find one that satisfies it), it's that the Where()
clause has to set up an iterator to walk over the elements, whereas Any()
with a predicate does not.
Related Topics
Swift: Mkannotation Long Title Text
How to Create Generic Closures in Swift
Using Nsdate to Get Date for Easter
Add a File Generated by Run Script into The Test Target Compilation List in Xcode
Change Color of Row Programmatically in Watchkit
Wkwebview Calayer to Image Exports Blank Image
How to Enable a Button in Different Cases in Swift
Non-Translucent UItabbar Creates Strange Grey Bar
How to Create Viewcontrollers Without Storyboard and Set One as Delegate of The Other One
How to Use Gpx File for UI Tests Only
Swiftui: Unable to Animate Images
How to Create Nested Dictionary Elements in Swift
Swift How to Make List Start at The Top with No Padding
Cannot Convert Value of Type 'Foo!' to Expected Argument Type 'Foo!'