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

type inference in color initalizer #663

Closed
markusfassbender opened this issue Nov 8, 2019 · 1 comment
Closed

type inference in color initalizer #663

markusfassbender opened this issue Nov 8, 2019 · 1 comment
Milestone

Comments

@markusfassbender
Copy link
Contributor

markusfassbender commented Nov 8, 2019

Description

We are using SwiftGen and have a limited type-check in Swift enabled in Build Configuration -> "Other Swift Flags" -> "-warn-long-function-bodies=250".

Often appears the message:

Initializer 'init(rgbaValue:)' took 311ms to type-check (limit: 250ms)

Solution

Current Implementation

internal extension Color {
  convenience init(rgbaValue: UInt32) {
    let red   = CGFloat((rgbaValue >> 24) & 0xff) / 255.0
    let green = CGFloat((rgbaValue >> 16) & 0xff) / 255.0
    let blue  = CGFloat((rgbaValue >>  8) & 0xff) / 255.0
    let alpha = CGFloat((rgbaValue      ) & 0xff) / 255.0

    self.init(red: red, green: green, blue: blue, alpha: alpha)
  }
}

My suggestion definitely brings advantages by adding the concrete types instead of letting the compiler type-inference do the work:

extension Color {
    convenience init(rgbaValue: UInt32) {
        let red:   CGFloat = CGFloat((rgbaValue >> UInt32(24)) & UInt32(0xff)) / CGFloat(255.0)
        let green: CGFloat = CGFloat((rgbaValue >> UInt32(16)) & UInt32(0xff)) / CGFloat(255.0)
        let blue:  CGFloat = CGFloat((rgbaValue >> UInt32(8 )) & UInt32(0xff)) / CGFloat(255.0)
        let alpha: CGFloat = CGFloat((rgbaValue              ) & UInt32(0xff)) / CGFloat(255.0)
        
        self.init(red: red, green: green, blue: blue, alpha: alpha)
    }
}
@markusfassbender
Copy link
Contributor Author

merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants