@@ -2063,5 +2063,1230 @@ welcome.
2063
2063
var y: NSMatchingOptions = nil
2064
2064
2065
2065
2066
- Release notes for older releases are available at
2067
2066
2067
+ 2014-01-22
2068
+ ----------
2069
+
2070
+ * The swift binary no longer has an SDK set by default. Instead, you must do
2071
+ one of the following:
2072
+ - pass an explicit "-sdk /path/to/sdk"
2073
+ - set SDKROOT in your environment
2074
+ - run swift through xcrun, which sets SDKROOT for you
2075
+
2076
+ * 'let' declarations can now be used as struct/class properties. A 'let'
2077
+ property is mutable within init(), and immutable everywhere else.
2078
+
2079
+ class C {
2080
+ let x = 42
2081
+ let y : Int
2082
+ init(y : Int) {
2083
+ self.y = y // ok, self.y is mutable in init()
2084
+ }
2085
+
2086
+ func test() {
2087
+ y = 42 // error: 'y' isn't mutable
2088
+ }
2089
+ }
2090
+
2091
+ * The immutability model for structs and enums is complete, and arguments are
2092
+ immutable by default. This allows the compiler to reject mutations of
2093
+ temporary objects, catching common bugs. For example, this is rejected:
2094
+
2095
+ func setTo4(a : Double[]) {
2096
+ a[10] = 4.0 // error: 'a' isn't mutable
2097
+ }
2098
+ ...
2099
+ setTo4(someArray)
2100
+
2101
+ since "a" is semantically a copy of the array passed into the function. The
2102
+ proper fix in this case is to mark the argument is @inout, so the effect is
2103
+ visible in the caller:
2104
+
2105
+ func setTo4(a : @inout Double[]) {
2106
+ a[10] = 4.0 // ok: 'a' is a mutable reference
2107
+ }
2108
+ ...
2109
+ setTo4(&someArray)
2110
+
2111
+ Alternatively, if you really just want a local copy of the argument, you can
2112
+ mark it var. The effects aren't visible in the caller, but this can be
2113
+ convenient in some cases:
2114
+
2115
+ func doStringStuff(var s : String) {
2116
+ s += "foo"
2117
+ print(s)
2118
+ }
2119
+
2120
+ * Objective-C instance variables are no longer imported from headers written in
2121
+ Objective-C. Previously, they would appear as properties within the
2122
+ Objective-C class, but trying to access them would result in a crash.
2123
+ Additionally, their names can conflict with property names, which confuses
2124
+ the Swift compiler, and there are no patterns in our frameworks that expect
2125
+ you to access a parent or other class's instance variables directly. Use
2126
+ properties instead.
2127
+
2128
+ * The NSObject protocol is now imported under the name
2129
+ "NSObjectProtocol" (rather than "NSObjectProto").
2130
+
2131
+ 2014-01-15
2132
+ ----------
2133
+
2134
+ * Improved deallocation of Swift classes that inherit from Objective-C
2135
+ classes: Swift destructors are implemented as -dealloc methods that
2136
+ automatically call the superclass's -dealloc. Stored properties are
2137
+ released right before the object is deallocated (using the same
2138
+ mechanism as ARC), allowing properties to be safely used in
2139
+ destructors.
2140
+
2141
+ * Subclasses of NSManagedObject are now required to provide initial
2142
+ values for each of their stored properties. This permits
2143
+ initialization of these stored properties directly after +alloc to
2144
+ provide memory safety with CoreData's dynamic subclassing scheme.
2145
+
2146
+ * 'let' declarations are continuing to make slow progress. Curried
2147
+ and selector-style arguments are now immutable by default, and
2148
+ 'let' declarations now get proper debug information.
2149
+
2150
+ 2014-01-08
2151
+ ----------
2152
+
2153
+ * The "static" keyword changed to "type". One can now define "type
Collapse comment Comment on line R2153
@lattner
Just curious - what was your reasoning behind undoing this change? It seems like this would make a lot of sense semantically..
It's a long and nuanced discussion, please ask on swift-users or something if you're curious.
Code has comments. Press enter to view.
2154
+ functions" and "type variables" which are functions and variables
2155
+ defined on a type (rather than on an instance of the type), e.g.,
2156
+
2157
+ class X {
2158
+ type func factory() -> X { ... }
2159
+
2160
+ type var version: Int
2161
+ }
2162
+
2163
+ The use of "static" was actively misleading, since type methods
2164
+ on classes are dynamically dispatched (the same as Objective-C
2165
+ "+" methods).
2166
+
2167
+ Note that "type" is a context-sensitive keyword; it can still be
2168
+ used as an identifier.
2169
+
2170
+ * Strings have a new native UTF-16 representation that can be
2171
+ converted back and forth to NSString at minimal cost. String
2172
+ literals are emitted as UTF-16 for string types that support it
2173
+ (including Swift's String).
2174
+
2175
+ * Initializers can now delegate to other initializers within the same
2176
+ class by calling 'self.init'. For example:
2177
+
2178
+ class A { }
2179
+
2180
+ class B : A {
2181
+ var title: String
2182
+
2183
+ init() {
2184
+ // note: cannot access self before delegating
2185
+ self.init(withTitle: "My Title")
2186
+ }
2187
+
2188
+ init withTitle(title: String) {
2189
+ self.title = title
2190
+ super.init()
2191
+ }
2192
+ }
2193
+
2194
+
2195
+ * Objective-C protocols no longer have the "Proto" suffix unless there
2196
+ is a collision with a class name. For example, UITableViewDelegate is
2197
+ now imported as "UITableViewDelegate" rather than
2198
+ "UITableViewDelegateProto". Where there is a conflict with a class,
2199
+ the protocol will be suffixed with "Proto", as in "NSObject" (the
2200
+ class) and "NSObjectProto" (the protocol).
2201
+
2202
+ 2014-01-01
2203
+ ----------
2204
+
2205
+ * Happy New Year
2206
+
2207
+ * Division and remainder arithmetic now trap on overflow. Like with the other
2208
+ operators, one can use the "masking" alternatives to get non-trapping
2209
+ behavior. The behavior of the non-trapping masking operators is defined:
2210
+ x &/ 0 == 0
2211
+ x &% 0 == 0
2212
+ SIGNED_MIN_FOR_TYPE &/ -1 == -1 // i.e. Int8: -0x80 / -1 == -0x80
2213
+ SIGNED_MIN_FOR_TYPE &% -1 == 0
2214
+
2215
+ * Protocol conformance checking for @mutating methods is now implemented: an
2216
+ @mutating struct method only fulfills a protocol requirement if the protocol
2217
+ method was itself marked @mutating:
2218
+
2219
+ protocol P {
2220
+ func nonmutating()
2221
+ @mutating
2222
+ func mutating()
2223
+ }
2224
+
2225
+ struct S : P {
2226
+ // Error, @mutating method cannot implement non-@mutating requirement.
2227
+ @mutating
2228
+ func nonmutating() {}
2229
+
2230
+ // Ok, mutating allowed, but not required.
2231
+ func mutating() {}
2232
+ }
2233
+
2234
+ As before, class methods never need to be marked @mutating (and indeed, they
2235
+ aren't allowed to be marked as such).
2236
+
2237
+
2238
+ 2013-12-25
2239
+ ----------
2240
+
2241
+ * Merry Christmas
2242
+
2243
+ * The setters of properties on value types (structs/enums) are now @mutating by
2244
+ default. To mark a setter non-mutating, use the @!mutating attribute.
2245
+
2246
+ * Compiler inserts calls to super.init() into the class initializers that do
2247
+ not call any initializers explicitly.
2248
+
2249
+ * A "map" method with the semantics of Haskell's "fmap" was added to
2250
+ Array<T>. Map applies a function f: T->U to the values stored in
2251
+ the array and returns an Array<U>. So,
2252
+
2253
+ (swift) func names(x: Int[]) -> String[] {
2254
+ return x.map { "<" + String($0) + ">" }
2255
+ }
2256
+ (swift) names(Array<Int>())
2257
+ // r0 : String[] = []
2258
+ (swift) names([3, 5, 7, 9])
2259
+ // r1 : String[] = ["<3>", "<5>", "<7>", "<9>"]
2260
+
2261
+ 2013-12-18
2262
+ ----------
2263
+
2264
+ * Global variables and static properties are now lazily initialized on first
2265
+ use. Where you would use dispatch_once to lazily initialize a singleton
2266
+ object in Objective-C, you can simply declare a global variable with an
2267
+ initializer in Swift. Like dispatch_once, this lazy initialization is thread
2268
+ safe.
2269
+
2270
+ Unlike C++ global variable constructors, Swift global variables and
2271
+ static properties now never emit static constructors (and thereby don't
2272
+ raise build warnings). Also unlike C++, lazy initialization naturally follows
2273
+ dependency order, so global variable initializers that cross module
2274
+ boundaries don't have undefined behavior or fragile link order dependencies.
2275
+
2276
+ * Swift has the start of an immutability model for value types. As part of this,
2277
+ you can now declare immutable value bindings with a new 'let' declaration,
2278
+ which is semantically similar to defining a get-only property:
2279
+
2280
+ let x = foo()
2281
+ print(x) // ok
2282
+ x = bar() // error: cannot modify an immutable value
2283
+ swap(&x, &y) // error: cannot pass an immutable value as @inout parameter
2284
+ x.clear() // error: cannot call mutating method on immutable value
2285
+ getX().clear() // error: cannot mutate a temporary
2286
+
2287
+ In the case of bindings of class type, the bound object itself is still
2288
+ mutable, but you cannot change the binding.
2289
+
2290
+ let r = Rocket()
2291
+ r.blastOff() // Ok, your rocket is mutable.
2292
+ r = Rocket() // error: cannot modify an immutable binding.
2293
+
2294
+ In addition to the 'let' declaration itself, 'self' on classes, and a few
2295
+ other minor things have switched to immutable bindings.
2296
+
2297
+ A pivotal part of this is that methods of value types (structs and enums) need
2298
+ to indicate whether they can mutate self - mutating methods need to be
2299
+ disallowed on let values (and get-only property results, temporaries, etc) but
2300
+ non-mutating methods need to be allowed. The default for a method is that it
2301
+ does not mutate 'self', though you can opt into mutating behavior with a new
2302
+ @mutating attribute:
2303
+
2304
+ struct MyWeirdCounter {
2305
+ var count : Int
2306
+
2307
+ func empty() -> Bool { return count == 0 }
2308
+
2309
+ @mutating
2310
+ func reset() {
2311
+ count = 0
2312
+ }
2313
+ ...
2314
+ }
2315
+
2316
+ let x = MyWeirdCounter()
2317
+ x.empty() // ok
2318
+ x.reset() // error, cannot mutate immutable 'let' value
2319
+
2320
+ One missing piece is that the compiler does not yet reject mutations of self
2321
+ in a method that isn't marked @mutating. That will be coming soon. Related
2322
+ to methods are properties. Getters and setters can be marked mutating as
2323
+ well:
2324
+
2325
+ extension MyWeirdCounter {
2326
+ var myproperty : Int {
2327
+ get:
2328
+ return 42
2329
+
2330
+ @mutating
2331
+ set:
2332
+ count = value*2
2333
+ }
2334
+ }
2335
+
2336
+ The intention is for setters to default to mutating, but this has not been
2337
+ implemented yet. There is more to come here.
2338
+
2339
+ * A "map" method with the semantics of Haskell's "fmap" was added to
2340
+ Optional<T>. Map applies a function f: T->U to any value stored in
2341
+ an Optional<T>, and returns an Optional<U>. So,
2342
+
2343
+ (swift) func nameOf(x: Int?) -> String? {
2344
+ return x.map { "<" + String($0) + ">" }
2345
+ }
2346
+ (swift)
2347
+ (swift) var no = nameOf(.None) // Empty optional in...
2348
+ // no : String? = <unprintable value>
2349
+ (swift) no ? "yes" : "no" // ...empty optional out
2350
+ // r0 : String = "no"
2351
+ (swift)
2352
+ (swift) nameOf(.Some(42)) // Non-empty in
2353
+ // r1 : String? = <unprintable value>
2354
+ (swift) nameOf(.Some(42))! // Non-empty out
2355
+ // r2 : String = "<42>"
2356
+
2357
+ * Cocoa types declared with the NS_OPTIONS macro are now available in Swift.
2358
+ Like NS_ENUM types, their values are automatically shortened based
2359
+ on the common prefix of the value names in Objective-C, and the name can
2360
+ be elided when type context provides it. They can be used in 'if' statements
2361
+ using the '&', '|', '^', and '~' operators as in C:
2362
+
2363
+ var options: NSJSONWritingOptions = .PrettyPrinted
2364
+ if options & .PrettyPrinted {
2365
+ println("pretty-printing enabled")
2366
+ }
2367
+
2368
+ We haven't yet designed a convenient way to author NS_OPTIONS-like types
2369
+ in Swift.
2370
+
2371
+ 2013-12-11
2372
+ ----------
2373
+
2374
+ * Objective-C 'id' is now imported as 'AnyObject' (formerly known as
2375
+ 'DynamicLookup'), Objective-C 'Class' is imported as 'AnyClass'.
2376
+
2377
+ * The casting syntax "x as T" now permits both implicit conversions
2378
+ (in which case it produces a value of type T) and for
2379
+ runtime-checked casts (in which case it produces a value of type T?
2380
+ that will be .Some(casted x) on success and .None on failure). An
2381
+ example:
2382
+
2383
+ func f(x: AnyObject, y: NSControl) {
2384
+ var view = y as NSView // has type 'NSView'
2385
+ var maybeView = x as NSView // has type NSView?
2386
+ }
2387
+
2388
+ * The precedence levels of binary operators has been redefined, with a much
2389
+ simpler model than C's. This is with a goal to define away classes of bugs
2390
+ such as those caught by Clang's -Wparentheses warnings, and to make it
2391
+ actually possible for normal humans to reason about the precedence
2392
+ relationships without having to look them up.
2393
+
2394
+ We ended up with 6 levels, from tightest binding to loosest:
2395
+ exponentiative: <<, >>
2396
+ multiplicative: *, /, %, &
2397
+ additive: +, -, |, ^
2398
+ comparative: ==, !=, <, <=, >=, >
2399
+ conjunctive: &&
2400
+ disjunctive: ||
2401
+
2402
+ * The "Enumerable" protocol has been renamed "Sequence".
2403
+
2404
+ * The "Char" type has been renamed "UnicodeScalar". The preferred
2405
+ unit of string fragments for users is called "Character".
2406
+
2407
+ * Initialization semantics for classes, structs and enums init methods are now
2408
+ properly diagnosed by the compiler. Instance variables now follow the same
2409
+ initialization rules as local variables: they must be defined before use. The
2410
+ initialization model requires that all properties with storage in the current
2411
+ class be initialized before super.init is called (or, in a root class, before
2412
+ any method is called on self, and before the final return).
2413
+
2414
+ For example, this will yield an error:
2415
+
2416
+ class SomeClass : SomeBase {
2417
+ var x : Int
2418
+
2419
+ init() {
2420
+ // error: property 'self.x' not initialized at super.init call
2421
+ super.init()
2422
+ }
2423
+ }
2424
+
2425
+ A simple fix for this is to change the property definition to "var x = 0",
2426
+ or to explicitly assign to it before calling super.init().
2427
+
2428
+ * Relatedly, the compiler now diagnoses incorrect calls to super.init(). It
2429
+ validates that any path through an initializer calls super.init() exactly once,
2430
+ that all ivars are defined before the call to super.init, and that any uses
2431
+ which require the entire object to be initialized come after the super.init
2432
+ call.
2433
+
2434
+ * Type checker performance has improved considerably (but we still
2435
+ have much work to do here).
2436
+
2437
+ 2013-12-04
2438
+ ----------
2439
+
2440
+ * The "slice" versus "array" subtlety is now dead. Slice<T> has been folded
2441
+ into Array<T> and T[] is just sugar for Array<T>.
2442
+
2443
+
2444
+ 2013-11-20
2445
+ ----------
2446
+ * Unreachable code warning has been added:
2447
+
2448
+ var y: Int = 1
2449
+ if y == 1 { // note: condition always evaluates to true
2450
+ return y
2451
+ }
2452
+ return 1 // warning: will never be executed
2453
+
2454
+ * Overflows on integer type conversions are now detected at runtime and, when
2455
+ dealing with constants, at compile time:
2456
+
2457
+ var i: Int = -129
2458
+ var i8 = Int8(i)
2459
+ // error: integer overflows when converted from 'Int' to 'Int8'
2460
+
2461
+ var si = Int8(-1)
2462
+ var ui = UInt8(si)
2463
+ // error: negative integer cannot be converted to unsigned type 'UInt8'
2464
+
2465
+ * 'def' keyword was changed back to 'func'.
2466
+
2467
+ 2013-11-13
2468
+ ----------
2469
+
2470
+ * Objective-C-compatible protocols can now contain optional
2471
+ requirements, indicated by the @optional attribute:
2472
+
2473
+ @class_protocol @objc protocol NSWobbling {
2474
+ @optional def wobble()
2475
+ }
2476
+
2477
+ A class that conforms to the NSWobbling protocol above can (but does
2478
+ not have to) implement wobble. When referring to the 'wobble'
2479
+ method for a value of type NSWobbling (or a value of generic type
2480
+ that is bounded by NSWobbling), the result is an optional value
2481
+ indicating whether the underlying object actually responds to the
2482
+ given selector, using the same mechanism as messaging 'id'. One can
2483
+ use '!' to assume that the method is always there, '?' to chain the
2484
+ optional, or conditional branches to handle each case distinctly:
2485
+
2486
+ def tryToWobble(w : NSWobbling) {
2487
+ w.wobble() // error: cannot call a value of optional type
2488
+ w.wobble!() // okay: calls -wobble, but fails at runtime if not there
2489
+ w.wobble?() // okay: calls -wobble only if it's there, otherwise no-op
2490
+ if w.wobble {
2491
+ // okay: we know -wobble is there
2492
+ } else {
2493
+ // okay: we know -wobble is not there
2494
+ }
2495
+ }
2496
+
2497
+ * Enums from Cocoa that are declared with the NS_ENUM macro are now imported
2498
+ into Swift as Swift enums. Like all Swift enums, the constants of the Cocoa
2499
+ enum are scoped as members of the enum type, so the importer strips off the
2500
+ common prefix of all of the constant names in the enum when forming the Swift
2501
+ interface. For example, this Objective-C declaration:
2502
+
2503
+ typedef NS_ENUM(NSInteger, NSComparisonResult) {
2504
+ NSOrderedAscending,
2505
+ NSOrderedSame,
2506
+ NSOrderedDescending,
2507
+ };
2508
+
2509
+ shows up in Swift as:
2510
+
2511
+ enum NSComparisonResult : Int {
2512
+ case Ascending, Same, Descending
2513
+ }
2514
+
2515
+ The enum cases can then take advantage of type inference from context.
2516
+ In Objective-C, you would write:
2517
+
2518
+ NSNumber *foo = [NSNumber numberWithInt: 1];
2519
+ NSNumber *bar = [NSNumber numberWithInt: 2];
2520
+
2521
+ switch ([foo compare: bar]) {
2522
+ case NSOrderedAscending:
2523
+ NSLog(@"ascending\n");
2524
+ break;
2525
+ case NSOrderedSame:
2526
+ NSLog(@"same\n");
2527
+ break;
2528
+ case NSOrderedDescending:
2529
+ NSLog(@"descending\n");
2530
+ break;
2531
+ }
2532
+
2533
+ In Swift, this becomes:
2534
+
2535
+ var foo: NSNumber = 1
2536
+ var bar: NSNumber = 2
2537
+
2538
+ switch foo.compare(bar) {
2539
+ case .Ascending:
2540
+ println("ascending")
2541
+ case .Same:
2542
+ println("same")
2543
+ case .Descending:
2544
+ println("descending")
2545
+ }
2546
+
2547
+ * Work has begun on implementing static properties. Currently they are supported
2548
+ for nongeneric structs and enums.
2549
+
2550
+ struct Foo {
2551
+ static var foo: Int = 2
2552
+ }
2553
+ enum Bar {
2554
+ static var bar: Int = 3
2555
+ }
2556
+ println(Foo.foo)
2557
+ println(Bar.bar)
2558
+
2559
+ 2013-11-06
2560
+ ----------
2561
+
2562
+ * 'func' keyword was changed to 'def'.
2563
+
2564
+ * Implicit conversions are now allowed from an optional type 'T?' to another
2565
+ optional type 'U?' if 'T' is implicitly convertible to 'U'. For example,
2566
+ optional subclasses convert to their optional base classes:
2567
+
2568
+ class Base {}
2569
+ class Derived : Base {}
2570
+
2571
+ var d: Derived? = Derived()
2572
+ var b: Base? = d
2573
+
2574
+ 2013-10-30
2575
+ ----------
2576
+
2577
+ * Type inference for variables has been improved, allowing any
2578
+ variable to have its type inferred from its initializer, including
2579
+ global and instance variables:
2580
+
2581
+ class MyClass {
2582
+ var size = 0 // inferred to Int
2583
+ }
2584
+
2585
+ var name = "Swift"
2586
+
2587
+ Additionally, the arguments of a generic type can also be inferred
2588
+ from the initializer:
2589
+
2590
+ // infers Dictionary<String, Int>
2591
+ var dict: Dictionary = ["Hello": 1, "World": 2]
2592
+
2593
+
2594
+ 2013-10-23
2595
+ ----------
2596
+
2597
+ * Missing return statement from a non-Void function is diagnosed as an error.
2598
+
2599
+ * Vector<T> has been replaced with Array<T>. This is a complete rewrite to use
2600
+ value-semantics and copy-on-write behavior. The former means that you never
2601
+ need to defensively copy again (or remember to attribute a property as "copy")
2602
+ and the latter yields better performance than defensive copying. Dictionary<T>
2603
+ is next.
2604
+
2605
+ * 'switch' can now pattern-match into structs and classes, using the syntax
2606
+ 'case Type(property1: pattern1, property2: pattern2, ...):'.
2607
+
2608
+ struct Point { var x, y: Double }
2609
+ struct Size { var w, h: Double }
2610
+ struct Rect { var origin: Point; var size: Size }
2611
+
2612
+ var square = Rect(Point(0, 0), Size(10, 10))
2613
+
2614
+ switch square {
2615
+ case Rect(size: Size(w: var w, h: var h)) where w == h:
2616
+ println("square")
2617
+ case Rect(size: Size(w: var w, h: var h)) where w > h:
2618
+ println("long rectangle")
2619
+ default:
2620
+ println("tall rectangle")
2621
+ }
2622
+
2623
+ Currently only stored properties ("ivars" in ObjC terminology) are
2624
+ supported by the implementation.
2625
+
2626
+ * Array and dictionary literals allow an optional trailing comma:
2627
+
2628
+ var a = [ 1, 2, ]
2629
+ var d = [ "a": 1, "b": 2, ]
2630
+
2631
+ 2013-10-16
2632
+ ----------
2633
+ * Unlike in Objective-C, objects of type 'id' in Swift do not
2634
+ implicitly convert to any class type. For example, the following
2635
+ code is ill-formed:
2636
+
2637
+ func getContentViewBounds(window : NSWindow) -> NSRect {
2638
+ var view : NSView = window.contentView() // error: 'id' doesn't implicitly convert to NSView
2639
+ return view.bounds()
2640
+ }
2641
+
2642
+ because contentView() returns an 'id'. One can now use the postfix
2643
+ '!' operator to allow an object of type 'id' to convert to any class
2644
+ type, e.g.,
2645
+
2646
+ func getContentViewBounds(window : NSWindow) -> NSRect {
2647
+ var view : NSView = window.contentView()! // ok: checked conversion to NSView
2648
+ return view.bounds()
2649
+ }
2650
+
2651
+ The conversion is checked at run-time, and the program will fail if
2652
+ the object is not an NSView. This is shorthand for
2653
+
2654
+ var view : NSView = (window.contentView() as NSView)!
2655
+
2656
+ which checks whether the content view is an NSView (via the "as
2657
+ NSView"). That operation returns an optional NSView (written
2658
+ NSView?) and the '!' operation assumes that the cast succeeded,
2659
+ i.e., that the optional has a value in it.
2660
+
2661
+ * The unconditional checked cast syntax 'x as! T' has been removed. Many cases
2662
+ where conversion from 'id' is necessary can now be handled by postfix '!'
2663
+ (see above). Fully general unconditional casts can still be expressed using
2664
+ 'as' and postfix '!' together, '(x as T)!'.
2665
+
2666
+ * The old "square bracket" attribute syntax has been removed.
2667
+
2668
+ * Overflows on construction of integer and floating point values from integer
2669
+ literals that are too large to fit the type are now reported by the compiler.
2670
+ Here are some examples:
2671
+
2672
+ var x = Int8(-129)
2673
+ // error: integer literal overflows when stored into 'Int8'
2674
+
2675
+ var y : Int = 0xFFFF_FFFF_FFFF_FFFF_F
2676
+ // error: integer literal overflows when stored into 'Int'
2677
+
2678
+ Overflows in constant integer expressions are also reported by the compiler.
2679
+
2680
+ var x : Int8 = 125
2681
+ var y : Int8 = x + 125
2682
+ // error: arithmetic operation '125 + 125' (on type 'Int8') results in
2683
+ // an overflow
2684
+
2685
+ * Division by zero in constant expressions is now detected by the compiler:
2686
+
2687
+ var z: Int = 0
2688
+ var x = 5 / z // error: division by zero
2689
+
2690
+ * Generic structs with type parameters as field types are now fully supported.
2691
+
2692
+ struct Pair<T, U> {
2693
+ var first: T
2694
+ var second: U
2695
+ }
2696
+
2697
+ 2013-10-09
2698
+ ----------
2699
+ * Autorelease pools can now be created using the 'autoreleasepool' function.
2700
+
2701
+ autoreleasepool {
2702
+ // code
2703
+ }
2704
+
2705
+ Note that the wrapped code is a closure, so constructs like 'break' and
2706
+ 'continue' and 'return' do not behave as they would inside an Objective-C
2707
+ @autoreleasepool statement.
2708
+
2709
+ * Enums can now declare a "raw type", and cases can declare "raw values",
2710
+ similar to the integer underlying type of C enums:
2711
+
2712
+ // Declare the underlying type as in Objective-C or C++11, with
2713
+ // ': Type'
2714
+ enum AreaCode : Int {
2715
+ // Assign explicit values to cases with '='
2716
+ case SanFrancisco = 415
2717
+ case EastBay = 510
2718
+ case Peninsula = 650
2719
+ case SanJose = 408
2720
+ // Values are also assignable by implicit auto-increment
2721
+ case Galveston // = 409
2722
+ case Baltimore // = 410
2723
+ }
2724
+
2725
+ This introduces 'fromRaw' and 'toRaw' methods on the enum to perform
2726
+ conversions from and to the raw type:
2727
+
2728
+ /* As if declared:
2729
+ extension AreaCode {
2730
+ // Take a raw value, and produce the corresponding enum value,
2731
+ // or None if there is no corresponding enum value
2732
+ static func fromRaw(raw:Int) -> AreaCode?
2733
+
2734
+
2735
+ // Return the corresponding raw value for 'self'
2736
+ func toRaw() -> Int
2737
+ }
2738
+ */
2739
+
2740
+ AreaCode.fromRaw(415) // => .Some(.SanFrancisco)
2741
+ AreaCode.fromRaw(111) // => .None
2742
+ AreaCode.SanJose.toRaw() // => 408
2743
+
2744
+ Raw types are not limited to integer types--they can additionally be
2745
+ character, floating-point, or string values:
2746
+
2747
+ enum State : String {
2748
+ case CA = "California"
2749
+ case OR = "Oregon"
2750
+ case WA = "Washington"
2751
+ }
2752
+
2753
+ enum SquareRootOfInteger : Float {
2754
+ case One = 1.0
2755
+ case Two = 1.414
2756
+ case Three = 1.732
2757
+ case Four = 2.0
2758
+ }
2759
+
2760
+ Raw types are currently limited to simple C-like enums with no payload cases.
2761
+ The raw values are currently restricted to simple literal values; expressions
2762
+ such as "1 + 1" or references to other enum cases are not yet supported.
2763
+ Raw values are also currently required to be unique for each case in an enum.
2764
+
2765
+ Enums with raw types implicitly conform to the 'RawRepresentable' protocol,
2766
+ which exposes the fromRaw and toRaw methods to generics:
2767
+
2768
+ protocol RawRepresentable {
2769
+ typealias RawType
2770
+ static func fromRaw(raw: RawType) -> Self?
2771
+ func toRaw() -> RawType
2772
+ }
2773
+
2774
+ * Attribute syntax has been redesigned (see rdar://10700853 and rdar://14462729)
2775
+ so that attributes now preceed the declaration and use the @ character to
2776
+ signify them. Where before you might have written:
2777
+
2778
+ func [someattribute=42] foo(a : Int) {}
2779
+
2780
+ you now write:
2781
+
2782
+ @someattribute=42
2783
+ func foo(a : Int) {}
2784
+
2785
+ This flows a lot better (attributes don't push the name for declarations away),
2786
+ and means that square brackets are only used for array types, collection
2787
+ literals, and subscripting operations.
2788
+
2789
+ * The 'for' loop now uses the Generator protocol instead of the Enumerator
2790
+ protocol to iterate a sequence. This protocol looks like this:
2791
+
2792
+ protocol Generator {
2793
+ typealias Element
2794
+ func next() -> Element?
2795
+ }
2796
+
2797
+ The single method next() advances the generator and returns an
2798
+ Optional, which is either .Some(value), wrapping the next value out
2799
+ of the underlying sequence, or .None to signal that there are no
2800
+ more elements. This is an improvement over the previous Enumerator
2801
+ protocol because it eliminates the separate isEmpty() query and
2802
+ better reflects the semantics of ephemeral sequences like
2803
+ un-buffered input streams.
2804
+
2805
+ 2013-10-02
2806
+ ----------
2807
+ * The [byref] attribute has been renamed to [inout]. When applied to a logical
2808
+ property, the getter is invoked before a call and the setter is applied to
2809
+ write back the result. "inout" conveys this better and aligns with existing
2810
+ Objective-C practice better.
2811
+
2812
+ * [inout] arguments can now be captured into closures. The semantics of a
2813
+ inout capture are that the captured variable is an independent local variable
2814
+ of the callee, and the inout is updated to contain the value of that local
2815
+ variable at function exit.
2816
+
2817
+ In the common case, most closure arguments do not outlive the duration of
2818
+ their callee, and the observable behavior is unchanged. However, if the
2819
+ captured variable outlives the function, you can observe this. For example,
2820
+ this code:
2821
+
2822
+ func foo(x : [inout] Int) -> () -> Int {
2823
+ func bar() -> Int {
2824
+ x += 1
2825
+ return x
2826
+ }
2827
+ // Call 'bar' once while the inout is active.
2828
+ bar()
2829
+ return bar
2830
+ }
2831
+
2832
+ var x = 219
2833
+ var f = foo(&x)
2834
+ // x is updated to the value of foo's local x at function exit.
2835
+ println("global x = \(x)")
2836
+ // These calls only update the captured local 'x', which is now independent
2837
+ // of the inout parameter.
2838
+ println("local x = \(f())")
2839
+ println("local x = \(f())")
2840
+ println("local x = \(f())")
2841
+
2842
+ println("global x = \(x)")
2843
+
2844
+ will print:
2845
+
2846
+ global x = 220
2847
+ local x = 221
2848
+ local x = 222
2849
+ local x = 223
2850
+ global x = 220
2851
+
2852
+ In no case will you end up with a dangling pointer or other unsafe construct.
2853
+
2854
+ * 'x as T' now performs a checked cast to 'T?', producing '.Some(t)' if the
2855
+ cast succeeds, or '.None' if the cast fails.
2856
+
2857
+ * The ternary expression ('x ? y : z') now requires whitespace between the
2858
+ first expression and the question mark. This permits '?' to be used
2859
+ as a postfix operator.
2860
+
2861
+ * A significant new piece of syntactic sugar has been added to ease working
2862
+ with optional values. The '?' postfix operator is analogous to '!', but
2863
+ instead of asserting on None, it causes all the following postfix
2864
+ operators to get skipped and return None.
2865
+
2866
+ In a sense, this generalizes (and makes explicit) the Objective-C behavior
2867
+ where message sends to nil silently produce the zero value of the result.
2868
+
2869
+ For example, this code
2870
+
2871
+ object?.parent.notifyChildEvent?(object!, .didExplode)
2872
+
2873
+ first checks whether 'object' has a value; if so, it drills to its
2874
+ parent and checks whether that object implements the 'notifyChildEvent'
2875
+ method; if so, it calls that method. (Note that we do not yet have
2876
+ generalized optional methods.)
2877
+
2878
+ This code:
2879
+
2880
+ var titleLength = object?.title.length
2881
+
2882
+ checks whether 'object' has a value and, if so, asks for the length of
2883
+ its title. 'titleLength' wil have type 'Int?', and if 'object' was
2884
+ missing, the variable will be initialized to None.
2885
+
2886
+ * Objects with type 'id' can now be used as the receiver of property
2887
+ accesses and subscript operations to get (but not set) values. The
2888
+ result is of optional type. For example, for a variable 'obj' of
2889
+ type 'id', the expression
2890
+
2891
+ obj[0]
2892
+
2893
+ will produce a value of type 'id', which will either contain the
2894
+ result of the message send objectAtIndexedSubscript(0) (wrapped in an
2895
+ optional type) or, if the object does not respond to
2896
+ objectAtIndexedSubscript:, an empty optional. The same approach
2897
+ applies to property accesses.
2898
+
2899
+ * '_' can now be used not only in 'var' bindings, but in assignments as well,
2900
+ to ignore elements of a tuple assignment, or to explicitly ignore values.
2901
+
2902
+ var a = (1, 2.0, 3)
2903
+ var x = 0, y = 0
2904
+ _ = a // explicitly load and discard 'a'
2905
+ (x, _, y) = a // assign a.0 to x and a.2 to y
2906
+
2907
+ 2013-09-24
2908
+ ----------
2909
+ * The 'union' keyword has been replaced with 'enum'. Unions and enums
2910
+ are semantically identical in swift (the former just has data
2911
+ associated with its discriminators) and 'enum' is the vastly more
2912
+ common case. For more rationale, please see docs/proposals/Enums.rst
2913
+ in the Swift source tree.
2914
+
2915
+ * The Optional type 'T?' is now represented as an enum:
2916
+
2917
+ enum Optional<T> {
2918
+ case None
2919
+ case Some(T)
2920
+ }
2921
+
2922
+ This means that, in addition to the existing Optional APIs, it can be
2923
+ pattern-matched with switch:
2924
+
2925
+ var x : X?, y : Y?
2926
+ switch (x, y) {
2927
+ // Both are present
2928
+ case (.Some(var a), .Some(var b)):
2929
+ println("both")
2930
+
2931
+ // One is present
2932
+ case (.Some, .None):
2933
+ case (.None, .Some):
2934
+ println("one")
2935
+
2936
+ // Neither is present
2937
+ case (.None, .None):
2938
+ println("neither")
2939
+ }
2940
+
2941
+ * Enums now allow multiple cases to be declared in a comma-separated list
2942
+ in a single 'case' declaration:
2943
+
2944
+ enum Color {
2945
+ case Red, Green, Blue
2946
+ }
2947
+
2948
+ * The Objective-C 'id' and 'Class' types now support referring to
2949
+ methods declared in any class or protocol without a downcast. For
2950
+ example, given a variable "sender" of type "id", one can refer to
2951
+ -isEqual: with:
2952
+
2953
+ sender.isEqual
2954
+
2955
+ The actual object may or may not respond to -isEqual, so this
2956
+ expression returns result of optional type whose value is determined via a
2957
+ compiler-generated -respondsToSelector send. When it succeeds, the
2958
+ optional contains the method; when it fails, the optional is empty.
2959
+
2960
+ To safely test the optional, one can use, e.g.,
2961
+
2962
+ var senderIsEqual = sender.isEqual
2963
+ if senderIsEqual {
2964
+ // this will never trigger an "unrecognized selector" failure
2965
+ var equal = senderIsEqual!(other)
2966
+ } else {
2967
+ // sender does not respond to -isEqual:
2968
+ }
2969
+
2970
+ When you *know* that the method is there, you can use postfix '!' to
2971
+ force unwrapping of the optional, e.g.,
2972
+
2973
+ sender.isEqual!(other)
2974
+
2975
+ This will fail at runtime if in fact sender does not respond to -isEqual:.
2976
+ We have some additional syntactic optimizations planned for testing
2977
+ an optional value and handling both the success and failure cases
2978
+ concisely. Watch this space.
2979
+
2980
+ * Weak references now always have optional type. If a weak variable
2981
+ has an explicit type, it must be an optional type:
2982
+
2983
+ var [weak] x : NSObject?
2984
+
2985
+ If the variable is not explicitly typed, its type will still be
2986
+ inferred to be an optional type.
2987
+
2988
+ * There is now an implicit conversion from T to T?.
2989
+
2990
+ 2013-09-17
2991
+ ----------
2992
+ * Constructor syntax has been improved to align better with
2993
+ Objective-C's init methods. The "constructor" keyword has been
2994
+ replaced with "init", and the selector style of declaration used for
2995
+ func declarations is now supported. For example:
2996
+
2997
+ class Y : NSObject {
2998
+ init withInt(i : Int) string(s : String) {
2999
+ super.init() // call superclass initializer
3000
+ }
3001
+ }
3002
+
3003
+ One can use this constructor to create a Y object with, e.g.,
3004
+
3005
+ Y(withInt:17, string:"Hello")
3006
+
3007
+ Additionally, the rules regarding the selector corresponding to such
3008
+ a declaration have been revised. The selector for the above
3009
+ initializer is "initWithInt:string:"; the specific rules are
3010
+ described in the documentation.
3011
+
3012
+ Finally, Swift initializers now introduce Objective-C entry points,
3013
+ so a declaration such as:
3014
+
3015
+ class X : NSObject {
3016
+ init() {
3017
+ super.init()
3018
+ }
3019
+ }
3020
+
3021
+ Overrides NSObject's '-init' method (which it calls first) as well
3022
+ as introducing the 'allocating' entry point so that one can create a
3023
+ new 'X' instance with the syntax 'X()'.
3024
+
3025
+ * Variables in top-level code (i.e. scripts, but not global variables in
3026
+ libraries) that lack an initializer now work just like local variables:
3027
+ they must be explicitly assigned-to sometime before any use, instead of
3028
+ being default constructed. Instance variables are still on the TODO
3029
+ list.
3030
+
3031
+ * Generic unions with a single payload case and any number of empty cases
3032
+ are now implemented, for example:
3033
+
3034
+ union Maybe<T> {
3035
+ case Some(T)
3036
+ case None
3037
+ }
3038
+
3039
+ union Tristate<T> {
3040
+ case Initialized(T)
3041
+ case Initializing
3042
+ case Uninitialized
3043
+ }
3044
+
3045
+ Generic unions with multiple payload cases are still not yet implemented.
3046
+
3047
+ 2013-09-11
3048
+ ----------
3049
+ * The implementation now supports partial application of class and struct
3050
+ methods:
3051
+
3052
+ (swift) class B { func foo() { println("B") } }
3053
+ (swift) class D : B { func foo() { println("D") } }
3054
+ (swift) var foo = B().foo
3055
+ // foo : () -> () = <unprintable value>
3056
+ (swift) foo()
3057
+ B
3058
+ (swift) foo = D().foo
3059
+ (swift) foo()
3060
+ D
3061
+
3062
+ Support for partial application of Objective-C class methods and methods in
3063
+ generic contexts is still incomplete.
3064
+
3065
+ 2013-09-04
3066
+ ----------
3067
+ * Local variable declarations without an initializer are no longer implicitly
3068
+ constructed. The compiler now verifies that they are initialized on all
3069
+ paths leading to a use of the variable. This means that constructs like this
3070
+ are now allowed:
3071
+
3072
+ var p : SomeProtocol
3073
+ if whatever {
3074
+ p = foo()
3075
+ } else {
3076
+ p = bar()
3077
+ }
3078
+
3079
+ where before, the compiler would reject the definition of "p" saying that it
3080
+ needed an initializer expression.
3081
+
3082
+ Since all local variables must be initialized before use, simple things like
3083
+ this are now rejected as well:
3084
+
3085
+ var x : Int
3086
+ print(x)
3087
+
3088
+ The fix is to initialize the value on all paths, or to explicitly default
3089
+ initialize the value in the declaration, e.g. with "var x = 0" or with
3090
+ "var x = Int()" (which works for any default-constructible type).
3091
+
3092
+ * The implementation now supports unions containing protocol types and weak
3093
+ reference types.
3094
+
3095
+ * The type annotation syntax, 'x as T', has been removed from the language.
3096
+ The checked cast operations 'x as! T' and 'x is T' still remain.
3097
+
3098
+ 2013-08-28
3099
+ ----------
3100
+ * 'this' has been renamed to 'self'. Similarly, 'This' has been renamed to
3101
+ 'Self'.
3102
+
3103
+ * Swift now supports unions. Unlike C unions, Swift's 'union' is type-safe
3104
+ and always knows what type it contains at runtime. Union members are labeled
3105
+ using 'case' declarations; each case may have a different set of
3106
+ types or no type:
3107
+
3108
+ union MaybeInt {
3109
+ case Some(Int)
3110
+ case None
3111
+ }
3112
+
3113
+ union HTMLTag {
3114
+ case A(href:String)
3115
+ case IMG(src:String, alt:String)
3116
+ case BR
3117
+ }
3118
+
3119
+ Each 'case' with a type defines a static constructor function for the union
3120
+ type. 'case' declarations without types become static members:
3121
+
3122
+ var br = HTMLTag.BR
3123
+ var a = HTMLTag.A(href:"http://www.apple.com/")
3124
+ // 'HTMLTag' scope deduced for '.IMG' from context
3125
+ var img : HTMLTag = .IMG(src:"http://www.apple.com/mac-pro.png",
3126
+ alt:"The new Mac Pro")
3127
+
3128
+ Cases can be pattern-matched using 'switch':
3129
+
3130
+ switch tag {
3131
+ case .BR:
3132
+ println("<br>")
3133
+ case .IMG(var src, var alt):
3134
+ println("<img src=\"\(escape(src))\" alt=\"\(escape(alt))\">")
3135
+ case .A(var href):
3136
+ println("<a href=\"\(escape(href))\">")
3137
+ }
3138
+
3139
+ Due to implementation limitations, recursive unions are not yet supported.
3140
+
3141
+ * Swift now supports autolinking, so importing frameworks or Swift libraries
3142
+ should no longer require adding linker flags or modifying your project file.
3143
+
3144
+ 2013-08-14
3145
+ ----------
3146
+ * Swift now supports weak references by applying the [weak] attribute to a
3147
+ variable declaration.
3148
+
3149
+ (swift) var x = NSObject()
3150
+ // x : NSObject = <NSObject: 0x7f95d5804690>
3151
+ (swift) var [weak] w = x
3152
+ // w : NSObject = <NSObject: 0x7f95d5804690>
3153
+ (swift) w == nil
3154
+ // r2 : Bool = false
3155
+ (swift) x = NSObject()
3156
+ (swift) w == nil
3157
+ // r3 : Bool = true
3158
+
3159
+ Swift also supports a special form of weak reference, called [unowned], for
3160
+ references that should never be nil but are required to be weak to break
3161
+ cycles, such as parent or sibling references. Accessing an [unowned]
3162
+ reference asserts that the reference is still valid and implicitly promotes
3163
+ the loaded reference to a strong reference, so it does not need to be loaded
3164
+ and checked for nullness before use like a true [weak] reference.
3165
+
3166
+ class Parent {
3167
+ var children : Array<Child>
3168
+
3169
+ func addChild(c:Child) {
3170
+ c.parent = this
3171
+ children.append(c)
3172
+ }
3173
+ }
3174
+
3175
+ class Child {
3176
+ var [unowned] parent : Parent
3177
+ }
3178
+
3179
+ 2013-07-31
3180
+ ----------
3181
+ * Numeric literals can now use underscores as separators. For example:
3182
+
3183
+ var billion = 1_000_000_000
3184
+ var crore = 1_00_00_000
3185
+ var MAXINT = 0x7FFF_FFFF_FFFF_FFFF
3186
+ var SMALLEST_DENORM = 0x0.0000_0000_0000_1p-1022
3187
+
3188
+ * Types conforming to protocols now must always declare the conformance in
3189
+ their inheritance clause.
3190
+
3191
+ * The build process now produces serialized modules for the standard library,
3192
+ greatly improving build times.
3193
+
3194
+ 2013-07-24
3195
+ ----------
3196
+ * Arithmetic operators "+", "-", "*", and "/" on integer types now do
3197
+ overflow checking and trap on overflow. A parallel set of masking operators,
3198
+ "&+", "&-", "&*", and "&/", are defined to perform two's complement wrapping
3199
+ arithmetic for all signed and unsigned integer types.
3200
+
3201
+ * Debugger support. Swift has a "-g" command line switch that turns on
3202
+ debug info for the compiled output. Using the standard lldb debugger
3203
+ this will allow single-stepping through Swift programs, printing
3204
+ backtraces, and navigating through stack frames; all in sync with
3205
+ the corresponding Swift source code. An unmodified lldb cannot
3206
+ inspect any variables.
3207
+
3208
+ Example session:
3209
+ $ echo 'println("Hello World")' >hello.swift
3210
+ $ swift hello.swift -c -g -o hello.o
3211
+ $ ld hello.o "-dynamic" "-arch" "x86_64" "-macosx_version_min" "10.9.0" \
3212
+ -framework Foundation lib/swift/libswift_stdlib_core.dylib \
3213
+ lib/swift/libswift_stdlib_posix.dylib -lSystem -o hello
3214
+ $ lldb hello
3215
+ Current executable set to 'hello' (x86_64).
3216
+ (lldb) b top_level_code
3217
+ Breakpoint 1: where = hello`top_level_code + 26 at hello.swift:1, addre...
3218
+ (lldb) r
3219
+ Process 38592 launched: 'hello' (x86_64)
3220
+ Process 38592 stopped
3221
+ * thread #1: tid = 0x1599fb, 0x0000000100000f2a hello`top_level_code + ...
3222
+ frame #0: 0x0000000100000f2a hello`top_level_code + 26 at hello.shi...
3223
+ -> 1 println("Hello World")
3224
+ (lldb) bt
3225
+ * thread #1: tid = 0x1599fb, 0x0000000100000f2a hello`top_level_code + ...
3226
+ frame #0: 0x0000000100000f2a hello`top_level_code + 26 at hello.shi...
3227
+ frame #1: 0x0000000100000f5c hello`main + 28
3228
+ frame #2: 0x00007fff918605fd libdyld.dylib`start + 1
3229
+ frame #3: 0x00007fff918605fd libdyld.dylib`start + 1
3230
+
3231
+ Also try "s", "n", "up", "down".
3232
+
3233
+ 2013-07-17
3234
+ ----------
3235
+ * Swift now has a 'switch' statement, supporting pattern matching of
3236
+ multiple values with variable bindings, guard expressions, and range
3237
+ comparisons. For example:
3238
+
3239
+ func classifyPoint(point:(Int, Int)) {
3240
+ switch point {
3241
+ case (0, 0):
3242
+ println("origin")
3243
+
3244
+ case (_, 0):
3245
+ println("on the x axis")
3246
+
3247
+ case (0, _):
3248
+ println("on the y axis")
3249
+
3250
+ case (var x, var y) where x == y:
3251
+ println("on the y = x diagonal")
3252
+
3253
+ case (var x, var y) where -x == y:
3254
+ println("on the y = -x diagonal")
3255
+
3256
+ case (-10..10, -10..10):
3257
+ println("close to the origin")
3258
+
3259
+ case (var x, var y):
3260
+ println("length \(sqrt(x*x + y*y))")
3261
+ }
3262
+ }
3263
+
3264
+ 2013-07-10
3265
+ ----------
3266
+ * Swift has a new closure syntax. The new syntax eliminates the use of
3267
+ pipes. Instead, the closure signature is written the same way as a
3268
+ function type and is separated from the body by the "in"
3269
+ keyword. For example:
3270
+
3271
+ sort(fruits) { (lhs : String, rhs : String) -> Bool in
3272
+ return lhs > rhs
3273
+ }
3274
+
3275
+ When the types are omitted, one can also omit the parentheses, e.g.,
3276
+
3277
+ sort(fruits) { lhs, rhs in lhs > rhs }
3278
+
3279
+ Closures with no parameters or that use the anonymous parameters
3280
+ ($0, $1, etc.) don't need the 'in', e.g.,
3281
+
3282
+ sort(fruits) { $0 > $1 }
3283
+
3284
+ * nil can now be used without explicit casting. Previously, 'nil' had
3285
+ type NSObject, so one would have to write (e.g.) 'nil as! NSArray'
3286
+ to create a nil NSArray. Now, 'nil' picks up the type of its
3287
+ context.
3288
+
3289
+ * POSIX.EnvironmentVariables and swift.CommandLineArguments global variables
3290
+ were merged into a swift.Process variable. Now you can access command line
3291
+ arguments with Process.arguments. In order to acces environment variables
3292
+ add "import POSIX" and use Process.environmentVariables.
0 commit comments