swift.isOpenSource == true
今回はProtocol Oriented Programmingを紹介する予定でしたが、
2015年12月3日
- [Swift.
org] ‒ a site dedicated to the opensource Swift community - Public source code repositories at [github.
com/ apple] - A new Swift package manager project for easily sharing and building code
- A Swift-native core libraries project with higher-level functionality above the standard library
- Platform support for all Apple platforms as well as Linux
抄訳すると、
オープンソースSwift専用サイト、
[Swift. org] の開設 (図1) 図1 Swift. org GitHubにおけるソースコード公開
(図2) 図2 github. com/ apple Swiftパッケージマネージャープロジェクト開始
Swiftネイティブ標準ライブラリ以上の高機能ライブラリプロジェクト開始
すべてのAppleデバイスに加え、
Linuxのサポート
クリスマスを待たずして、
Swift on Linux:Getting Started
オープンソースとなった意義はこの後じっくり吟味するとして、utils/
をそのまま実行すると、
用意するもの
64-bit版のUbuntu 14.04 LTSまたはUbuntu 15.10
いずれはもっと多くのプラットフォームでサポートさせるはずですが、
clangのアップデート(14.04 LTSのみ)
Ubuntu 15.
$ sudo apt-get install clang-3.6 $ sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-3.6 100 $ sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-3.6 100
ダウンロード
あとは、̃/swift
に置いています。全部で90MB弱。clangなどが含まれていないとはいえ、
$ wget https://swift.org/builds/ubuntu1404/swift-2.2-SNAPSHOT-2015-12-01-b/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gz $ tar zxvf swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gz $ mv swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04 ̃/swift
なお、
REPL
それでは、
$ ̃/swift/usr/bin/swift
と叩けば、
$ export PATH=$HOME/swift/usr/bin:$PATH
などで̃/swift/
をパスに追加しておけば、swift
だけでOKです。
Rubyにおけるirb
や引数なしのpython
を実行したのと同様に、

Tips
このREPL、irb
やインタラクティブモードのpython
と比べると、
関数/メソッド補完
そのひとつは、1.
と打った後で
1> 1. Available completions: advancedBy(n: Distance) -> Int advancedBy(n: Int, limit: Int) -> Int bigEndian: Int byteSwapped: Int description: String distanceTo(other: Int) -> Distance hashValue: Int littleEndian: Int predecessor() -> Int stride(through: Int, by: Distance) -> StrideThrough<Int> stride(to: Int, by: Distance) -> StrideTo<Int> successor() -> Int toIntMax() -> IntMax 1> 1.successor() $R0: Int = 2
...su
と打って.successor()
まで補完してくれます。
ブロック編集のサポート
SwiftのREPLのヒストリーは、
1> (1...10).reduce(0) { 2. $0 + $1 3. } $R0: Int = 55
この状態でreduce(0)
をreduce(1)
に、$0 + $1を$0 * $1
に編集して、}
の後ろまでカーソルを移動してから
4> (1...10).reduce(1) { 5. $0 * $1 6. } $R1: Int = 3628800
となります。ブロックの終了、}
以前にリターンした場合、
import Glibc
Swiftは強力な言語です。しかしPerlやPythonやRubyなどのスクリプト言語と異なり、
7> let pi = -2 * atan2(-1, 0) repl.swift:7:15: error: use of unresolved identifier 'atan2' let pi = -2 * atan2(-1, 0)
新規playgroundで、 これを 公約どおり、 この2つの疑問に対し、 食えなきゃ誰も食ってくれない。オープンでなければ、 WWDC2014におけるデビューからわずか1年半、 Appleプラットフォームとは無縁だった読者も、 2022年8月18日発売import UIKit
、import Cocoa
というimport Glibc
だそうです。連載第7回で筆者が予想したimport POSIX
ではなく。 7> import Glibc
8> let pi = -2 * atan2(-1, 0)
pi: Double = 3.1415926535897931
#if
と組み合わせると、chmod+x
したうえでスクリプトとして実行可能で、swiftc pi.
でコンパイルしても動くことを確認しました。 #!/usr/bin/env swift
#if os(OSX)
import Cocoa
#elseif os(iOS)
import UIKit
#elseif os(Linux)
import Glibc
#endif
let π = -2 * atan2(-1.0, 0.0)
print("π = \(π)")
あらためて、
本誌最新号をチェック!
Software Design 2022年9月号
B5判/
定価1,342円
MySQL アプリ開発者の必修5科目
不意なトラブルに困らないためのRDB基礎知識
OSSソースコードリーディングのススメ
企業のシステムを支えるOSとエコシステムの全貌
今さら聞けないSSH
MySQLで学ぶ文字コード
新生