0

Does anybody have any insight as to why the Swift compiler's error messages are so bizarre and counterintuitive?

I was tinkering around with collection functions that take closures and the $0, $1 syntax. I tried this:

let dict1 = ["a" : true, "b": false, "c": true]

dict1.forEach
{
  print($1.1)
  dict2[$0.0] = String($0.1)
}

It seems that when you pass the entries in a dictionary into a closure, you can refer to each key/value pair as $0 and $1, or $0.0 and $0.1.

If you use $0 and $1, the $0 parameter is the key and $1 is the value for each key/value pair. If you use $0.0 and $1.1 instead, then $0.0 is the key and $0.1 is the value for each key/value pair.

The print statement is invalid. The closure only takes 1 parameter, the key/value pairs in the dictionary that is passed to it.

The compiler flags the print statement with the rather confusing error message "Value of type 'Bool' has no member '1'. It does make sense if you think about it, however. $1 maps to the value of a dictionary entry, and since we are passing in [String:Bool] dictionary entries, the value is a Bool. Trying to then fetch a $1 member of a bool does not make sense.

What doesn't make sense is that with the above code, the SECOND line:

dict2[$0.0] = String($0.1)

throws an error as well:

"Value of type String has no member '0'". Huh? Why does the line above cause the line below to fail?

Worse, if you enter these two lines as the body of the closure:

print($0, $1)
dict2[$0.0] = String($0.1)

Then the second line throws an error. Comment out the print statement and the assignment works. Comment out the assignment and the print works, but try to include both in the closure and you get an error.

This seems to suggest that you can refer to the key/value pairs passed into the closure as $0:$1, or $0.0:$0.1, but not both. Once you use a given notation, you have to follow that notation throughout the closure. It seems bizarre and horrible that the contents of one statement cause the next statement to throw an error.

4
  • The thing is that you want the compiler to infer the type of your closure but you are using the closure parameters in a way that leads to conflicting types. It is not bizarre at all. The real problem is that the IDE does not show you the inferred types so it is very difficult for a beginner to see the error.
    – Sulthan
    Mar 10, 2016 at 14:49
  • @Sulthan An additional comment to yours: after some tests, it seems as if the compiler can infer the type of the closure even if there's (seemingly) ambiguity/conflicting types in the references to the arguments in scope of the closure. Inference of dictionarys' Element to a two-argument key-value pair takes precedence over inference to a tuple. Hence, type inference does work out above, but trying to access members .0 or .1 of String or Bool type immutables, does not. Subsequently, error messages are not obfuscated in this case.
    – dfrib
    Mar 10, 2016 at 17:28
  • @Sulthan, can you elaborate? I'm a senior iOS developer. I have fairly solid knowledge of Swift, but am lacking in the subtleties.
    – Duncan C
    Mar 10, 2016 at 18:51
  • 1
    I think there are two answers which more than sufficiently explain the error messages, which seem to make perfect sense to me. I'd argue that this could all be avoided if we simply wrote dict1.forEach { key, value in /* ... */ }. It read better and avoid the ambiguity problems altogether.
    – nhgrif
    Mar 20, 2016 at 0:38

3 Answers 3

2

In the absence of explicit typing it all boils down to type inference.

Either the closure will be inferred to take a single tuple parameter $0 of type (String, Bool), in which case you need to refer to things as $0.0 and $0.1

Or it will be inferred to take two parameters $0: String and $1: Bool.

You can't have it both ways and at no point does $1.1 make any sense.

1

The demand for non-ambiguity in choice of key-value pair ($0/$1) vs tuple ($0.0/$0.1) is expected

The demand for an unambiguous choice between a tuple ($0.0/$0.1) or two key-value parameters ($0/$1) is expected behaviour; otherwise, the Swift compiler cannot infer the type of the arguments of the anonymous closure.

Many functional (.forEach, .map, .flatMap) tools cover, behind the hood, iterating over a sequence, using the generator method next() -> Element? method blueprinted in GeneratorType protocol. For dictionaries, associated type of Element is the type (Key, Value), and the generator DictionaryGenerator implements, as expected, the generator method with signature next() -> (Key, Value)?.

The possibility of an ambiguous choice in the assignment of the (Key, Value) return of .next() is a peculiarity of tuples, not of closures or dictionaries themselves.

// some tuple
let tuple : (String, Int) = ("One", 1)

// #1 assign tuple simply as a tuple
let tup : (String, Int)
tup = tuple

// #2 assign tuple to two seperate mutables: key & val
let key : String
let val : Int
(key, val) = tuple
print(key) // One
print(val) // 1

Anonymous closures, at the core of type inference, naturally does not allow any ambiguity in this choice, and as we'll see below, the compiler does make a choice of inference, even if there is seemingly an unambiguity. The closure (say for .forEach) needs to know, without doubt, which of the above assignments to choose. Once this choice is made, we cannot change it from assignment to assignment within the same closure, e.g. trying to use $0.1 as well as $1 (since only one of the "assignments" above is in effect within the scope of the closure).


Compare with explicit iterating over the Element:s of a dictionary

Comparing with explicitly iterating over a dictionary (using a for ... in dict1 loop) makes a good analogy; naturally we need to choose, in the signature of the for loop, whether to assign the immutable from next() to a tuple or to an explicit two-parameter key-value pair.

/* #1: Iterating over dictionary elements as tuples */
for tup in dict1 {
    print(tup.0, tup.1)
}
dict1.forEach { print($0.0, $0.1) }

/* #2: Iterating over dictionary elements as key-value 
   (separate) entities pairs */
for (key, val) in dict1 {
    print(key, val)
}
dict1.forEach { print($0, $1) }

In case #1, we access the entries in the dictionary as a tuple, which we can sub-access as tup.0 and tup.1 for key and value access, respectively (compare with $0.0 and $0.1). Whereas for case #2, we separate the "vars in ..." into two separate entities, key and val, comparable to the separate anonymous closure arguments $0 and $1. In the case of closures—much like for the loops above—the compiler needs to decide upon which of the above .next() "assignment cases" to make use of: by tuple or by key-value pair.


So the error messages, in this case, makes sense after all?

From the discussion above and the example+error messages you posted, we can infer (no pun intended) that the compiler infers the anonymous arguments in the following case as a two-parameter key-value assignment (and not a tuple)

dict1.forEach {
    print($1.1)                   // (E1)
    dict2[$0.0] = String($0.1)    // (E2)
}

Why? We could make an educated guess that this is because $1 is the first anonymous argument to appear in the closure, and that the use of $1 points to a key-value assignment of .next(), rather than a tuple (we shall see shortly that this guess is wrong, and that it's precedence rather that time of appearance that governs this). In this context, the two errors you've posted does, after all, make sense

(E1) "Value of type 'Bool' has no member '1'"

(E2) "Value of type String has no member '0'"

(E1) makes sense as $1 is inferred to be value, which is a boolean.

let foo : Bool = false
foo.1 // same error (E1)

(E1) makes sense as $0 is inferred to be key, which is a string.

let bar : String = "bar"
foo.0 // same error (E2)

With this explained, it's apparent that also the single (E2) error in the following part is to be expected (and is, imho, spot on).

dict1.forEach {
    print($0, $1)              // OK, anon. arguments inferred as key-value par (not tuple)
    dict2[$0.0] = String($0.1) // (E2); none of the arguments is a tuple.
}

This leads us to test quite an interesting case: what if our first reference to the anonymous arguments is non-sufficient to unambiguously infer the closures' argument type? E.g., what if our first reference is $0.0?

dict1.forEach {
    print($0.0)                // error (E2)
    dict2[$0.0] = String($1)   // error (E2)
}

// ... or 

dict1.forEach {
    print($0.0)                // error (E2)
    dict2[$0.0] = String($0.1) // error (E2)
    print($1)
}

So it would seem as inferring the anon. arguments as a separate two-argument pair has higher inference precedence than inference as tuple: the compiler doesn't care which anon. argument that appear first, but whether we treat .next() return as a

  • precedence higher: key-valuepair,
  • precedence lower: tuple.

With regard to weird spot-off error messages, in general: I agree, even if this (imho) is not the case here, we do see them a lot. I mostly run into these obfuscated error messages in the context of closures, and I think a lot of them relates type inference combined with the fact that the underlying standard functions used by members in the closures are throwing-functions that are evaluated lazily. Such functions tend to throw "their own errors" due to some invalidities of the caller (e.g. closure), without even reaching the point of an actual error of themselves. See e.g. the following Q&A for such an example.

1

$0 is shorthand for the first argument in the closure. When introducing $1, the compiler is inferring that you are treating both the key and and the value as separate arguments in the closure ($0 being the key, $1 being the value), rather than $0 as a the single-argument tuple (key, value). $1.1 tries to say "hey, $1 is some collection type that has additional values, access the value at index 1". But in your case it's just a value that cannot be subscripted.

I think it makes sense. You shouldn't be able to use both types of syntax in a closure, since $0 and $0.1 can represent different values (which change the value of $0).

As for why you get the weird error message, only the authors of SourceKit and llvm can answer that, which makes this question borderline opinion-based.

1
  • 1
    The weird error messages are due to type inferring. If all types (variables, closures etc) were specified explicitly, the compiler wouldn't have to guess. However, in most cases the programmer wants the types to be inferred - and when there is an error, it usually infers them wrong and emits a type mismatch error.
    – Sulthan
    Mar 10, 2016 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.