Skip to content

Instantly share code, notes, and snippets.

View inamiy's full-sized avatar

Yasuhiro Inami inamiy

View GitHub Profile
@inamiy
inamiy / SwiftElmFrameworkList.md
Last active March 11, 2024 10:20
React & Elm inspired frameworks in Swift
@inamiy
inamiy / mimicking-extensibles.swift
Created March 7, 2021 08:00
Mimicking extensibles (intersection/union types) in Swift https://twitter.com/inamiy/status/1368468757702569986
struct State1 {
var foo1: Int
}
struct State2 {
var bar2: Bool
}
// Q. How to make a "flattened" `struct State1_2 { var foo1: Int, var bar2: Bool }`
// from `State1` and `State2`? (a.k.a. extensible record / intersection type)?
@inamiy
inamiy / recursive-struct.swift
Created February 9, 2021 06:23
Recursive struct in Swift using Box (indirect struct)
@propertyWrapper
class Box<T> {
var wrappedValue: T
var projectedValue: Box<T> {
Box(wrappedValue)
}
init(_ value: T) {
self.wrappedValue = value
// 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
@MainActor
class ViewModel: ObservableObject {}
@propertyWrapper
struct Wrapper<T> {
var wrappedValue: T
}
@propertyWrapper
struct MainWrapper<T> {
@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
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 / SwiftUI-emulating-React-Hooks.swift
Last active March 20, 2023 07:19
SwiftUI emulating React custom hooks (DynamicProperty composition) https://twitter.com/inamiy/status/1313343537132433409
import SwiftUI
struct ContentView: View {
@UseCounter
var counter: Int = 0
@UseCounterEffect(initialCount: 1, onUpdate: { count in
print("===> update: \(count)")
})
var counterEffect: Void
-- Solving Fix / Mu / Nu exercise in
-- https://stackoverflow.com/questions/45580858/what-is-the-difference-between-fix-mu-and-nu-in-ed-kmetts-recursion-scheme-pac
{-# LANGUAGE RankNTypes, GADTs #-}
----------------------------------------
-- Fix / Mu / Nu
newtype Fix f = Fix { unFix :: f (Fix f) }
@inamiy
inamiy / try_x_=.swift
Created February 13, 2023 05:29
Didn't know `try x = ...` was possible. https://twitter.com/inamiy/status/1623625122652491776
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
public actor A {
nonisolated let mySendable: MySendable
nonisolated let nonSendable: NonSendable
public init() {
self.mySendable = MySendable()
self.nonSendable = NonSendable()
}
public nonisolated var forwardedMySendable: MySendable {