SlideShare a Scribd company logo
1 of 40
Download to read offline
Marco Eidinger, Nov 12th 2022
A tour through Swift attributes
Highlight: System Programming Interfaces (experimental)
About me
https://blog.eidinger.info/
• Developing iOS apps and frameworks
for over a decade at my day job.
• Personally, I contribute to the iOS
community with open-source tools
like SwiftPlantUML and
XCSnippetsApp.
• I love to write and share my
programming knowledge on my blog
SwiftyTech (+ 100 articles).
• Find me on on GitHub and Twitter as
@MarcoEidinger.
Attributes
Non-exclusive list of o
ffi
cial attributes
Attributes
Non-exclusive list of o
ffi
cial attributes
@autoclosure
@escaping
@frozen
@main
@globalActor
@inlinable
@available
@Sendable
@preconcurrency
@warn_unqualified_access
@testable
@propertyWrapper
@dynamicMemberLookup
@nonobjc
@objc
@objcMembers
We use Swift attributes all the time
import SwiftUI
struct ExampleAppApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
We use Swift attributes all the time
import SwiftUI
@main
struct ExampleAppApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
SwiftUI framework
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
extension App {
/// Initializes and runs the app.
///
/// If you precede your ``SwiftUI/App`` conformer's declaration with the
/// @main attribute, the system calls the conformer's `main()`
/// method to launch the app.
/// SwiftUI provides a default implementation of the method
/// that manages the launch process in a platform-appropriate way.
@MainActor public static func main()
}
Attributes
Part of the Swift programming language
Two kinds of attributes
apply to declarations vs. types
@frozen public enum Exhaustive {}
import SwiftUI
struct LazyView<Content: View>: View {
let build: () -> Content
init(_ build: @autoclosure @escaping () -> Content) {
self.build = build
}
var body: Content {
self.build()
}
}
Attributes are essential building blocks
Example: SwiftUI
import SwiftUI
struct LocalizedView: View {
var counter = 0
var body: some View {
Button("Increase counter") {
counter += 1
}
}
}
Attributes are essential building blocks
Example: SwiftUI
import SwiftUI
struct LocalizedView: View {
@State var counter = 0
var body: some View {
Button("Increase counter") {
counter += 1
}
}
}
Attributes are essential building blocks
Example: SwiftUI
import SwiftUI
struct LocalizedView: View {
@State var counter = 0
var body: some View {
Button("Increase counter") {
counter += 1
}
}
}
@frozen @propertyWrapper public struct State<Value> : DynamicProperty {…}
Attributes are essential building blocks
Example: Concurrency
@MainActor class ViewModel: ObservableObject {}
Attributes are essential building blocks
Example: Concurrency
@globalActor final public actor MainActor : GlobalActor {…}
@MainActor class ViewModel: ObservableObject {}
Attributes are essential building blocks
Example: Concurrency
@globalActor final public actor MainActor : GlobalActor {…}
@MainActor class ViewModel: ObservableObject {}
extension MainActor {
public static func run<T>(
resultType: T.Type = T.self,
body: @MainActor @Sendable () throws -> T) async rethrows -> T where T : Sendable
}
Interesting use cases
Extensively used in SwiftUI (but not limited to SwiftUI)
@warn_unquali
fi
ed_access
Interesting use cases
Extensively used in SwiftUI (but not limited to SwiftUI)
• Use @warn_unquali
fi
ed_access to make your SwiftUI view modi
fi
ers safer.
Interesting use cases
Extensively used in SwiftUI (but not limited to SwiftUI)
• Use @warn_unquali
fi
ed_access to make your SwiftUI view modi
fi
ers safer.
@resultBuilder
Interesting use cases
Extensively used in SwiftUI (but not limited to SwiftUI)
• Use @warn_unquali
fi
ed_access to make your SwiftUI view modi
fi
ers safer.
• Use @resultBuilder to create cool DSLs.
Interesting use cases
Extensively used in SwiftUI (but not limited to SwiftUI)
• Use @warn_unquali
fi
ed_access to make your SwiftUI view modi
fi
ers safer.
• Use @resultBuilder to create cool DSLs.
@propertyWrappers
Interesting use cases
Extensively used in SwiftUI (but not limited to SwiftUI)
• Use @warn_unquali
fi
ed_access to make your SwiftUI view modi
fi
ers safer.
• Use @resultBuilder to create cool DSLs.
• Use @propertyWrappers to create reusable property implementation
patterns. Property wrappers in the wild.
Interesting use cases
Extensively used in SwiftUI (but not limited to SwiftUI)
• Use @warn_unquali
fi
ed_access to make your SwiftUI view modi
fi
ers safer.
• Use @resultBuilder to create cool DSLs.
• Use @propertyWrappers to create reusable property implementation
patterns. Property wrappers in the wild.
@_alwaysEmitIntoClient
Interesting use cases
Extensively used in SwiftUI (but not limited to SwiftUI)
• Use @warn_unquali
fi
ed_access to make your SwiftUI view modi
fi
ers safer.
• Use @resultBuilder to create cool DSLs.
• Use @propertyWrappers to create reusable property implementation
patterns. Property wrappers in the wild.
• SwiftUI uses @_alwaysEmitIntoClient to back port new features
https://docs.swift.org/swift-book/ReferenceManual/Attributes.html
Stable
Code is better than documentation
• Swift 5.7
• https://github.com/apple/swift/blob/release/5.7/include/swift/AST/Attr.def
• Swift 5.8 and beyond
• Code Ownership of Swift Attributes to swift-syntax
• Replace Attr.def with a gyb
fi
le that reads from swift-syntax to automatically
generate the attribute nodes.
• https://github.com/apple/swift-syntax/blob/main/gyb_syntax_support/
AttributeKinds.py
Definition of Declaration Attributes
https://github.com/apple/swift-syntax/blob/main/gyb_syntax_support/AttributeKinds.py
Tests reveal specification
Example: https://github.com/apple/swift/blob/main/test/attr/warn_unquali
fi
ed_access.swift
https://github.com/apple/swift/blob/release/5.7/docs/ReferenceGuides/UnderscoredAttributes.md
Experimental
Warning: Apple discourages
using underscored attributes
those semantics are subject to change and most likely need to go through the Swift evolution process before being stabilized.
Early adoption still possible
Successful transition from experimental to proper language feature
@resultBuilder
@_functionBuilder
Swift 5.1 Swift 5.4
System Programming Interface
https://blog.eidinger.info/system-programming-interfaces-spi-in-swift-explained
System Programming Interface
Experimental @_spi and related attributes
Attribute
Introduced
in
@_spi Swift 5.3
@_spi_available(platform, version) Swift 5.7
@_spiOnly master (~ Swift 5.8)
System Programming Interface
Experimental @_spi
Shopping.swiftinterfaces
// swift-interface-format-version: 1.0
// swift-compiler-version: Apple Swift version 5.7 (swiftlang-5.7.0.127.4
clang-1400.0.29.50)
// swift-module-flags: -target arm64-apple-macosx10.13 -enable-objc-interop
-enable-library-evolution -swift-version 5 -Onone -module-name Shopping
import Swift
import _Concurrency
public struct ShoppingCartItem {
public init()
}
public struct ShoppingCart {
public init()
public func payCash()
}
Shopping.private.swiftinterfaces
// swift-interface-format-version: 1.0
// swift-compiler-version: Apple Swift version 5.7 (swiftlang-5.7.0.127.4
clang-1400.0.29.50)
// swift-module-flags: -target arm64-apple-macosx10.13 -enable-objc-interop
-enable-library-evolution -swift-version 5 -Onone -module-name Shopping
import Swift
import _Concurrency
public struct ShoppingCartItem {
public init()
}
public struct ShoppingCart {
public init()
public func payCash()
@_spi(PayPal) public func payWithPayPal()
}
@_spi_available(platform, version)
// Module "Shopping"
public struct ShoppingCart {
public init() {}
@_spi_available(watchOS 9, *)
@available(tvOS, unavailable)
public private(set) var items = [ShoppingCartItem]()
public func payCash() {}
@_spi(PayPal) public func payWithPayPal() {}
@_spi(Bitcoin) public func payWithBitcoin() {}
}
// swift-interface-format-version: 1.0
// swift-compiler-version: Apple Swift version 5.7
// swift-module-flags: -target arm64-apple-macosx10
version 5 -Onone -module-name Shopping
import Swift
import _Concurrency
public struct ShoppingCartItem {
public init()
}
public struct ShoppingCart {
public init()
@available(watchOS, unavailable)
@available(tvOS, unavailable)
public var items: [Shopping.ShoppingCartItem] {
get
}
public func payCash()
}
@_spiOnly
Usage and Prerequisites
@_spiOnly
Impact to .swiftinterfaces
// swift-interface-format-version: 1.0
// swift-compiler-version: Apple Swift version 5.8-dev (LLVM 338153f292479fa, Swift b752d7883c26cea)
// swift-module-flags: -target arm64-apple-macosx10.13 -enable-objc-interop -enable-library-evolution -swift-
version 5 -Onone -module-name Shopping
import Swift
import _Concurrency
import _StringProcessing
// swift-interface-format-version: 1.0
// swift-compiler-version: Apple Swift version 5.8-dev (LLVM 338153f292479fa, Swift b752d7883c26cea)
// swift-module-flags: -target arm64-apple-macosx10.13 -enable-objc-interop -enable-library-evolution -swift-
version 5 -Onone -module-name Shopping
/*@_spiOnly*/ import CryptoKit
import Swift
import _Concurrency
import _StringProcessing
public struct ShoppingCartItem {
Thank you!

More Related Content

Similar to A tour through Swift attributes

Baruco 2014 - Rubymotion Workshop
Baruco 2014 - Rubymotion WorkshopBaruco 2014 - Rubymotion Workshop
Baruco 2014 - Rubymotion WorkshopBrian Sam-Bodden
 
Refactoring Large Web Applications with Backbone.js
Refactoring Large Web Applications with Backbone.jsRefactoring Large Web Applications with Backbone.js
Refactoring Large Web Applications with Backbone.jsStacy London
 
Refactor Large applications with Backbone
Refactor Large applications with BackboneRefactor Large applications with Backbone
Refactor Large applications with BackboneColdFusionConference
 
Refactor Large apps with Backbone
Refactor Large apps with BackboneRefactor Large apps with Backbone
Refactor Large apps with BackbonedevObjective
 
Itb 2021 - Bulding Quick APIs by Gavin Pickin
Itb 2021 - Bulding Quick APIs by Gavin PickinItb 2021 - Bulding Quick APIs by Gavin Pickin
Itb 2021 - Bulding Quick APIs by Gavin PickinGavin Pickin
 
Cross Platform Mobile Development using Flutter by Wei Meng Lee at Mobile foc...
Cross Platform Mobile Development using Flutter by Wei Meng Lee at Mobile foc...Cross Platform Mobile Development using Flutter by Wei Meng Lee at Mobile foc...
Cross Platform Mobile Development using Flutter by Wei Meng Lee at Mobile foc...DevClub_lv
 
Advanced #6 clean architecture
Advanced #6  clean architectureAdvanced #6  clean architecture
Advanced #6 clean architectureVitali Pekelis
 
Building Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in RailsBuilding Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in RailsJim Jeffers
 
Telerik AppBuilder Presentation for TelerikNEXT Conference
Telerik AppBuilder Presentation for TelerikNEXT ConferenceTelerik AppBuilder Presentation for TelerikNEXT Conference
Telerik AppBuilder Presentation for TelerikNEXT ConferenceJen Looper
 
SharePoint Fest Seattle - SharePoint Framework, Angular & Azure Functions
SharePoint Fest Seattle - SharePoint Framework, Angular & Azure FunctionsSharePoint Fest Seattle - SharePoint Framework, Angular & Azure Functions
SharePoint Fest Seattle - SharePoint Framework, Angular & Azure FunctionsSébastien Levert
 
Spring Boot Workshop - January w/ Women Who Code
Spring Boot Workshop - January w/ Women Who CodeSpring Boot Workshop - January w/ Women Who Code
Spring Boot Workshop - January w/ Women Who CodePurnima Kamath
 
China Science Challenge
China Science ChallengeChina Science Challenge
China Science Challengeremko caprio
 
SgCodeJam24 Workshop
SgCodeJam24 WorkshopSgCodeJam24 Workshop
SgCodeJam24 Workshopremko caprio
 
Understanding iOS from an Android perspective
Understanding iOS from an Android perspectiveUnderstanding iOS from an Android perspective
Understanding iOS from an Android perspectiveLauren Yew
 
Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]GDSC UofT Mississauga
 
