How to cast both sides of Either? - flutter

I am trying to have Either<Failure, Concrete> where Concrete extends Abstract class, but I cannot get it to work. If I try to cast the left side I get the error:
type 'Right<Failure, Concrete>' is not a subtype of type 'Either<Failure, Abstract>' in type cast
if I try for the right side I get:
type 'Left<Object, Concrete>' is not a subtype of type 'Either<Failure, Concrete>' in type cast
And bimap does not seems to solve it. Is there an easy way to do it? tks

Related

type 'ParserRecovery' is not a subtype of type 'TypeBuilder?' in type cast

I'm trying to run my code and I'm getting this issue.
Unhandled exception:
type 'ParserRecovery' is not a subtype of type 'TypeBuilder?' in type cast
#0 OutlineBuilder._endClassMethod (package:front_end/src/fasta/source/outline_builder.dart:1726:37)
My code is not even getting compiled, nor I get any warnings/errors even if I make any errors in my code.

Can not satisfy generic constraint with subclass?

I have following compiler error:
Cannot convert value of type ‘PostTagViewModel<CreatePostMediaViewModel>’ to expected argument type ‘PostTagViewModel<PostMediaViewModel>’
CreatePostMediaViewModel is subclass of PostMediaViewModel, so on the first glance it seemed like this should work out of the box, but sadly it does not?
EDIT:
Some addtional info, swift array is generic and I can pass [Subclass] where parameter is declared [Baseclass]

How do I correctly cast a dynamic list to a List<List<String>> without runtime errors when .cast() has no effect?

I'm receiving data in a MethodCall object, which means that I cannot receive it as any type other than dynamic:
dynamic listOfObjects = methodCall.arguments;
but because I am the one sending the data from the platform-specific code, I know that the data is guaranteed to be of type List<List<String>>.
I want to inflate this data into a collection of concrete Dart object types:
List<DartObject> dartObjects =
methodCall.arguments.map((raw) => DartObject(
prop1: raw[0],
prop2: raw[1],
prop3: raw[2],
)).toList();
but this code fails with this error:
type 'List<dynamic>' is not a subtype of type 'List<String>' in type cast
I've tried extensively to solve this issue on my own:
Dart's own documentation on fixing common type problems,
responses to similar Flutter issues, and
answers to similar Stack Overflow questions
all say to use the List's cast() method, but even this falls over at runtime with the same error:
(call.arguments as List).cast<List<String>>()
// => type 'List<dynamic>' is not a subtype of type 'List<String>' in type cast
I'm sure I must be missing something obvious at this point. What am I doing wrong?
In short, how do I correctly cast to List<List<String>> without copying everything into a new data structure (i.e. without the use of .from or .map)?
The following code should work:
(call.arguments as List<dynamic>).map((e) => (e as List<dynamic>).cast<String>())

Cast to a Metatype Type in Swift?

Can you cast to a Metatype Type in Swift? It really seems like you should be able to (after all you can instantiate objects from Metatypes).
The following doesn't work:
class Super {}
class A : Super {}
let superA:Super = A()
let subType = superA.dynamicType
let afterCast = superA as subType
//Compiler error: "use of undeclared type 'subType'"
Does anyone know the right way to do this?
Edit:
As newacct pointed out, the result of .dynamicType is obviously not known until runtime, so a compile-time cast to the result of .dynamicType would not make sense.
So the answer is: "You can't" (and there is no good reason to try).
First of all, as takes a type, not an expression, on the right-hand side. So what you have is a syntax error.
What you seem to be trying to do is "cast" to a type that is computed at runtime. What would that even mean? Let's first consider what is a "cast".
Usually, when we have a cast expression x as T, it has two components:
At compile-time: The entire cast expression x as T has compile-time type T?, which allows you to do stuff with the resulting expression that you maybe cannot do on x directly. In other words, it allows you to change the compile-time type.
At runtime: It checks whether the runtime type of x is a subtype of T, and if it is, it evaluates to the optional containing that value, otherwise, it evaluates to nil.
If the type T is not known at compile-time, then obviously you cannot do the compile-time part of it. (The compile-time type of the resulting expression cannot, obviously, depend on something which is not known at compile-time.)
The other part, the runtime component, could that be done with a type computed at runtime? Sure. For example,
import Foundation
let afterCast : Super? =
(superA as AnyObject).isKindOfClass(subType) ? superA : nil
It's not clear if that is what you want.

For "Box[+T]", can I say "The type Box is covariant", is it correct?

Say I defined a type constructor Box:
trait Box[+T]
What is correct?
type Box is covariant
type constructor Box is covariant
type parameter T is covariant
type parameter T in type constructor Box is covariant
If all of them are incorrect, what's the correct expression?
About variance from the book Functional Programming in Scala:
In the declaration trait List[+A], the + in front of the
type parameter A is a variance annotation which signals that A
is a covariant or “positive” parameter of List. This means
that, for instance, List[Dog] is considered a subtype of
List[Animal] , assuming Dog is a subtype of Animal.
The correct one is :
"type Box is covariant in T"