Skip to content

Instantly share code, notes, and snippets.

@hcrub
Last active May 3, 2019 21:28
Show Gist options
  • Select an option

  • Save hcrub/e2aabc7959bb5c7c1b35ba8acd9e5f2a to your computer and use it in GitHub Desktop.

Select an option

Save hcrub/e2aabc7959bb5c7c1b35ba8acd9e5f2a to your computer and use it in GitHub Desktop.

Revisions

  1. hcrub revised this gist May 3, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Optional.swift
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ extension Optional: OptionalType {}
    // The following example shows how you can use the `OptionalType` interface to scope
    // an extension containing an associated type defined as optional. E.g. `InputType??`

    interface Validation {
    protocol Validation {
    associatedType InputType
    }

  2. hcrub created this gist May 3, 2019.
    28 changes: 28 additions & 0 deletions Optional.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    /// Interface representing an optionally nil value type.
    protocol OptionalType: ExpressibleByNilLiteral {
    /// The boxed optional type.
    associatedtype Wrapped
    }

    extension Optional: OptionalType {}

    // Example

    // In this case, we have an extension that returns an optional type.
    // If the associated type is defined as optional, you will return a double optional.
    // The following example shows how you can use the `OptionalType` interface to scope
    // an extension containing an associated type defined as optional. E.g. `InputType??`

    interface Validation {
    associatedType InputType
    }

    // Returns `InputType?`
    extension Validation {
    func validate() -> InputType? {}
    }

    // Returns `InputType?`
    extension Validation: InputType {
    func validate() -> InputType {}
    }