Skip to content
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

Rule request: avoid calling init(arrayLiteral:) #1096

Closed
marcelofabri opened this issue Dec 31, 2016 · 3 comments
Closed

Rule request: avoid calling init(arrayLiteral:) #1096

marcelofabri opened this issue Dec 31, 2016 · 3 comments
Labels
rule-request Requests for a new rules.

Comments

@marcelofabri
Copy link
Collaborator

marcelofabri commented Dec 31, 2016

ExpressibleByArrayLiteral protocol declares init(arrayLiteral elements: Element...), but according to the documentation it shouldn't be called directly.

The types that implement this protocol are:

  • Array
  • ArraySlice
  • ContiguousArray
  • IndexPath
  • NSArray
  • NSCountedSet
  • NSMutableArray
  • NSMutableOrderedSet
  • NSMutableSet
  • NSOrderedSet
  • NSSet
  • SBElementArray
  • Set

List obtained from https://developer.apple.com/reference/swift/expressiblebyarrayliteral

@keith
Copy link
Collaborator

keith commented Jan 18, 2017

@marcelofabri quick question on this one, I see the info in the init(arrayLiteral docs, but not in the ExpressibleByIntegerLiteral protocol. Is there info somewhere about not calling this?

@marcelofabri
Copy link
Collaborator Author

@keith Not directly, but I think we can generalize it for all protocols in this "family" based on the proposal that renamed them:

No, it's exactly the opposite, as I keep saying.  Conformance to this
protocol does *not* mean you can initialize the type with a literal.
  func f<T: IntegerLiteralConvertible>() -> T {
    return T(integerLiteral: 43) // Error
    return T(43)                 // Also an Error
  }

// It means an instance of the type can be *written* as a literal:

  func f<T: IntegerLiteralConvertible>() -> T {
    return 43   // OK
  }

@ghost
Copy link

ghost commented Apr 24, 2018

Another example:

    _ = Calendar.current.dateComponents(Set(arrayLiteral: Calendar.Component.hour, Calendar.Component.minute, Calendar.Component.second, Calendar.Component.nanosecond), from: Date())

The above works but is not necessary to call Set(arrayLiteral: ...).

_ = Calendar.current.dateComponents([Calendar.Component.year, Calendar.Component.month, Calendar.Component.day], from: Date())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule-request Requests for a new rules.
Projects
None yet
Development

No branches or pull requests

2 participants