قالب وردپرس درنا توس
Home / IOS Development / Swift 4.1 – Ole Begemann

Swift 4.1 – Ole Begemann



Swift 4.1 has been out for a month, so I'm late for the party with this, but I want to point out what an important step forward this release is for Swift.

Even if you never write a conditional protocol compliance yourself, having a more powerful type of system is a big deal for the default library and other libraries. Users of these libraries do not need to know or care about conditional compliance – for them it simply means less frustration because more things like unexplained did not work until now, work as expected.

For example, you can now assert that two sets are the same in a unit test:

  la    crossed    =    Set   (  5   ... [1
9659009] 15
) . 10 ) XCTAssertEqual [ [19659000] [[19659009] 5 . < 10 ))

Such has always been. In fact, these things are so fundamental that in a few months we will wonder how we could get some work in a language that did not support the most basic things.

The same applies to the second new generic function, recursive restrictions on associated types. Of course the sequence of a sequence should in itself be a sequence (and have the same element type). However, so far, this restriction can not be expressed in the type system.

For example, this simple method of releasing the first and last element of a sequence will not compile in Swift 4.0:

  extension    Sequence    {
      func    dropFirstAndLast   ( )    ->    SubSequence    {
          // Error: Type Value & # 39; Self.SubSequence & # 39; has no member dropLast 
    ] 
    ] 
] 

  Screenshot of Xcode 9.2 (Swift 4.0) showing a compilation error
Error in Swift 4.0 because the compiler does not know that SubSequence returned at dropFirst is a sequence .

Only when you explicitly limit the extension to sequences whose subsequence is also a sequence and whose underscore subtitle type is the underscore type (another thing that also applies to all sequences, but the compiler did not know it is the compiler satisfied:

  extension    Sequence    where    SubSequence :    Sequence     SubSequence    = =    Subsequent 


Source link