4
normanzb
281d

when the swift language cannot infer the generic type from the invocation, it also doesn't want you to explicily specify generic type in the bracket...

says i have a method:
```
func createDeferred<T>() -> ABC.XYZ.Deferred<T>
```

then if you call it like this:
```
let dfd = createDeferred()
```
It complaints that it cannot infer generic type T, which make sense.

But it also doesn't want you to code it this way:
```
let dfd = createDeferred<Int>("countProperty")
```
if you do so, it mumbles gibberish: "Cannot explicitly specialize a generic function".

What it actually trying to say is, you should put the type somewhere else so that it can show off its smartness to "infer" it from there:

```
let dfd: ABC.XYZ.Deferred<Int> = createDeferred()
```
with a few more typing and findout what exact type it is, it finally works.

the moral of the story is, in order to communicate with the wonderful work apple genius made, you don't tell it what is the answer straight away, that's defiance, you must hide the answer somewhere intricately and let the smarty swifty swift to find it out for you.

Comments
  • 2
    Type inference is done by Apple Juniors at run time, they'd be out of a job if you could specify types yourself.
  • 1
    Generics in Swift work a little bit differently than they do in C#, for instance.

    If you want to specify the generic type explicitly, in your case you can do it by applying an explicit type to the variable that you assign the result to.

    Admittedly, the error message from the compiler is not very helpful.
  • 1
    Ah, I see that you already found out.
  • 0
    @localpost in order for your joke to work, you’d have to understand the problem, which you clearly don’t.
  • 1
    @Lensflare right, I don't have the swift fan shirt on my avatar so I'll withdraw from this discussion
  • 0
    @localpost Great. Thanks.
Add Comment