昨天swift開源了,喜大淚奔的好消息!
swift的官方網站https://swift.org
swift在github的開源地址https://github.com/apple/swift
今天早上J君問我,swift開源了有什麼好處呢?
我想從以下的幾個方面來回答他:
學習swift的時候,遇到問題,或者有一些想法的時候,你可以打開swift的源碼參考一番,我相信會有很大的收獲。
舉個例子,我們來看看swift中的Bool類型的定義和實現,是不是對你自定義一個類型的時候很有幫助呢?
public struct Bool {
internal var _value: Builtin.Int1
/// Default-initialize Boolean value to `false`.
@_transparent
public init() {
let zero: Int8 = 0
self._value = Builtin.trunc_Int8_Int1(zero._value)
}
@_transparent
internal init(_ v: Builtin.Int1) { self._value = v }
}
extension Bool : _BuiltinBooleanLiteralConvertible, BooleanLiteralConvertible {
@_transparent
public init(_builtinBooleanLiteral value: Builtin.Int1) {
self._value = value
}
/// Create an instance initialized to `value`.
@_transparent
public init(booleanLiteral value: Bool) {
self = value
}
}
extension Bool : BooleanType {
@_transparent
@warn_unused_result
public func _getBuiltinLogicValue() -> Builtin.Int1 {
return _value
}
/// Identical to `self`.
@_transparent public var boolValue: Bool { return self }
/// Construct an instance representing the same logical value as
/// `value`.
public init<T : BooleanType>(_ value: T) {
self = value.boolValue
}
}
extension Bool : CustomStringConvertible {
/// A textual representation of `self`.
public var description: String {
return self ? "true" : "false"
}
}
// This is a magic entrypoint known to the compiler.
@_transparent
public // COMPILER_INTRINSIC
func _getBool(v: Builtin.Int1) -> Bool { return Bool(v) }
@_transparent
extension Bool : Equatable, Hashable {
/// The hash value.
///
/// **Axiom:** `x == y` implies `x.hashValue == y.hashValue`.
///
/// - Note: the hash value is not guaranteed to be stable across
/// different invocations of the same program. Do not persist the
/// hash value across program runs.
public var hashValue: Int {
return self ? 1 : 0
}
}
//===----------------------------------------------------------------------===//
// Operators
//===----------------------------------------------------------------------===//
// Unary logical complement.
@_transparent
@warn_unused_result
public prefix func !(a: Bool) -> Bool {
return Bool(Builtin.xor_Int1(a._value, true._value))
}
@_transparent
@warn_unused_result
public func ==(lhs: Bool, rhs: Bool) -> Bool {
return Bool(Builtin.cmp_eq_Int1(lhs._value, rhs._value))
}
swift開源不到一天的時間,swift項目在github收到了13087個star,1351個fork。並且還在快速增長中......
這表示了眾多開發者對swift這個語言的關注和熱情十分的高漲,並且全球的開發者都會為swift貢獻自己的代碼和力量。大家看下圖體驗一下:
目前swift支持的平台,除了自家的iOS, OS X, watchOS, 和 tvOS.還支持了Linux平台。
New Platforms
We can’t wait to see the new places we can bring Swift—together. We truly believe that this language that we love can make software safer, faster, and easier to maintain. We’d love your help to bring Swift to even more computing platforms.
蘋果公司是支持大家把swift移植到別的平台去的,日後必定有有能力的開發者,在別的新的平台上使用swift。