Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
// Returns `URL?`
public enum SocketEndPoint: String {
case eventSocket = "http://nowhere.com/events"
case liveSocket = "http://nowhere.com/live"
public var url: URL? {
return URL(string: self.rawValue)
}
}
// Uses Implicitly Unwrapped Optional in initializer
// Will crash on malformed URL
public struct WebSocket {
public let url: URL
public init(url: URL!) {
self.url = url
}
}
// His initializer
fileprivate init() {
self.eventSocket = WebSocket(url: SocketEndPoint.events.url))
self.liveSocket = WebSocket(url: SocketEndPoint.live.url))
self.eventSocket.delegate = self
self.liveSocket.delegate = self
}
extension URL {
/// Non-optional initializer with better fail output
public init(safeString string: String) {
guard let instance = URL(string: string) else {
fatalError("Unconstructable URL: \(string)")
}
self = instance
}
}
// Returns `URL`
public enum SocketEndPoint: String {
case eventSocket = "http://nowhere.com/events"
case liveSocket = "http://nowhere.com/live"
public var url: URL {
return URL(safeString: self.rawValue)
}
}
// Vanilla `URL` initialization
public struct WebSocket {
public let url: URL
public init(url: URL) {
self.url = url
}
}
// His initializer
fileprivate init() {
self.eventSocket = WebSocket(url: SocketEndPoint.events.url))
self.liveSocket = WebSocket(url: SocketEndPoint.live.url))
self.eventSocket.delegate = self
self.liveSocket.delegate = self
}
// Returns `URL` with guided fail
public enum SocketEndPoint: String {
case eventSocket = "http://nowhere.com/events"
case liveSocket = "http://nowhere.com/live"
public var url: URL? {
guard let url = URL(string: self.rawValue) else {
fatalError("Unconstructable URL: \(self.rawValue)")
}
return url
}
}
// Uses normal initializer
public struct WebSocket {
public let url: URL
public init(url: URL) {
self.url = url
}
}
// His initializer
fileprivate init() {
self.eventSocket = WebSocket(url: SocketEndPoint.events.url))
self.liveSocket = WebSocket(url: SocketEndPoint.live.url))
self.eventSocket.delegate = self
self.liveSocket.delegate = self
}

delight-by commented Jan 29, 2017

How about that?

enum SocketEndPoint {
     // if you want customized error
    static var events: URL = url("http://nowhere.com/events")

    // but I'd go with this one, as you can always validate SocketEndPoint in tests and go runtime checks free after that
    static var live = URL(string: "http://nowhere.com/live")! 

    private static func url(_ string: String) -> URL {
        guard let url = URL(string: string) else {
            fatalError("Unconstructable URL: \(string)")
        }
        return url
    }
}

SocketEndPoint.events
SocketEndPoint.live
  • less noise at call sites
  • stored, instead of reevaluated on every access
  • can be extended
  • if you need more than just urls (e.g. SocketEndPoint.events.metadata), you can have structs in place of URL's
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment