-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Casting to Task without specifying generic type #36
Comments
Hello, I just gave it a quick try and it seems to work:
Surely there is a better way of doing this but this, I think, could at least work somehow. |
Hi. Any updates on this? I'm running into the same problem. I'm porting my code from Objc to Swift and have a function like this: (void) handleException:(BFTask*)task{} converted to Swift: func handleException(_ task: Task)() In ObjC the input can be any BFTask but in Swift I get the "Task cannot be caster to Task" error mentioned above by @adrianhr91 . @xxtesaxx solution is a smart approach but requires the use of AnyObject in the input rather than a specific type. |
I ended up going a different route, by using an extension I can create a function in the Task Class that can be called from any generic Task. So instead of a function that takes a generic Task as input, I have a function that is a prototype of a generic Task. extension Task{ This way handleException() can be called on any Task Hope that helps someone else. |
I'm trying to create a universal error handler for my application and to make matters even more complicated I'm using an event bus to pass a task around. I'm trying to do the following:
Option 1 gives a compile time error saying the generic type TResut of Task cannot be inferred.
Option 2 causes a runtime error saying
Task<SpecificType> cannot be caster to Task<AnyObject>
I have the same implementation in Java where it seems you don't need to specify the generic type so I appreciate this might be a language limitation rather than a library one.
TResult
could be potentially any type so I can't specify it in the method above. Is there any way to work around this?The text was updated successfully, but these errors were encountered: