关于Swift和UI中的“some”关键字是什么?的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于AWESOMESWIFT-swift.libhunt.com-swift类库网站、C#中的
关于Swift和UI中的“ some”关键字是什么?的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于AWESOME SWIFT-swift.libhunt.com-swift类库网站、C# 中的上下文关键字是什么?、C语言中的 auto 关键字是什么?、Go 语言中的 defer 关键字是什么?等相关知识的信息别忘了在本站进行查找喔。
本文目录一览:- Swift(UI)中的“ some”关键字是什么?(swift 关键字及作用)
- AWESOME SWIFT-swift.libhunt.com-swift类库网站
- C# 中的上下文关键字是什么?
- C语言中的 auto 关键字是什么?
- Go 语言中的 defer 关键字是什么?
Swift(UI)中的“ some”关键字是什么?(swift 关键字及作用)
新的SwiftUI教程具有以下代码:
struct ContentView: View { var body: some View { Text("Hello World") }}
第二行单词some
和在其网站上被高亮显示,就像它是一个关键字一样。
Swift
5.1似乎没有some
作为关键字,而且我也看不出该词some
还有什么其他用处,因为它通常位于类型所在的位置。是否有Swift的未发布新版本?以某种我不知道的方式在类型上使用的函数吗?
关键字有some
什么作用?
答案1
小编典典some View
是SE-0244引入的不透明结果类型,在带有Xcode 11的Swift
5.1中可用。您可以将其视为“反向”通用占位符。
与调用方可以满足的常规通用占位符不同:
protocol P {}struct S1 : P {}struct S2 : P {}func foo<T : P>(_ x: T) {}foo(S1()) // Caller chooses T == S1.foo(S2()) // Caller chooses T == S2.
不透明的结果类型是 实现 满足的隐式通用占位符,因此您可以考虑一下:
func bar() -> some P { return S1() // Implementation chooses S1 for the opaque result.}
看起来像这样:
func bar() -> <Output : P> Output { return S1() // Implementation chooses Output == S1.}
实际上,此功能的最终目标是允许以更明确的形式使用反向泛型,这也将使您添加约束,例如-> <T : Collection> T whereT.Element == Int
。有关更多信息,请参见此帖子。
要摆脱的主要问题是,函数返回some P
是一个函数,该函数返回符合的特定 单个 具体类型的值P
。尝试在函数中返回不同的符合类型会产生编译器错误:
// error: Function declares an opaque return type, but the return// statements in its body do not have matching underlying types.func bar(_ x: Int) -> some P { if x > 10 { return S1() } else { return S2() }}
因为隐式通用占位符不能由多种类型满足。
这与returning函数相反P
,后者可以用于表示 两者 S1
和S2
因为它表示任意P
符合的值:
func baz(_ x: Int) -> P { if x > 10 { return S1() } else { return S2() }}
好的,不透明结果类型-> some P
比协议返回类型有什么好处-> P
?
1.不透明的结果类型可以与PAT一起使用
当前协议的主要限制是PAT(具有关联类型的协议)不能用作实际类型。尽管此限制在将来的语言版本中可能会取消,但由于不透明的结果类型实际上只是通用的占位符,因此今天它们可以与PATs一起使用。
这意味着您可以执行以下操作:
func giveMeACollection() -> some Collection { return [1, 2, 3]}let collection = giveMeACollection()print(collection.count) // 3
2.不透明的结果类型具有标识
由于不透明的结果类型强制返回单个具体类型,因此编译器知道对同一函数的两次调用必须返回相同类型的两个值。
这意味着您可以执行以下操作:
// foo() -> <Output : Equatable> Output {func foo() -> some Equatable { return 5 // The opaque result type is inferred to be Int.}let x = foo()let y = foo()print(x == y) // Legal both x and y have the return type of foo.
这是合法的,因为编译器知道两者x
并且y
具有相同的具体类型。这是==
两个参数类型都为的重要要求Self
。
protocol Equatable { static func == (lhs: Self, rhs: Self) -> Bool}
这意味着它期望两个值都与具体符合类型相同。即使Equatable
可以用作类型,也无法将两个任意Equatable
符合的值相互比较,例如:
func foo(_ x: Int) -> Equatable { // Assume this is legal. if x > 10 { return 0 } else { return "hello world" }}let x = foo(20)let y = foo(5)print(x == y) // Illegal.
由于编译器无法证明两个任意Equatable
值具有相同的基础具体类型。
In a similar manner, if we introduced another opaque type returning function:
// foo() -> <Output1 : Equatable> Output1 {func foo() -> some Equatable { return 5 // The opaque result type is inferred to be Int.}// bar() -> <Output2 : Equatable> Output2 {func bar() -> some Equatable { return "" // The opaque result type is inferred to be String.}let x = foo()let y = bar()print(x == y) // Illegal, the return type of foo != return type of bar.
The example becomes illegal because although both foo
and bar
return someEquatable
, their “reverse” generic placeholders Output1
and Output2
could
be satisfied by different types.
3. Opaque result types compose with generic placeholders
Unlike regular protocol-typed values, opaque result types compose well with
regular generic placeholders, for example:
protocol P { var i: Int { get }}struct S : P { var i: Int}func makeP() -> some P { // Opaque result type inferred to be S. return S(i: .random(in: 0 ..< 10))}func bar<T : P>(_ x: T, _ y: T) -> T { return x.i < y.i ? x : y}let p1 = makeP()let p2 = makeP()print(bar(p1, p2)) // Legal, T is inferred to be the return type of makeP.
This wouldn’t have worked if makeP
had just returned P
, as two P
values
may have different underlying concrete types, for example:
struct T : P { var i: Int}func makeP() -> P { if .random() { // 50:50 chance of picking each branch. return S(i: 0) } else { return T(i: 1) }}let p1 = makeP()let p2 = makeP()print(bar(p1, p2)) // Illegal.
Why use an opaque result type over the concrete type?
At this point you may be thinking to yourself, why not just write the code as:
func makeP() -> S { return S(i: 0)}
Well, the use of an opaque result type allows you to make the type S
an
implementation detail by exposing only the interface provided by P
, giving
you flexibility of changing the concrete type later down the line without
breaking any code that depends on the function.
For example, you could replace:
func makeP() -> some P { return S(i: 0)}
with:
func makeP() -> some P { return T(i: 1)}
without breaking any code that calls makeP()
.
See the Opaque Types section of the language guide and the Swift
evolution proposal for further
information on this feature.
AWESOME SWIFT-swift.libhunt.com-swift类库网站
https://swift.libhunt.com/categories/688-events
29 Events libraries and projects
- ORDERED BY POPULARITY
- ORDER BY DEV ACTIVITY
-
ReactiveCocoa
10.0 7.3 Objective-CReactiveCocoa (RAC) is a Cocoa framework inspired by Functional Reactive Programming. It provides APIs for composing and transforming streams of values over time. -
RxSwift
9.9 8.8 L3 SwiftMicrosoft Reactive Extensions (Rx) for Swift and iOS/OSX platform. -
PromiseKit
9.8 8.7 L5 Swiftasync promise programming lib. -
ReSwift
9.6 6.3 L5 SwiftUnidirectional Data Flow in Swift -
Bond
9.3 8.8 L1 Swifta Swift binding framework. -
BrightFutures
8.3 4.7 L4 Swiftpromise and future lib for swift. -
Katana
8.2 8.7 L4 SwiftSwift apps a la React and Redux. -
ReactorKit
7.8 6.4 SwiftA framework for reactive and unidirectional application architecture. -
ReactKit
7.4 0.0 L3 SwiftSwift Reactive Programming. -
FutureKit
6.4 0.7 L2 SwiftA Swift based Future/Promises Library. -
SwiftEventBus
6.4 3.2 L5 SwiftA publish/subscribe event bus optimized for iOS. -
EmitterKit
5.7 3.5 L5 Swiftan implementation of event emitters and listeners in swift. -
Signals
4.9 3.3 L5 Swiftreplaces delegates and notifications. -
Safe
4.9 0.0 L2 SwiftA modern concurrency and synchronization for Swift. -
snail
4.5 7.1 L5 SwiftAn observables framework for Swift -
Reactor
4.1 2.4 L5 SwiftPowering your RAC architecture. -
VueFlux
3.8 6.8 SwiftUnidirectional Data Flow State Management Architecture -
SignalKit
3.7 0.0 L5 SwiftSwift event and binding framework. -
Observable
3.7 6.2 SwiftThe easiest way to observe values. -
When
3.4 5.4 L5 SwiftA lightweight implementation of Promises in Swift. -
Caravel
3.3 0.0 L2 SwiftA Swift event bus for UIWebView and JS. -
Future
2.5 0.0 L4 SwiftA micro framework providing Future. -
NoticeObserveKit
2.3 0.0 L5 SwiftNoticeObserveKit is type-safe NotificationCenter wrapper that associates notice type with info type. -
Aftermath
1.8 0.0 L5 SwiftStateless message-driven micro-framework in Swift. -
Notificationz
1.6 2.5 L5 SwiftHelping you own NSNotificationCenter by providing a simple, customizable adapter. -
Forbind
1.2 0.0 L4 SwiftFunctional chaining and Promises in Swift. -
ReduxSwift
1.0 0.0 L5 SwiftPredictable state container for Swift apps too -
PureFutures
0.7 0.0 L4 SwiftFutures and Promises library. -
SSEventFlow
0.3 4.4 L5 SwiftA type safe alternative to NSNotification, inspired by Flux.
C# 中的上下文关键字是什么?
在C#中,一些标识符在代码上下文中具有特殊含义,例如get和set被称为上下文关键字。
以下是显示上下文关键字的表格:
上下文关键字 | |||||||
---|---|---|---|---|---|---|---|
add | alias | ascending | descending | dynamic | from | get | |
global | group | into | join | let | orderby | partial (type) | |
partial(method) | remove | select | set |
以上就是C# 中的上下文关键字是什么?的详细内容,更多请关注php中文网其它相关文章!
C语言中的 auto 关键字是什么?
在C语言中,每个函数的局部变量都被称为自动(auto)变量。在函数块内部声明的变量被称为局部变量。本地变量也被称为自动变量。在变量的数据类型前使用auto
关键字是可选的。如果本地变量中没有存储任何值,那么它就由一个垃圾值组成。
Go 语言中的 defer 关键字是什么?
go 语言中的 defer 关键字是什么?
在编写程序时,我们经常需要在某个函数或方法执行完毕后,执行一些清理或资源释放工作。这个时候,Go 语言提供了一种方便的机制,通过使用 defer 关键字,可以将这些清理或资源释放工作推迟到函数或方法返回之前执行。
defer 关键字是一个编译时解析的语法糖,它通过将一个函数或方法的调用推迟到当前函数或方法返回之前执行,来实现在程序执行结束前,释放资源或执行清理工作的目的。defer 关键字可以出现在一个函数或方法中的任何位置,并且可以有多个 defer 语句,这些语句的执行顺序是倒序的。
defer func() {
// defer 延迟执行的代码
}()
示例中的 defer 语句将要执行的函数定义在匿名函数中,可以是任意的函数或方法调用。defer 后的左括号必须紧跟着 defer 关键字,右括号则在要执行的代码的末尾,这样才能确保正确的语法结构。
defer 关键字的使用场景不仅仅是在释放资源或执行清理工作方面。它还可以用来监控函数或方法的执行情况,使用defer 来记录函数或方法在调用前后的时间戳,可以轻松地记录函数或方法执行花费的时间。
func someFunction() {
start := time.Now() // 记录起始时间
defer func() {
1 2 |
|
", elapsed)
}()
// someFunction 的主要逻辑
}
在上面的示例中,someFunction 在执行返回后会输出执行花费的时间。这种监控函数或方法性能的方式非常实用,可以帮助我们更好地了解程序的性能瓶颈所在,并作出调整和改进。
defer 关键字也可以使用在 panic 和 recover 处理中,当程序发生 panic 时,可以通过 defer 语句中的 recover 语句把程序恢复到正常状态。
func someFunction() {
defer func() {
1 2 3 |
|
}()
// someFunction 的主要逻辑
}
在上面的示例中,someFunction 在发生 panic 时会输出 panic 的错误信息,这样我们可以及时处理程序的错误,并且让程序尽量保持正常运行状态。
总结来说,Go 语言中的 defer 关键字是用来推迟某个函数或方法的执行,处理资源释放或清理工作,监控函数或方法执行情况,处理 panic 和 recover 等方面的一个实用工具。它既能提高程序的效率和性能,也能提高程序的可靠性和健壮性,因此在编写程序时,使用 defer 关键字是一个非常好的选择。
以上就是Go 语言中的 defer 关键字是什么?的详细内容,更多请关注php中文网其它相关文章!
我们今天的关于Swift和UI中的“ some”关键字是什么?的分享已经告一段落,感谢您的关注,如果您想了解更多关于AWESOME SWIFT-swift.libhunt.com-swift类库网站、C# 中的上下文关键字是什么?、C语言中的 auto 关键字是什么?、Go 语言中的 defer 关键字是什么?的相关信息,请在本站查询。
本文标签: