DEV Community

Discussion on: What simple things annoy you about your favourite programming languages?

Collapse
 
palle profile image
Palle • Edited

In Swift, it's often annoying that you cannot have a variable of a generic protocol type. You need to have a generic type implementing that protocol because you cannot specify associated types when declaring a variable.

For example, Swift has a Collection protocol, which has an associated generic type Element.

Instead of declaring a variable with let foo: Collection<Int> = ... you need to have a generic variable C: Collection where C.Element == Int, which can be pretty annoying because your enclosing structures also need to be generic.

Example

struct Foo<C: Collection> where C.Element == Int {
    var bar: C
}
Enter fullscreen mode Exit fullscreen mode

It would be so much more pleasant to write

struct Foo {
    var bar: Collection<Int>
}
Enter fullscreen mode Exit fullscreen mode

Yes I am aware of the AnyCollection<Element> type but that is just a workaround that is specific to the collection protocol and not a general solution and yes I am aware that the Swift compiler always wants to specialize generics at compile time but this is just annoying.