What is the cause of this type error?
The problem is that when you don't explicitly supply the generic parameter type of ZipList
when you refer to it, the compiler will try and infer it for you – which it doesn't always get correct.
As you're already inside a ZipList<A>
class, the compiler will try and infer ZipList
to be ZipList<A>
when you omit the generic parameter (see this question for more info about this behaviour).
Therefore it's now expecting an input of [A]
in the ZipList(xs:_)
initialiser, meaning that the map function is inferred to be A -> A
, which you're trying to pass A -> B
to, causing the type mismatch (this is why f
is highlighted as the problem in your error).
If you simplify down your example to just calling init()
on your ZipList
without providing an argument, you'll see a more helpful error message:
class ZipList<A> {
init() {}
func map<B>() -> ZipList<B> {
// error: Cannot convert return expression of type 'ZipList<A>' to 'ZipList<B>'
return ZipList()
}
}
The fact that the compiler completely ignores the explicit type annotation of the return for the map()
method is a bug and is tracked by SR-1789. The cause, as described by Jordan Rose in the comments of the report is that:
It seems to be a case of us eagerly assuming the parameters are the same as for
self
. (That's usually a feature, but not when it gets in the way of other inference.)
The solution, as you've already found, is to explicitly state the generic parameter type of ZipList
when you create a new instance:
return ZipList<B>(xs: xs.map(f))
This forces the generic parameter to be of type B
, therefore preventing Swift from incorrectly inferring it, allowing the map
function to resolve.
As for what the error message "Cannot convert value of type 'A -> B' to expected argument type '_ -> _" means, _
in this case simply refers to a generic type which the compiler cannot resolve (not a helpful error message, I know). So all the compiler is telling you is that it was expecting a function that takes an input of an unknown type, and returns that same type.
It often helps when diagnosing these kind of error messages to split the expression up into multiple sub-expressions and inspect the types for each those to try and find the mis-match. It can also help to begin simplifying the example down (like using init()
instead of init(xs:[A])
in your map
method), until you run into a more helpful error message.
What is the cause of this mysterious Haskell type error involving reflection?
reify :: forall a r. a -> (forall s. Reifies s a => Proxy s -> r) -> r
The skolem requirement essentially states that r
in the type above can not depend on the s
which is being quantified in the second argument. Otherwise, indeed it would "escape" its scope since reify
returns r
.
In
reifyBad :: (forall s. Reifies s s => Proxy s) -> ()
reifyBad p = reify undefined (\(_ :: Proxy t) -> p @t `seq` ())
we see that the second argument of reify
is \(_ :: Proxy t) -> p @t `seq` ()
, so the type r
would be return type of that function, which is ()
. Since r ~ ()
does not depend on s
, there's no escaping problem here.
However, p @t
according to the type of p
requires Reifies t t
.
Since reify
will choose t ~ s
, the constraint is the same as Reifies s s
.
Instead, reify
only provides Reifies s a
where a
is the type of undefined
.
The subtle point here is that, while undefined
can produce any type a
, the type checker can not unify s
and a
. This is because a function with the same type as reify
is entitled to receive only one value of a fixed (rigid) type a
, and then choose as many types s
as wanted. Unifying all such s
s with a single a
would be wrong, effectively constraining the choice of s
by reify
.
Instead, in the variant
reifyBad' :: (forall s. Reifies s s => Proxy s) -> ()
reifyBad' p = reify undefined (\(_ :: Proxy t) -> p @t) `seq` ()
here r
is inferred to be the return type of \(_ :: Proxy t) -> p @t
, which is Proxy t
, where t ~ s
. Since r ~ Proxy s
does depend on s
, we trigger a skolem error.
Why does assigning to `NaN` or `undefined` cause a TypeError?
In ES5, there are 6 different kinds of native errors:
EvalError
This exception is not currently used within this specification. This
object remains for compatibility with previous editions of this
specification.RangeError
Indicates a numeric value has exceeded the allowable range.
ReferenceError
Indicate that an invalid reference value has been detected.
SyntaxError
Indicates that a parsing error has occurred.
TypeError
Indicates the actual type of an operand is different than the expected
type.URIError
Indicates that one of the global URI handling functions was used in a
way that is incompatible with its definition.
In your case, the error is thrown because you attempt to assign a value to window.NaN
or window.undefined
, which are non-writable properties.
Before assigning the new value, the internal [[Put]] method checks [[CanPut]], which will return false because the property is non-enumerable. And therefore [[Put]] will throw.
So the problem is that the writability of the assigned reference (left operand in the assignment expression) is not the expected one. Then, among the 6 error kinds above, the most appropriate seems TypeError, even if writability is not exactly a type.
What could cause a type error during the build process of Keras Layer?
The stacktrace indicates that somewhere in the code a Dimension
value is given as n argument where you usually expect an integer
/float
.
I think that this might be caused by this line
shape = tf.TensorShape((input_shape[1], self.num_outputs))
please try to change it to
shape = (input_shape[1], self.num_outputs)
Edit: As pointed out in the comments, this is not working of input_shape
is a Dimension
object and not an int
. To solve this, use this modification:
shape = tf.TensorShape((input_shape[1], self.num_outputs)).as_list()
This will at first create a new TensorShape
which is able to deal with the Dimension
data type and then convert this to a list of integers.
setTimeout function in react causes type error
Event values are cleared by react. You either need to use event.persist to persit event values or store the values from event to be used later
According to react documentation:
SyntheticEvent
object will be reused and all properties will be
nullified after the event callback has been invoked. This is for
performance reasons. As such, you cannot access the event in an
asynchronous way.
const handleSearchChange = (e) => {
// setSearchKey(e.target.value.toLowerCase().trim());
clearTimeout(timeOut.current);
const value = e.target.value.toLowerCase().trim();
timeOut.current = setTimeout(() => {
setSearchKey(value);
}, 500);
}
Related Topics
Initialize @Stateobject With a Parameter in Swiftui
Deleting List Elements from Swiftui'S List
How to Generate a Random Number in a Range (10...20) Using Swift
Get Class Name of Object as String in Swift
How to Use Swift'S Codable to Encode into a Dictionary
When Are Argument Labels Required in Swift
Nsdate Timeintervalsince1970 Not Working in Swift
Swift Optional Escaping Closure Parameter
How to Change the Textual Representation Displayed For a Type in Swift
Get HTML from Wkwebview in Swift
What Is the 'Open' Keyword in Swift
Non-'@Objc' Method Does Not Satisfy Optional Requirement of '@Objc' Protocol