chalup / advent-swift

Advent of Code in Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

People normally use enum or struct for Error

ayoy opened this issue · comments

class CannotFindNounAndVerb : Error {}

Class is fine (i.e. it compiles), but remember that classes in Swift are reference types, while structs and enums are value types.

Ok, why exactly would I want my error to be a value type instead of reference type? (I know the difference between these two; I just don't see the all cons & pros here).

There's a host of reasons, and it might depend on your particular use case, but for me it's that structs are simpler to handle by the runtime. Reference types seem heavier as they need to be handled by the reference counting mechanism.

That said, errors in Swift are in most cases enum types, and you would like to follow the convention. Unless you're interested in inheritance or preventing copying.