Swift 学习之二十:类型强转(+、-、*、/)

OperatorForceTransfer.swift:

//
//  OperatorForceTransfer.swift
//  FormatForceTransfer
//
//  Created by sixiaobo on 14-6-11.
//  Copyright (c) 2014年 com.huangyibiao. All rights reserved.
//

import Foundation

//
// @brief In order to make different types can be operated, 
//        define the below functions.
// @author huangyibiao

///////////////////////////////// + //////////////////////////////
// make Int + Double = Double derectly
@infix func +(lhsParam: Int, rhsParam: Double) -> Double {
    return Double(lhsParam) + rhsParam
}

// make Double + Int = Double derectly
@infix func +(lhsParam: Double, rhsParam: Int) -> Double {
    return lhsParam + Double(rhsParam)
}

// make Int + Float = Float derectly
@infix func +(lhsParam: Int, rhsParam: Float) -> Float {
    return Float(lhsParam) + rhsParam
}

// make Float + Int = Float derectly
@infix func +(lhsParam: Float, rhsParam: Int) -> Float {
    return lhsParam + Float(rhsParam)
}

// make Double + Float = Double derectly
@infix func +(lhsParam: Double, rhsParam: Float) -> Double {
    return lhsParam + Double(rhsParam)
}

// make Float + Double = Double derectly
@infix func +(lhsParam: Float, rhsParam: Double) -> Double {
    return Double(lhsParam) + rhsParam
}


///////////////////////////////// - //////////////////////////////
// make Int - Double = Double derectly
@infix func -(lhsParam: Int, rhsParam: Double) -> Double {
    return Double(lhsParam) - rhsParam
}

// make Double - Int = Double derectly
@infix func -(lhsParam: Double, rhsParam: Int) -> Double {
    return lhsParam - Double(rhsParam)
}

// make Int - Float = Float derectly
@infix func -(lhsParam: Int, rhsParam: Float) -> Float {
    return Float(lhsParam) - rhsParam
}

// make Float - Int = Float derectly
@infix func -(lhsParam: Float, rhsParam: Int) -> Float {
    return lhsParam - Float(rhsParam)
}

// make Double - Float = Double derectly
@infix func -(lhsParam: Double, rhsParam: Float) -> Double {
    return lhsParam - Double(rhsParam)
}

// make Float - Double = Double derectly
@infix func -(lhsParam: Float, rhsParam: Double) -> Double {
    return Float(lhsParam) - rhsParam
}


///////////////////////////////// * //////////////////////////////
// make Int * Double = Double derectly
@infix func *(lhsParam: Int, rhsParam: Double) -> Double {
    return Double(lhsParam) * rhsParam
}

// make Double * Int = Double derectly
@infix func *(lhsParam: Double, rhsParam: Int) -> Double {
    return lhsParam * Double(rhsParam)
}

// make Int * Float = Float derectly
@infix func *(lhsParam: Int, rhsParam: Float) -> Float {
    return Float(lhsParam) * rhsParam
}

// make Float * Int = Float derectly
@infix func *(lhsParam: Float, rhsParam: Int) -> Float {
    return lhsParam * Float(rhsParam)
}

// make Double * Float = Double derectly
@infix func *(lhsParam: Double, rhsParam: Float) -> Double {
    return lhsParam * Double(rhsParam)
}

// make Float * Double = Double derectly
@infix func *(lhsParam: Float, rhsParam: Double) -> Double {
    return Float(lhsParam) * rhsParam
}


///////////////////////////////// / //////////////////////////////
// make Int / Double = Double derectly
@infix func /(lhsParam: Int, rhsParam: Double) -> Double {
    return Double(lhsParam) / rhsParam
}

// make Double * Int = Double derectly
@infix func /(lhsParam: Double, rhsParam: Int) -> Double {
    return lhsParam / Double(rhsParam)
}

// make Int / Float = Float derectly
@infix func /(lhsParam: Int, rhsParam: Float) -> Float {
    return Float(lhsParam) / rhsParam
}

// make Float / Int = Float derectly
@infix func /(lhsParam: Float, rhsParam: Int) -> Float {
    return lhsParam / Float(rhsParam)
}

// make Double / Float = Double derectly
@infix func /(lhsParam: Double, rhsParam: Float) -> Double {
    return lhsParam / Double(rhsParam)
}

// make Float / Double = Double derectly
@infix func /(lhsParam: Float, rhsParam: Double) -> Double {
    return Float(lhsParam) / rhsParam
}









test in main.swift:

import Foundation

println("test begin!")
println(1 + 2.0)
println(2.0 + 1)
println(Float(1.0) + 2.0)
println(1 - 2.0)
println(1.0 - 2)
println(1 * 2.0)
println(1 / 2.0)
println(1.0 / 2)
println(1 / 2.0)
println(1 % 2.0)
println(1.0 % 2)

result:

test begin!
3.0
3.0
3.0
-1.0
-1.0
2.0
0.5
0.5
0.5
1.0
1.0
Program ended with exit code: 0




上一篇:微软开放 Insider Preview 计划供企业试用


下一篇:swift 学习这十八:宏()