2016-09-13 New document that describes the formats used for creating books for Swift Playgrounds
This package includes a sample Xcode project and related tools to aid in creating, debugging, and producing a playground book document compatible with Swift Playgrounds for iPad, including support for all of the features available in Swift Playgrounds 2.1. This package requires a Mac, and supports Xcode 9.3. It may not be compatible with later releases of Xcode.
(edited)import Foundation let calendar = Calendar.init(identifier: Calendar.Identifier.gregorian) DispatchQueue.concurrentPerform(iterations: 1000) { i in let timeZone = TimeZone(secondsFromGMT: 0)! let components: DateComponents = calendar.dateComponents(in: timeZone, from: Date()) }
<stdin>:7:9: warning: immutable value 'components' was never used; consider replacing with '_' or removing it let components: DateComponents = calendar.dateComponents(in: timeZone, from: Date()) ^~~~~~~~~~ _ pure virtual method called pure virtual method called terminate called without an active exception terminate called recursively pure virtual method called terminate called recursively
print(String(UInt64.max) == String(Int64.max))
let data = """ {"data": 9223372036854775808} """ struct Foo: Codable { var data: UInt64 } import Foundation print(try! JSONDecoder().decode(Foo.self, from: data.data(using: .utf8)!))
(edited)./Playground.playground/Resources/
この辺にFrameworkのパスを置ければなんとかならないかなfunc f(_ a: Any) { print(type(of: a)) } func f(_ a: Float) { print(type(of: a)) } f(1)
func f(_ a: Any) { print(type(of: a)) } func f(_ a: Float) { print(type(of: a)) } f(1)
swift-5.1-RELEASE
Int
{}
の補完とかするのがわかりづらそうだった記憶があります。自分で入力してないから、 }
を消しちゃったりインデント破壊しちゃったときに、直し方がわからないみたいな。The code signature contains entitlements.  iCloud entitlements: iCloud store: “BVNY5T73P8.com.apple.Playgrounds”. iCloud containers: “iCloud.com.apple.Playgrounds”. Sandbox entitlements: User-selected files, read/write access: YES. Allow outgoing network connections: YES. Allow incoming network connections: YES. Allow camera access: YES. Application identifier: “BVNY5T73P8.com.apple.PlaygroundsMac”. com.apple.developer.ClassKit-environment: “production”. com.apple.developer.associated-domains: “applinks:developer.apple.com”. com.apple.developer.icloud-services: “CloudDocuments”. com.apple.developer.associated-application-identifier: “BVNY5T73P8.com.apple.Playgrounds”. com.apple.developer.icloud-container-identifiers: “iCloud.com.apple.Playgrounds”. Temporary exception entitlements: files.home-relative-path.read-write: “/Documents/Playgrounds/”. iokit-user-client-class: “AppleMobileFileIntegrityUserClient”. shared-preference.read-only: “com.apple.AssetCacheLocator”. mach-lookup.global-name: “com.apple.rtcreportingd”, “com.apple.AssetCacheLocatorService”, “com.apple.security.syspolicy.exec”. Apple private exception entitlements: tcc.policy-override: YES. amfi.can-load-cdhash: YES. MobileContainerManager.wipeContainer: “com.apple.PlaygroundsMac.ExecutionExtension”. syspolicy.execution-policy-bypass: YES. rtcreportingd: YES. tcc.allow-prompting: “kTCCServiceAll”.
print(1)
swift-5.2-RELEASE
1
protocol Initializable { init() } struct MyStruct: Initializable { init() {} } protocol P { associatedtype Assoc func foo() -> Assoc func bar() -> Assoc } struct S: P { func foo() -> some Initializable { return MyStruct() } func bar() -> Assoc { return Assoc() } }
Assoc
が @returnTypeOf(P.foo())
に推論されてそれが使えるみたいですね。associatedtype が複数箇所で使われてたら some
は使えないとおもってた。@returnTypeOf(P.foo())
fixed opaque type at P.Assoc
みたいな別概念で処理するのかと思ってた@_opaqueReturnTypeOf("$s5test21SV3fooQryF", 0) 🦸
protocol Initializable { init() } struct MyStruct: Initializable { init() {} } protocol P { associatedtype Assoc func foo() -> Assoc func bar() -> Assoc } struct S: P { func foo() -> some Initializable { return MyStruct() } func bar() -> some Initializable { return MyStruct() } }
<stdin>:13:8: error: type 'S' does not conform to protocol 'P' struct S: P { ^ <stdin>:8:18: note: ambiguous inference of associated type 'Assoc': 'some Initializable' (result of 'S.bar()') vs. 'some Initializable' (result of 'S.foo()') associatedtype Assoc ^ <stdin>:15:8: note: matching requirement 'bar()' to this declaration inferred associated type to 'some Initializable' func bar() -> some Initializable { return MyStruct() } ^ <stdin>:14:8: note: matching requirement 'foo()' to this declaration inferred associated type to 'some Initializable' func foo() -> some Initializable { return MyStruct() } ^
@_opaqueReturnTypeOf(...)
の後は無視されるからなんでも良いんですけどね。protocol SomeProtocol {} extension Int: SomeProtocol {} extension SomeProtocol { func toOpaque() -> some SomeProtocol { return 1 } func toProtocol() -> SomeProtocol { return 1 } } extension SomeProtocol { func type() -> String { return "protocol" } } extension Int { func type() -> String { return "int" } } let a = 1.toOpaque() let b = 1.toProtocol() print(type(of: a)) print(type(of: b)) print(a.type()) print(b.type())
Int Int protocol protocol
type
がwitness tableに載っていないのでtypeメソッドの呼び出しが全部静的になってますねーprotocol SomeProtocol { func type() -> String } extension Int: SomeProtocol {} extension SomeProtocol { func toOpaque() -> some SomeProtocol { return 1 } func toProtocol() -> SomeProtocol { return 1 } } extension SomeProtocol { func type() -> String { return "protocol" } } extension Int { func type() -> String { return "int" } } let a = 1.toOpaque() let b = 1.toProtocol() print(type(of: a)) print(type(of: b)) print(a.type()) print(b.type())
Int Int int int
-> some SomeProtocol
と -> SomeProtocol
の戻り値の型は違うはずなのでは?と思ってましたけど違いましたっけ?Opaque Result Typeはどういう動きだったっけ?protocol SomeProtocol {} extension Int: SomeProtocol {} extension Bool: SomeProtocol {}
があるとして、 extension SomeProtocol { func toOpaque() -> some SomeProtocol { return true } func toProtocol() -> SomeProtocol { if [true, false].randomElement()! { return 1 } else { return true } } }
前者は 1
か true
かを場合分けて違う型を返せないんだっけ? (edited)func foo<T: Equatable>(arg1: T, arg2: T)
これでarg1にtrue、arg2に1を入れようとしてるのと同じ-> SomeProtocol
の戻り値って存在型で合ってます?type(of: a)
の出力は Int
だろ type(of: b)
か (edited)a
と b
は型が違いますよね…?print(type(of: Optional(1) as Any))
(edited)func printType<T>(_ v: T) { print(type(of: v)) } protocol P {} extension Int: P {} let a: P = 1 printType(a) print(type(of: a))
protocol SomeProtocol {} extension Int: SomeProtocol {} extension Bool: SomeProtocol {} extension SomeProtocol { func toOpaque() -> some SomeProtocol { return 1 } func toProtocol() -> SomeProtocol { return 1 } } let a = 1.toOpaque() let b = 1.toProtocol() withUnsafeBytes(of: a) { (pointer) in print(pointer.count) } withUnsafeBytes(of: b) { (pointer) in print(pointer.count) }
printType(b)
だとちゃんと SomeProtocol
が出るa.type()
で int
ではなく protocol
が出るのは、単純にコンパイル時に静的ディスパッチでprotocolのメソッドが割り当てられたからですか?protocol P {} extension Int: P {} func fooP(_ a: P) { print(type(of: a)) } func fooT<T>(_ a: T) { print(type(of: a)) } func fooPT(_ a: P) { fooT(a) } fooP(1) fooT(1) fooPT(1)
protocol P { static func foo() } extension P { static func foo() { print("P") } } extension Int: P { static func foo() { print("Int") } } func callFoo<T>(_ v: T) { print("Calling foo for \(type(of: v))") print("T is P ? \(T.self is P.Type)") if let t = type(of: v) as? P.Type { t.foo() } else { print("Failed to cast") } } let a: P = 1 callFoo(a) callFoo(1)
Calling foo for P T is P ? false Failed to cast Calling foo for Int T is P ? true Int
type(of: v)
が P
なのに as? P.Type
が 失敗するの・・・?protocol P {} extension Int: P {} func checkT<T>(_ t: T) { let tIsP1 = type(of: t) == P.self print("tIsP1=\(tIsP1)") let tIsP2 = type(of: t) is P.Type print("tIsP2=\(tIsP2)") } let a: P = 1 checkT(a)
tIsP1=true tIsP2=false
protocol P {} extension Int: P {} print(P.self is P.Type) print(P.self is P.Protocol) print(Int.self is P.Type) print(Int.self is P.Protocol)
(edited)false true true false
stderr:<stdin>:5:14: warning: 'is' test is always true print(P.self is P.Protocol) ^ <stdin>:6:16: warning: 'is' test is always true print(Int.self is P.Type) ^ <stdin>:7:16: warning: cast from 'Int.Type' to unrelated type 'P.Protocol' always fails print(Int.self is P.Protocol) ~~~~~~~~ ^ ~~~~~~~~~~
(edited)is P.Type
はその型の値が existential P に代入できる型かどうかを示しているんじゃないか? existential P それ自体は conform P していないから、 P に代入できないis P.Protocol
Int.self is P.Type
で false
だよなP.Protocol
って記法、最近見つけて、いつからあったのか、どういう意味なのか、わからないままだった。