Skip to content

Instantly share code, notes, and snippets.

@brandonvanha
Last active February 27, 2018 05:57
Show Gist options
  • Save brandonvanha/f50fa1ce40132f648319e7f8390060bf to your computer and use it in GitHub Desktop.
Save brandonvanha/f50fa1ce40132f648319e7f8390060bf to your computer and use it in GitHub Desktop.

Revisions

  1. brandonvanha revised this gist Feb 27, 2018. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions generics_and_protocols.swift
    Original file line number Diff line number Diff line change
    @@ -2,3 +2,13 @@ protocol Numeric {
    func *(lhs: Self, rhs: Self) -> Self
    }

    extension Double: Numeric {}
    extension Float: Numeric {}
    extension Int: Numeric {}

    func genericMultiplier<T: Numeric>(lhs: T, rhs: T) -> T {
    return lhs * rhs
    }

    genericMultiplier(lhs: 5, rhs: 6)
    genericMultiplier(lhs: 18.2 rhs: 19.5)
  2. brandonvanha created this gist Feb 27, 2018.
    4 changes: 4 additions & 0 deletions generics_and_protocols.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    protocol Numeric {
    func *(lhs: Self, rhs: Self) -> Self
    }