[swift-evolution] Explicity Size Clipping
    James Campbell 
    james at supmenow.com
       
    Wed Jan  6 13:08:06 CST 2016
    
    
  
This is the full code:
//
//  Box.swift
//  Project
//
//  Created by James Campbell on 06/01/2016.
//
//
// This File implements the Box protocol for Type Safe Alogrithm Types and
other useful things.
import Foundation
//MARK:- Box
protocol Box: CustomStringConvertible, CustomDebugStringConvertible {
    typealias FloatLiteralType = Double
    typealias IntegerLiteralType = Int
    typealias BoxType = Any
    var value: BoxType { get set }
    init()
    init(_ value: BoxType)
}
extension Box where BoxType: CustomStringConvertible {
    var description: String {
        return self.value.description
    }
    var debugDescription: String {
        return "\(self.value.description)"
    }
}
//MARK: FloatingPointBox
protocol FloatingPointBox: Box, FloatLiteralConvertible,
IntegerLiteralConvertible {
    typealias BoxType = Double
    typealias FloatLiteralConvertible = Double
    typealias IntegerLiteralConvertible = Int
}
extension Box where Self.BoxType == Double {
    init(_ value: Double) {
        self.init()
        self.value = value
    }
    init(_ value: Int) {
        self.init()
        self.value = Double(value)
    }
    init(_ value: UInt32) {
        self.init()
        self.value = Double(value)
    }
}
extension FloatLiteralType {
    init<T: Box where T.BoxType == Double >(_ box: T) {
        self.init(box.value)
    }
    init<T: Box where T.BoxType == Int >(_ box: T) {
        self.init(box.value)
    }
}
extension CGFloat {
    init<T: Box where T.BoxType == Double >(_ box: T) {
        self.init(box.value)
    }
    init<T: Box where T.BoxType == Int >(_ box: T) {
        self.init(box.value)
    }
}
//Adding FloatLiteralConvertible, IntegerLiteralConvertible
extension FloatingPointBox where Self.BoxType == Double,
Self.FloatLiteralConvertible
== Double {
    init(floatLiteral value: Double) {
        self.init(value)
    }
    init(integerLiteral value: Int) {
        self.init(value)
    }
    init<T: IntegerType>(_ value: T) {
        self.init(value)
    }
}
//
//  Angles.swift
//  Sup
//
//  Created by James Campbell on 22/12/2015.
//  Copyright © 2015 Sup. All rights reserved.
//
// This File defines representations of Degrees and Radians as Type Safe
Alogrithm Types
import Foundation
//MARK:- Degree
struct Degree: FloatingPointBox {
    var value: Double = 0
    init()
    {
    }
}
protocol DegreeConvertiable {
    init(degreeLiteral value: Degree)
}
extension Degree: RadianConvertiable {
    init(radianLiteral value: Radian) {
        self.value = Double(value) * 180.0 / M_PI
    }
    init(_ value: Radian) {
        self.init(radianLiteral: value)
    }
}
//MARK:- Radian
struct Radian: FloatingPointBox {
    var value: Double = 0
    init()
    {
    }
}
protocol RadianConvertiable {
    init(radianLiteral value: Radian)
}
extension Radian: DegreeConvertiable {
    init(degreeLiteral value: Degree) {
        self.value = Double(value) * M_PI / 180.0
    }
    init(_ value: Degree) {
        self.init(degreeLiteral: value)
    }
}
let someOptiona: Degreee? = nil
 let degree = someOptional ?? Degree(arc4random_uniform(360))
On Wed, Jan 6, 2016 at 6:57 PM, Félix Cloutier <felixcca at yahoo.ca> wrote:
> I either don't understand or can't reproduce the issue. This code:
>
> class Box {
> let value: Int
> init(value: Int) {
> self.value = value
> }
> }
>
> let test: UInt32 = 4
> let box = Box(value: test)
>
>
> does not compile ("foo.swift:10:22: error: cannot convert value of type
> 'UInt32' to expected argument type 'Int'").
>
> Félix
>
> Le 6 janv. 2016 à 13:17:03, James Campbell <james at supmenow.com> a écrit :
>
> protocol Box: CustomStringConvertible, CustomDebugStringConvertible {
>
>
>     typealias FloatLiteralType = Double
>
>     typealias IntegerLiteralType = Int
>
>     typealias BoxType = Any
>
>
>     var value: BoxType { get set }
>
>
>     init()
>
>     init(_ value: BoxType)
>
> }
>
> extension Box where Self.BoxType == Double {
>
>
>     init(_ value: Double) {
>
>
>         self.init()
>
>         self.value = value
>
>     }
>
>
>     init(_ value: Int) {
>
>         self.init()
>
>         self.value = Double(value)
>
>     }
>
>
>     init(_ value: UInt32) {
>
>         self.init()
>
>         self.value = Double(value)
>
>     }
>
> }
>
>
>
> Without that last method, Swift tries to give the UInt32 to the Int
> version of the method which isn't safe in my eyes and I would have expected
> a compiler error.
>
> On Wed, Jan 6, 2016 at 6:14 PM, Félix Cloutier <felixcca at yahoo.ca> wrote:
>
>> I don't understand. Can you show a code example?
>>
>> Félix
>>
>> Le 6 janv. 2016 à 13:02:43, James Campbell via swift-evolution <
>> swift-evolution at swift.org> a écrit :
>>
>> I have a overloaded method for a protocol that takes an Int or double.
>>
>> I have an issue with swift calling the Int version when passing in a
>> UInt32 . Ideally I would prefer the compiler to detect I am about to do
>> something which will cause an overflow and throw an error.
>>
>> I would then have to explicitly add a overloaded UInt32 method or
>> explicitly convert it to Int like so "Int(uintVariable)".
>>
>> --
>>  Wizard
>> james at supmenow.com
>> +44 7523 279 698
>>  _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>>
>>
>
>
> --
>  Wizard
> james at supmenow.com
> +44 7523 279 698
>
>
>
-- 
 Wizard
james at supmenow.com
+44 7523 279 698
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160106/0fccde68/attachment.html>
    
    
More information about the swift-evolution
mailing list