2016-11-13 1 views

답변

1

직접하실 수 없습니다. 그것은 Swift의 현재 누락 된 기능입니다. 당신은 더미 프로토콜을 사용하여 주위를 얻을 수 있습니다 :

protocol _Int {} 
extension Int: _Int {} 

extension CountableClosedRange where Bound: _Int { 
    var sum: Int { 
     // These forced casts are acceptable because 
     // we know `Int` is the only type to conform to`_Int` 
     let lowerBound = self.lowerBound as! Int 
     let upperBound = self.upperBound as! Int 
     let count = self.count as! Int 

     return (lowerBound * count + upperBound * count)/2 
    } 
} 

print((1...100).sum) //5050 

FloatingPoint 또는 Integer 프로토콜도 여기에 유용 할 수 있습니다.

+0

감사합니다. Alexander! 그게 효과가 있었어. – skabob11

1

비슷한 문제가 있었지만 다른 해결책이 작동하지 않았습니다. 이것은 단지

extension CountableClosedRange where Bound == Int { 
... 
} 

를 사용
extension CountableClosedRange where Bound: Integer, Bound == Int { 
... 
} 

했다 (코드 완료)를 코딩에 잘 작동하지만 컴파일하는 동안 분할 오류 (11) 결과.