Upgrade to Pro — share decks privately, control downloads, hide ads and more …

swift_and_oss

 swift_and_oss

to4iki

May 15, 2016
Tweet

More Decks by to4iki

Other Decks in Programming

Transcript

  1. // header file(main.h) @interface MyObject : NSObject { int val;

    id obj; } + (void)classMethod:(id)arg; // class method - (id)method:(NSObject*)arg1 with:(int)arg2; // instance method @end // implement file(main.m) @implementation MyObject + (void)classMethod:(id)arg {} - (id)method:(NSObject*)arg1 with:(int)args2 { return obj; } - (id)init {} - (void)dealloc sample
  2. The Swift language is… it also greatly benefited from the

    experiences hard-won by many other languages in the field, drawing ideas from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list. http://nondot.org/sabre/
  3. Swiftͷಛ௃ • ύοέʔδ؅ཧγεςϜ cocoapods/carthage • Objective-Cͱͷ૬ޓޓ׵ Bridging-Header.h • LazyίϨΫγϣϯ •

    ৄ͍͠ݴޠ࢓༷ͳͲ͸: - https://github.com/hatena/Hatena-Textbook/blob/master/swift-programming-language.md
  4. sample final class MyObject: NSObject { let val: Int init(val:

    Int) { self.val = val } func method() {} static func classMethod() {} } let obj = MyObject(val: 1) obj.method() MyObject.classMethod()
  5. ؔ਺ܕͷτϨϯυ • ৽͍͠ݴޠʹ͸ؔ਺ܕͷΤοηϯεؚ͕·Ε͍ͯΔ • Scala, Elixir, Clojure, Rust, Swift.. •

    طଘͷݴޠʹ΋Өڹ • Java8ͷOption/ ES2015(Babel) • ݴޠͷύϥμΠϜ͕มΘΖ͏ͱ͍ͯ͠Δ/τϨϯυ͕Ҡ͍ͬͯΔ • ͦͷลͷٕज़ʹೃછΜͰ͓͘ͱ૯߹తͳֶशίετΛ཈͑Δ͜ͱ͕ग़དྷΔ • ৽͍͠middleware/FWɺreactive΍streamΈ͍ͨͳύϥμΠϜ • ৄ͘͠͸: http://postd.cc/functional-programming-should-be-your-1-priority-for-2015/
  6. immutability let str = "immutable" // letͰෆม(immutable)ͳม਺એݴ // classͷΦϒδΣΫτͷ୅ೖ͸ࢀর౉͠(෭࡞༻͕͋Δ) class

    User { var input = 0 } let a = User() let b = a a.input = 1 // input: 2 b.input = 2 // input: 2 // struct͸஋౉͠(copyΛऔ͍ͬͯΔ) struct User2 { var input = 0 } var c = User2() var d = c c.input = 1 // input: 1 d.input = 2 // input: 2
  7. immutability • ग़དྷΔ͚ͩ࠶୅ೖΛগͳͯ͘͠ϓϩάϥϛϯά͕Մೳ - ඪ४APIͷॆ࣮ // case-1 ෭࡞༻ let ary

    = Array(1...10) var res = 0 for x in ary { if x % 2 == 0 { res += x * 2 } } res // 60 // case-2 Array(1...10) .filter({ x in x % 2 == 0 }) .map({ x in x * 2 }) .reduce(0, combine: { acc, x in acc + x }) // 60
  8. higher-order function // Int -> Stringͳؔ਺Λม਺ʹ୅ೖͰ͖Δ let doubleStr: Int ->

    String = { (i: Int) -> String in "\(i * 2)" } // Ҿ਺ʹؔ਺ΛऔΔࣄ͕ग़དྷΔ func map2<A, B>(xs: [A], f: A -> B) -> [B] { return xs.reduce([]) { (acc: [B], x: A) in acc + [f(x)] } } map2([1,2,3], f: doubleStr) // [2,4,6] map2(Array(1...3)) { $0 * 2 } // [2,4,6] • Ҿ਺ʹؔ਺Λ౉ͯ͠ॲཧΛҕৡ
  9. pattern match // ex1 let HTTP_STATUS = 200 switch HTTP_STATUS

    { case 200...299: "success" default: "failure" } // ex2 enum StringOptional { case None case Some(String) } let opt = StringOptional.Some("something") // Some("something") switch opt { case let .Some(val): val // something case .None: "nothing" } • ύλʔϯϚονͱ͸ɺ͋Δ஋Λͦͷܕͷ࣋ͪ͏Δߏ଄ʹΑͬͯ෦෼త͋Δ ͍͸શମతʹ۩ମԽ͠ɺ಺แ͢Δ஋΁ͷ෼ղΛͨ͠ΓɺҰக۩߹Λ৚݅ ࣜͱͯ͠ར༻͢Δૢ࡞ͷ͜ͱΛࢦ͢
  10. SwiftͷOption? ~ Optional chaining class Pet { let name: String?

    } class User { let pet: Pet? } func findById(id: Int) -> User? { // User or nil } // Optional chaining let name: String? = findById(1)?.pet?.name // -> Some("taro") or nil name! // taro name ?? "noname" // taro • Option<A>Ҏ֎ͷܕʹରͯͦ͠΋ͦ΋null୅ೖ͸ग़དྷͳ͍
  11. SwiftͷOption? let name1: String? = findById(1)?.pet?.name // -> Some("taro") let

    name2: String? = findById(2)?.pet?.name // -> Some("jiro") // if let unwrapVal = Optional.Some(val) {} if let unwrapName1 = name1, let unwrapName2 = name2 { unwrapName1 + " and "+ unwrapName2 // -> taro and jiro } • if let ʹΑΔpattern matchingͰSome஋ΛऔΓग़͢
  12. extensionͰػೳ֦ு extension String { func potato() -> String { return

    "\(self) Ά͍ͯͱ" } } "৯΂͍ͨ".potato() // ৯΂͍ͨ Ά͍ͯͱ • खܰʹطଘΫϥε΁ͷmethod௥ՃͳͲ͕ग़དྷΔ
  13. protocolͷdefault࣮૷Λॻ͚Δ struct Umaru: PotatoProtocol {} protocol PotatoProtocol { func umaruun()

    } // default࣮૷ΛUmaruܕʹߜ͍ͬͯΔ extension PotatoProtocol where Self: Umaru { func umaruun() { print("͏·ΔʙΜ") } } Umaru().umaruun() // ͏·ΔʙΜ //struct Chibana: PotatoProtocol {} // compile err • interfaceͷσϑΥϧτ࣮૷Λ෇༩Ͱ͖Δ(++ର৅ܕͷ੍໿)
  14. property observer struct Umaru { var cokeCount: Int = 0

    { willSet { // propertyʹ஋Λset͢Δલʹ࣮ߦ͞ΕΔ print("coke͕\(cokeCount)ݸ͔Β\(newValue)ݸʹͳΓ·͢") } didSet { // propertyʹ஋Λsetͨ͠ޙʹ࣮ߦ͞ΕΔ if oldValue != cokeCount { print("coke͕\(cokeCount)ݸʹͳΓ·ͨ͠") } } } } var umr = Umaru() umr.cokeCount = 1 // coke͕0ݸ͔Β1ݸʹͳΓ·͢ // coke͕1ݸʹͳΓ·ͨ͠ • see also: http://www.slideshare.net/hasegawatomoki/swift-45247767
  15. SwiftͷOSSԽ • ݩʑ2015 WWDCͰΞφ΢ϯε • OSX + LinuxͰͷಈ࡞αϙʔτɺcore API /

    swift compilerެ։ • Apache License 2.0 • GitHub: https://github.com/apple/swift
  16. Repository • swift: ຊମ • swift-package-manager: ύοέʔδϚωδϟʔ • swift-corelibs-foundation: Foundation/CoreFoundation

    • swift-llbuild: ϏϧυγεςϜ • swift-evolution: ࣍ظversion΁޲͚ͨϩʔυϚοϓͳͲ
  17. Linux porting • Objective-Cʹґଘ͠ͳ͍(❌NS~) • package manager/LLDB debugger/REPL • server-side-swift

    / OSXҎ֎ͷiOS։ൃ΋Ͱ͖Δ͔΋ • Ubuntu 15.10~޲͚ʹެ։ • tarballΛల։ → PATHΛ௨͚ͩ͢Ͱ͙͢࢖͑Δ • ެ։͞Ε͍ͯΔͷ͸ඪ४APIͷΈ http://gihyo.jp/admin/serial/01/ubuntu-recipe/0401
  18. Contributing • Bug Report • Answering Questions → mailing list

    • Incremental Development • Code Template → API Design Guidelines • Code Review • Commit Messages
  19. ఏڙऀࢹ఺ • ίϛϡχςΟ͔Βͷ੒ՌΛऔΓೖΕΒΕΔΑ͏ʹͳΔ • → ։ൃ͕εϐʔυΞοϓ͢Δ • → ߴ඼࣭Խʹͭͳ͕ΔՄೳੑ •

    ։ൃͷܧଓੑ • ΍Γͨ͘ͳ͍ࣄΛ΍ͬͯ͘ΕΔ • ιϑτ΢ΣΞͷࢥ૝ΛकΓଓ͚ͳ͚Ε͹