SlideShare a Scribd company logo
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

Spring boot
Spring bootSpring boot
Baruco 2014 - Rubymotion Workshop
Baruco 2014 - Rubymotion WorkshopBaruco 2014 - Rubymotion Workshop
Baruco 2014 - Rubymotion Workshop
Brian Sam-Bodden
 
Refactor Large apps with Backbone
Refactor Large apps with BackboneRefactor Large apps with Backbone
Refactor Large apps with Backbone
devObjective
 
Refactor Large applications with Backbone
Refactor Large applications with BackboneRefactor Large applications with Backbone
Refactor Large applications with Backbone
ColdFusionConference
 
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
Stacy London
 
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
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...
DevClub_lv
 
Advanced #6 clean architecture
Advanced #6  clean architectureAdvanced #6  clean architecture
Advanced #6 clean architecture
Vitali Pekelis
 
Building Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in RailsBuilding Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in Rails
Jim Jeffers
 
Telerik AppBuilder Presentation for TelerikNEXT Conference
Telerik AppBuilder Presentation for TelerikNEXT ConferenceTelerik AppBuilder Presentation for TelerikNEXT Conference
Telerik AppBuilder Presentation for TelerikNEXT Conference
Jen 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 Functions
Sé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 Code
Purnima Kamath
 
China Science Challenge
China Science ChallengeChina Science Challenge
China Science Challenge
remko caprio
 
SgCodeJam24 Workshop
SgCodeJam24 WorkshopSgCodeJam24 Workshop
SgCodeJam24 Workshop
remko caprio
 
Understanding iOS from an Android perspective
Understanding iOS from an Android perspectiveUnderstanding iOS from an Android perspective
Understanding iOS from an Android perspective
Lauren 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 elasticsearch
protofy
 
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
Stephen Fink
 
Apic dc api deep dive
Apic dc api deep dive Apic dc api deep dive
Apic dc api deep dive
Cisco DevNet
 
Stmik bandung
Stmik bandungStmik bandung
Stmik bandung
farid savarudin
 

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
 
Refactor Large apps with Backbone
Refactor Large apps with BackboneRefactor Large apps with Backbone
Refactor Large apps with Backbone
 
Refactor Large applications with Backbone
Refactor Large applications with BackboneRefactor Large applications with Backbone
Refactor Large applications with Backbone
 
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
 
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

UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 

Recently uploaded (20)

UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 

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 {