Skip to content

Instantly share code, notes, and snippets.

@ukitaka
ukitaka / indirect+loe.swift
Last active October 1, 2018 02:57
indirect+loe.swift
func update<T>(value: inout T, update: (inout T) -> Void) {
update(&value)
}
struct Hoge {
var list: List<Int>
}
enum List<T> {
case `nil`
@ukitaka
ukitaka / output
Created July 14, 2018 04:47
swift -frontend -typecheck -debug-constraints test.swift
---Constraint solving for the expression at [test.swift:1:14 - line:1:14]---
---Initial constraints for the given expression---
(integer_literal_expr type='$T0' location=test.swift:1:14 range=[test.swift:1:14 - line:1:14] value=0)
Score: 0 0 0 0 0 0 0 0 0 0 0 0
Contextual Type: Int at [test.swift:1:8 - line:1:8]
Type Variables:
$T0 [inout allowed] literal=3 bindings={(subtypes of) (default from ExpressibleByIntegerLiteral) Int} @ locator@0x7faa21801000 [IntegerLiteral@test.swift:1:14]
Active Constraints:
@ukitaka
ukitaka / test.s
Created June 16, 2018 03:48
clang -S -mllvm --x86-asm-syntax=intel test.c
.section __TEXT,__text,regular,pure_instructions
.macosx_version_min 10, 13
.intel_syntax noprefix
.globl _main ## -- Begin function main
.p2align 4, 0x90
_main: ## @main
.cfi_startproc
## BB#0:
push rbp
Lcfi0:
@ukitaka
ukitaka / once.swift
Created April 25, 2018 09:15
Set value to optional only once.
class MyClass {
var value: Int? {
willSet {
precondition(self.value == nil) }
}
}
}
@ukitaka
ukitaka / Eq.sil.swift
Created February 26, 2018 10:51
Eq.sil
*** SIL module before Guaranteed Passes transformation (0) ***
// unwrapByEq(_:)
sil @_T02Eq08unwrapByA0ySiSgF : $@convention(thin) (Optional<Int>) -> () {
// %0 // users: %5, %1
bb0(%0 : $Optional<Int>):
debug_value %0 : $Optional<Int>, let, name "optional", argno 1 // id: %1
// function_ref Bool._getBuiltinLogicValue()
%2 = function_ref @_T0Sb21_getBuiltinLogicValueBi1_yF : $@convention(method) (Bool) -> Builtin.Int1 // user: %17
// function_ref != infix<A>(_:_:)
%3 = function_ref @_T0s2neoiSbxSg_ABts9EquatableRzlF : $@convention(thin) <τ_0_0 where τ_0_0 : Equatable> (@in Optional<τ_0_0>, @in Optional<τ_0_0>) -> Bool // user: %13
@ukitaka
ukitaka / compare.swift
Created February 6, 2018 08:46
haskell-like-func-def.swift
protocol MyComparable {
static func < (lhs: Self, rhs: Self) -> Bool
static func >= (lhs: Self, rhs: Self) -> Bool
}
extension MyComparable {
static func < (lhs: Self, rhs: Self) -> Bool {
return !(rhs >= lhs)
}
protocol NonEmptyHList {
associatedtype Head
associatedtype Tail
}
struct NEHLCons<H, T: NonEmptyHList>: NonEmptyHList {
typealias Head = H
typealias Tail = T
let head: H
typealias Int = MyInt
struct MyInt: ExpressibleByIntegerLiteral, Equatable {
typealias IntegerLiteralType = Swift.Int
let value: Swift.Int
init(integerLiteral value: Swift.Int) {
self.value = value
}
---Constraint solving for the expression at [sub.swift:4:11 - line:4:15]---
(increasing score due to function conversion)
(overload set choice binding ($T1) -> $T0 := () -> Dog)
---Initial constraints for the given expression---
(call_expr type='Dog' location=sub.swift:4:11 range=[sub.swift:4:11 - line:4:15] arg_labels=
(type_expr type='Dog.Type' location=sub.swift:4:11 range=[sub.swift:4:11 - line:4:11] typerepr='Dog')
(tuple_expr type='()' location=sub.swift:4:14 range=[sub.swift:4:14 - line:4:15]))
Score: 0 0 0 0 1 0 0 0 0 0 0 0 0
Type Variables:
#0 = $T0 [inout allowed] as Dog
@ukitaka
ukitaka / exist.swift
Last active November 15, 2017 12:47
存在型
protocol Animal {
func bark()
}
struct Dog: Animal {
func bark() {
print("わんわん")
}
}