Skip to content

Instantly share code, notes, and snippets.

View inamiy's full-sized avatar

Yasuhiro Inami inamiy

View GitHub Profile
View recursive-opaque-type.swift
// Recursive opaque type fails compile because recursiveness can’t deterministically pick the right type to replace `some P`.
protocol P {}
struct Parent<Child: P>: P {
let children: [Child]
}
func recursive() -> some P {
// ERROR: Function opaque return type was inferred as 'Parent<some P>', which defines the opaque type in terms of itself
@inamiy
inamiy / MainActor-PropertyWrapper.swift
Last active May 20, 2023 02:02
`@propertyWrapper` + `@MainActor var wrappedValue` which propagates `@MainActor` context to the encapsulating type. https://twitter.com/inamiy/status/1659530309501853696
View MainActor-PropertyWrapper.swift
@MainActor
class ViewModel: ObservableObject {}
@propertyWrapper
struct Wrapper<T> {
var wrappedValue: T
}
@propertyWrapper
struct MainWrapper<T> {
@inamiy
inamiy / try_x_=.swift
Created February 13, 2023 05:29
Didn't know `try x = ...` was possible. https://twitter.com/inamiy/status/1623625122652491776
View try_x_=.swift
func foo() throws -> Int {
1
}
func fooAsync() async throws -> Int {
1
}
class Foo {
var x: Int = 0
@inamiy
inamiy / non-Sendable-actor-boundary.swift
Created December 19, 2022 14:58
non-`Sendable` cannot cross actor boundary
View non-Sendable-actor-boundary.swift
public actor A {
nonisolated let mySendable: MySendable
nonisolated let nonSendable: NonSendable
public init() {
self.mySendable = MySendable()
self.nonSendable = NonSendable()
}
public nonisolated var forwardedMySendable: MySendable {
@inamiy
inamiy / task-weak-self-nonisolated-error.swift
Last active December 15, 2022 13:00
`Task { [weak self] in self?.prop = value }` causes non-isolated mutation error https://twitter.com/inamiy/status/1603267958209331200
View task-weak-self-nonisolated-error.swift
actor A {
var prop: Int = 0
func foo() {
// OK
Task {
self.prop = 1
}
// ERROR: Actor-isolated property 'prop' can not be mutated from a non-isolated context
@inamiy
inamiy / ChatGPT.txt
Last active December 3, 2022 01:26
ChatGPT: 人類の歴史から得られる教訓は何ですか? https://twitter.com/inamiy/status/1598850855331328003
View ChatGPT.txt
Q. 人類の歴史から得られる教訓は何ですか?
人類の歴史から得られる教訓は、様々ありますが、その一つとして、「人類は、自然界と共存しながら繁栄することが大切である」ということが挙げられます。
人類は、自然界にとって重要な存在です。人類が進化してきた過程では、自然界が大きく変化しましたが、その変化は、自然界の平衡を乱し、将来的に人類自身の生存を脅かすことになるかもしれません。
そのため、人類は、自然界と共存しながら、環境を保全しながら繁栄することが大切です。人類は、自然界を破壊したり、他の種を排除したりしてはいけません。また、人類は、自然界から学び、自然界を尊重しながら発展していくことが大切です。
以上のように、人類の歴史から得られる教訓として、「人類は、自然界と共存しながら繁栄することが大切である」ということが挙げられます。ご参考までになれば幸いです。
Q. 他にもありますか?
人類の歴史から得られる教訓は、様々ありますが、その一つとして、「人類は、他の文化や習慣を尊重しなければならない」ということが挙げられます。
@inamiy
inamiy / avoid-implicitSelfCapture.swift
Created December 2, 2022 11:03
Avoid @_implicitSelfCapture pitfall by always adding `[weak self]` in `Task.init` closure
View avoid-implicitSelfCapture.swift
// Always use `[weak self]` explicitly here to prevent `self` from being retained until the end of Task scope.
//
// NOTE:
// Because of `Task.init` having `@_implicitSelfCapture` by (bad) API design,
// without `[weak self]` will cause implicit strong `self` reference, which may never get deallocated
// especially when using `for await`.
//
// See also: https://twitter.com/inamiy/status/1544252986339504128
Task { [weak self] in
for await x in sequence {
@inamiy
inamiy / AsyncSequence-subprotocol-try-await-element-type.swift
Created November 21, 2022 06:54
AsyncSequence subprotocol with primary associated type, erase with existential, and check try-await element type
View AsyncSequence-subprotocol-try-await-element-type.swift
import Foundation
import _Concurrency
extension AsyncStream {
public init<S: AsyncSequence & Sendable>(
_ sequence: S,
bufferingPolicy limit: Continuation.BufferingPolicy = .unbounded
) where S.Element == Element {
self.init(bufferingPolicy: limit) { continuation in
let task = Task {
@inamiy
inamiy / swift-anonymous-enum-case-flattening.swift
Last active July 27, 2022 05:26
Swift Anonymous Enum and automatic case-flattening (Thought experiment) https://twitter.com/inamiy/status/1552162547079725057
View swift-anonymous-enum-case-flattening.swift
//---------------------------------------------------
// Anonymous 2-case enum example
//---------------------------------------------------
struct Error0: Swift.Error {}
struct Error1: Swift.Error {}
// Here, new `|` symbol is used as an anonymous enum type.
typealias Error01 = Error0 | Error1
@inamiy
inamiy / iOS16-NavigationStack.swift
Last active June 12, 2022 06:55
iOS 16 NavigationStack binding + navigationDestination behavior (Xcode 14 Beta 1, Swift 5.7) https://twitter.com/inamiy/status/1535861181998862337
View iOS16-NavigationStack.swift
import SwiftUI
struct RootStringNavStack: View {
@State
var navigationPath: [String] = []
// For hook-printing.
var navigationPathBinding: Binding<[String]> {
.init(
get: { navigationPath },