Prototyping applications with heroku and elasticsearch
 Prototyping applications with heroku and elasticsearch Prototyping applications with heroku and elasticsearch
Prototyping applications with heroku and elasticsearchprotofy
 
OpenWhisk Under the Hood -- London Oct 16 2016
OpenWhisk Under the Hood -- London Oct 16 2016OpenWhisk Under the Hood -- London Oct 16 2016
OpenWhisk Under the Hood -- London Oct 16 2016Stephen Fink
 
Apic dc api deep dive
Apic dc api deep dive Apic dc api deep dive
Apic dc api deep dive Cisco DevNet
 

Similar to A tour through Swift attributes (20)

Spring boot
Spring bootSpring boot
Spring boot
 
Baruco 2014 - Rubymotion Workshop
Baruco 2014 - Rubymotion WorkshopBaruco 2014 - Rubymotion Workshop
Baruco 2014 - Rubymotion Workshop
 
Refactoring Large Web Applications with Backbone.js
Refactoring Large Web Applications with Backbone.jsRefactoring Large Web Applications with Backbone.js
Refactoring Large Web Applications with Backbone.js
 
Refactor Large applications with Backbone
Refactor Large applications with BackboneRefactor Large applications with Backbone
Refactor Large applications with Backbone
 
Refactor Large apps with Backbone
Refactor Large apps with BackboneRefactor Large apps with Backbone
Refactor Large apps with Backbone
 
Itb 2021 - Bulding Quick APIs by Gavin Pickin
Itb 2021 - Bulding Quick APIs by Gavin PickinItb 2021 - Bulding Quick APIs by Gavin Pickin
Itb 2021 - Bulding Quick APIs by Gavin Pickin
 
Cross Platform Mobile Development using Flutter by Wei Meng Lee at Mobile foc...
Cross Platform Mobile Development using Flutter by Wei Meng Lee at Mobile foc...Cross Platform Mobile Development using Flutter by Wei Meng Lee at Mobile foc...
Cross Platform Mobile Development using Flutter by Wei Meng Lee at Mobile foc...
 
Advanced #6 clean architecture
Advanced #6  clean architectureAdvanced #6  clean architecture
Advanced #6 clean architecture
 
Building Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in RailsBuilding Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in Rails
 
Telerik AppBuilder Presentation for TelerikNEXT Conference
Telerik AppBuilder Presentation for TelerikNEXT ConferenceTelerik AppBuilder Presentation for TelerikNEXT Conference
Telerik AppBuilder Presentation for TelerikNEXT Conference
 
SharePoint Fest Seattle - SharePoint Framework, Angular & Azure Functions
SharePoint Fest Seattle - SharePoint Framework, Angular & Azure FunctionsSharePoint Fest Seattle - SharePoint Framework, Angular & Azure Functions
SharePoint Fest Seattle - SharePoint Framework, Angular & Azure Functions
 
Spring Boot Workshop - January w/ Women Who Code
Spring Boot Workshop - January w/ Women Who CodeSpring Boot Workshop - January w/ Women Who Code
Spring Boot Workshop - January w/ Women Who Code
 
China Science Challenge
China Science ChallengeChina Science Challenge
China Science Challenge
 
SgCodeJam24 Workshop
SgCodeJam24 WorkshopSgCodeJam24 Workshop
SgCodeJam24 Workshop
 
Understanding iOS from an Android perspective
Understanding iOS from an Android perspectiveUnderstanding iOS from an Android perspective
Understanding iOS from an Android perspective
 
Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]
 
Prototyping applications with heroku and elasticsearch
 Prototyping applications with heroku and elasticsearch Prototyping applications with heroku and elasticsearch
Prototyping applications with heroku and elasticsearch
 
OpenWhisk Under the Hood -- London Oct 16 2016
OpenWhisk Under the Hood -- London Oct 16 2016OpenWhisk Under the Hood -- London Oct 16 2016
OpenWhisk Under the Hood -- London Oct 16 2016
 
Apic dc api deep dive
Apic dc api deep dive Apic dc api deep dive
Apic dc api deep dive
 
Stmik bandung
Stmik bandungStmik bandung
Stmik bandung
 

Recently uploaded

Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 

Recently uploaded (20)

Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 

A tour through Swift attributes

  • 1. Marco Eidinger, Nov 12th 2022 A tour through Swift attributes Highlight: System Programming Interfaces (experimental)
  • 2. About me https://blog.eidinger.info/ • Developing iOS apps and frameworks for over a decade at my day job. • Personally, I contribute to the iOS community with open-source tools like SwiftPlantUML and XCSnippetsApp. • I love to write and share my programming knowledge on my blog SwiftyTech (+ 100 articles). • Find me on on GitHub and Twitter as @MarcoEidinger.
  • 3. Attributes Non-exclusive list of o ffi cial attributes
  • 4. Attributes Non-exclusive list of o ffi cial attributes @autoclosure @escaping @frozen @main @globalActor @inlinable @available @Sendable @preconcurrency @warn_unqualified_access @testable @propertyWrapper @dynamicMemberLookup @nonobjc @objc @objcMembers
  • 5. We use Swift attributes all the time import SwiftUI struct ExampleAppApp: App { var body: some Scene { WindowGroup { ContentView() } } }
  • 6. We use Swift attributes all the time import SwiftUI @main struct ExampleAppApp: App { var body: some Scene { WindowGroup { ContentView() } } }
  • 7. SwiftUI framework @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *) extension App { /// Initializes and runs the app. /// /// If you precede your ``SwiftUI/App`` conformer's declaration with the /// @main attribute, the system calls the conformer's `main()` /// method to launch the app. /// SwiftUI provides a default implementation of the method /// that manages the launch process in a platform-appropriate way. @MainActor public static func main() }
  • 8. Attributes Part of the Swift programming language
  • 9. Two kinds of attributes apply to declarations vs. types @frozen public enum Exhaustive {} import SwiftUI struct LazyView<Content: View>: View { let build: () -> Content init(_ build: @autoclosure @escaping () -> Content) { self.build = build } var body: Content { self.build() } }
  • 10. Attributes are essential building blocks Example: SwiftUI import SwiftUI struct LocalizedView: View { var counter = 0 var body: some View { Button("Increase counter") { counter += 1 } } }
  • 11. Attributes are essential building blocks Example: SwiftUI import SwiftUI struct LocalizedView: View { @State var counter = 0 var body: some View { Button("Increase counter") { counter += 1 } } }
  • 12. Attributes are essential building blocks Example: SwiftUI import SwiftUI struct LocalizedView: View { @State var counter = 0 var body: some View { Button("Increase counter") { counter += 1 } } } @frozen @propertyWrapper public struct State<Value> : DynamicProperty {…}
  • 13. Attributes are essential building blocks Example: Concurrency @MainActor class ViewModel: ObservableObject {}
  • 14. Attributes are essential building blocks Example: Concurrency @globalActor final public actor MainActor : GlobalActor {…} @MainActor class ViewModel: ObservableObject {}
  • 15. Attributes are essential building blocks Example: Concurrency @globalActor final public actor MainActor : GlobalActor {…} @MainActor class ViewModel: ObservableObject {} extension MainActor { public static func run<T>( resultType: T.Type = T.self, body: @MainActor @Sendable () throws -> T) async rethrows -> T where T : Sendable }
  • 16. Interesting use cases Extensively used in SwiftUI (but not limited to SwiftUI) @warn_unquali fi ed_access
  • 17. Interesting use cases Extensively used in SwiftUI (but not limited to SwiftUI) • Use @warn_unquali fi ed_access to make your SwiftUI view modi fi ers safer.
  • 18. Interesting use cases Extensively used in SwiftUI (but not limited to SwiftUI) • Use @warn_unquali fi ed_access to make your SwiftUI view modi fi ers safer. @resultBuilder
  • 19. Interesting use cases Extensively used in SwiftUI (but not limited to SwiftUI) • Use @warn_unquali fi ed_access to make your SwiftUI view modi fi ers safer. • Use @resultBuilder to create cool DSLs.
  • 20. Interesting use cases Extensively used in SwiftUI (but not limited to SwiftUI) • Use @warn_unquali fi ed_access to make your SwiftUI view modi fi ers safer. • Use @resultBuilder to create cool DSLs. @propertyWrappers
  • 21. Interesting use cases Extensively used in SwiftUI (but not limited to SwiftUI) • Use @warn_unquali fi ed_access to make your SwiftUI view modi fi ers safer. • Use @resultBuilder to create cool DSLs. • Use @propertyWrappers to create reusable property implementation patterns. Property wrappers in the wild.
  • 22. Interesting use cases Extensively used in SwiftUI (but not limited to SwiftUI) • Use @warn_unquali fi ed_access to make your SwiftUI view modi fi ers safer. • Use @resultBuilder to create cool DSLs. • Use @propertyWrappers to create reusable property implementation patterns. Property wrappers in the wild. @_alwaysEmitIntoClient
  • 23. Interesting use cases Extensively used in SwiftUI (but not limited to SwiftUI) • Use @warn_unquali fi ed_access to make your SwiftUI view modi fi ers safer. • Use @resultBuilder to create cool DSLs. • Use @propertyWrappers to create reusable property implementation patterns. Property wrappers in the wild. • SwiftUI uses @_alwaysEmitIntoClient to back port new features
  • 25. Code is better than documentation • Swift 5.7 • https://github.com/apple/swift/blob/release/5.7/include/swift/AST/Attr.def • Swift 5.8 and beyond • Code Ownership of Swift Attributes to swift-syntax • Replace Attr.def with a gyb fi le that reads from swift-syntax to automatically generate the attribute nodes. • https://github.com/apple/swift-syntax/blob/main/gyb_syntax_support/ AttributeKinds.py
  • 26. Definition of Declaration Attributes https://github.com/apple/swift-syntax/blob/main/gyb_syntax_support/AttributeKinds.py
  • 27. Tests reveal specification Example: https://github.com/apple/swift/blob/main/test/attr/warn_unquali fi ed_access.swift
  • 29. Warning: Apple discourages using underscored attributes those semantics are subject to change and most likely need to go through the Swift evolution process before being stabilized.
  • 30. Early adoption still possible Successful transition from experimental to proper language feature @resultBuilder @_functionBuilder Swift 5.1 Swift 5.4
  • 32.
  • 33. System Programming Interface Experimental @_spi and related attributes Attribute Introduced in @_spi Swift 5.3 @_spi_available(platform, version) Swift 5.7 @_spiOnly master (~ Swift 5.8)
  • 35. Shopping.swiftinterfaces // swift-interface-format-version: 1.0 // swift-compiler-version: Apple Swift version 5.7 (swiftlang-5.7.0.127.4 clang-1400.0.29.50) // swift-module-flags: -target arm64-apple-macosx10.13 -enable-objc-interop -enable-library-evolution -swift-version 5 -Onone -module-name Shopping import Swift import _Concurrency public struct ShoppingCartItem { public init() } public struct ShoppingCart { public init() public func payCash() }
  • 36. Shopping.private.swiftinterfaces // swift-interface-format-version: 1.0 // swift-compiler-version: Apple Swift version 5.7 (swiftlang-5.7.0.127.4 clang-1400.0.29.50) // swift-module-flags: -target arm64-apple-macosx10.13 -enable-objc-interop -enable-library-evolution -swift-version 5 -Onone -module-name Shopping import Swift import _Concurrency public struct ShoppingCartItem { public init() } public struct ShoppingCart { public init() public func payCash() @_spi(PayPal) public func payWithPayPal() }
  • 37. @_spi_available(platform, version) // Module "Shopping" public struct ShoppingCart { public init() {} @_spi_available(watchOS 9, *) @available(tvOS, unavailable) public private(set) var items = [ShoppingCartItem]() public func payCash() {} @_spi(PayPal) public func payWithPayPal() {} @_spi(Bitcoin) public func payWithBitcoin() {} } // swift-interface-format-version: 1.0 // swift-compiler-version: Apple Swift version 5.7 // swift-module-flags: -target arm64-apple-macosx10 version 5 -Onone -module-name Shopping import Swift import _Concurrency public struct ShoppingCartItem { public init() } public struct ShoppingCart { public init() @available(watchOS, unavailable) @available(tvOS, unavailable) public var items: [Shopping.ShoppingCartItem] { get } public func payCash() }
  • 39. @_spiOnly Impact to .swiftinterfaces // swift-interface-format-version: 1.0 // swift-compiler-version: Apple Swift version 5.8-dev (LLVM 338153f292479fa, Swift b752d7883c26cea) // swift-module-flags: -target arm64-apple-macosx10.13 -enable-objc-interop -enable-library-evolution -swift- version 5 -Onone -module-name Shopping import Swift import _Concurrency import _StringProcessing // swift-interface-format-version: 1.0 // swift-compiler-version: Apple Swift version 5.8-dev (LLVM 338153f292479fa, Swift b752d7883c26cea) // swift-module-flags: -target arm64-apple-macosx10.13 -enable-objc-interop -enable-library-evolution -swift- version 5 -Onone -module-name Shopping /*@_spiOnly*/ import CryptoKit import Swift import _Concurrency import _StringProcessing public struct ShoppingCartItem {