Why is an array not assignable to Iterable?
Arrays can implement interfaces (Cloneable
and java.io.Serializable
). So why not Iterable
? I guess Iterable
forces adding an iterator
method, and arrays don't implement methods. char[]
doesn't even override toString
. Anyway, arrays of references should be considered less than ideal - use List
s. As dfa comments, Arrays.asList
will do the conversion for you, explicitly.
(Having said that, you can call clone
on arrays.)
Type '{}' is not assignable to type 'any[] & Iterableany'
The API response is actually an Array
of Objects
. You are trying to assign an array of objects to an object.
Just reassign apiResult={};
to apiResult=[];
or apiResult:any= [];
and it should work as expected.
Type 'IterableIteratornumber' is not an array type or a string type
Apparently it is being strict and doesn't want to create an array using spread from an iterator (which is not an array). However it's possible to create an array from an iterator using the Array.from
function:
range(size: number, startAt = 0) {
return Array.from(Array(size).keys()).map((i) => i + startAt);
}
Array.from
accepts Iterable
.
Why is java.util.Set not Assignable From java.lang.Iterable?
That's because you're using isAssignableFrom
wrong.
As the docs say, isAssignableFrom(Class<?> cls)
"determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter". So cls
would be Set.class, and the full syntax would be:
Iterable.class.isAssignableFrom(Set.class)
.
...which, indeed, returns true
.
Typescript array.from(iterator) incorrect type and cannot be cast?
As you probably know, an iterator is an object with a next
method used for iterating through an iterable (an object with a [Symbol.iterator]
method for getting an iterator from it).
Array.from
accepts an iterable (or an array-like), but in that code it thinks it's just getting an iterator.
Most iterators, including all of the ones you get from standard JavaScript methods, are also iterable because they implement [Symbol.iterator]() { return this; }
, directly or indirectly through the iterator prototype —but not all do. So it's not safe to assume that all iterators are iterable.
You probably want to update routes
to show that it returns something that's both an iterator and iterable:
routes(): Iterator<T> & Iterable<T> {
return this._routes.values();
}
Then:
const iter = contentMap.routes();
const contentArray = Array.from(iter);
(There's no need for the explicit types on those, TypeScript will infer them.)
Here's a version on the playground demonstrating the problem using Iterator
.
Here's that same code using Iterator<T> & Iterable<T>
as above.
Why is FormArray not assignable to NgIterableany?
It should be
*ngFor="let year of formArray.controls;
formArray.controls is iterable
See working demo here
Type 'IterableIterator[number, any]' is not an array type or a string type
I solved it by adding "downlevelIteration": true, in compilerOptions inside tsconfig.josn file. Note my target was es2015
Why doesn't array instanceof Iterable compile in Java?
According to the JLS, Java SE 7 edition, §15.20.2 (Type Comparison Operator instanceof
):
If a cast of the RelationalExpression to the ReferenceType would be rejected as a
compile-time error, then theinstanceof
relational expression likewise produces
a compile-time error. In such a situation, the result of theinstanceof
expression
could never be true.
And §15.16 (Cast Expressions) states:
It is a compile-time error if the compile-time type of the operand may never be
cast to the type specified by the cast operator according to the rules of casting
conversion (§5.5).
Finally, §5.5.1 (Reference Type Casting) states:
Given a compile-time reference type S (source) and a compile-time reference type
T (target), a casting conversion exists from S to T if no compile-time errors occur
due to the following rules.[...]
If S is an array type SC[], that is, an array of components of type SC:
- If T is an interface type, then a compile-time error occurs unless T is the type
java.io.Serializable
or the typeCloneable
(the only interfaces implemented
by arrays).
Therefore, Java requires that your test to see if an array type is an instance of java.lang.Iterable
results in a compile-time error.
If you want to try and make it work (always return false
) anyway, you can cast the array to Object
first, like so:
System.out.println((((Object)array) instanceof Iterable));
Related Topics
Reverse Each Individual Word of "Hello World" String with Java
Can the Jvm Recover from an Outofmemoryerror Without a Restart
Print "Hello World" Every X Seconds
Why Is Java's Iterator Not an Iterable
Java 1.6: Creating an Array of List<T>
Docker: Combine Multiple Images
How to Implement Dynamic Gui in Swing
How to Remove Duplicate White Spaces in String Using Java
Eclipse: Attach Source/Javadoc to a Library via a Local Property
Debug a Java Application Without Starting the Jvm with Debug Arguments
Size of a Byte in Memory - Java
Clean Way to Combine Multiple Jars? Preferably Using Ant
How to Convert Ascii Code (0-255) to Its Corresponding Character