Skip to content

Instantly share code, notes, and snippets.

View sinsoku's full-sized avatar

Takumi Shotoku sinsoku

View GitHub Profile
@sinsoku
sinsoku / class_scope.swift
Last active March 5, 2016 07:09
typealiasでextensionしたときの挙動
class User {
typealias MyInt = Int
}
extension User.MyInt {
func say() {
print("MyInt")
}
}
1.say() // MyInt
@sinsoku
sinsoku / niwatako_ext.swift
Created March 5, 2016 14:27
@niwatako さんのプロトコル拡張の問題の話
protocol P {
func say()
}
class A : P {}
class B : A {}
extension P {
func say() { print("P") }
}
@sinsoku
sinsoku / action.rb
Last active March 18, 2016 14:48
action resource
# app/models/posts/action.rb
class Action
include ActiveModel::Model
attr_reader :post_id, :name
def save
return false if invalid?
@post = Post.find(post_id)
@sinsoku
sinsoku / mirrorSample.swift
Created June 15, 2016 17:27
mirror sample
class Foo {
let number: Int? = nil
}
let m = Mirror(reflecting: Foo())
let defNumber = m.children.filter { $0.label == "number" }[0]
defNumber.value == nil
// Playground execution failed: MyPlayground.playground:1:17: error: value of type 'Any' (aka 'protocol<>') can never be nil, comparison isn't allowed
// defNumber.value == nil
//~~~~~~~~~~~~~~~ ^
@sinsoku
sinsoku / unhighlignter.js
Last active December 24, 2020 02:44
WIP Pull Request Unhighlignter for GitHub が使えなくなったので、 Tampermonkey の UserScript で再実装した
// ==UserScript==
// @name WIP Pull Request Unhighlignter
// @author sinsoku
// @version 0.4
// @match https://github.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
@sinsoku
sinsoku / EquatableWithGenerics.swift
Created June 27, 2016 11:37
Equatable の振る舞いを Generics で変えたい
struct A<T>: Equatable {
let name: String
// 2. 最悪、Tに比較メソッドを実装する
// func == <T>(lhs: A<T>, rhs: A<T>) -> Bool {
// return T.compare(lhs, rhs)
// }
}
func == <T>(lhs: A<T>, rhs: A<T>) -> Bool {
return false
@sinsoku
sinsoku / MarkerClass.swift
Created June 27, 2016 13:47
Generics と空クラスでメソッドの挙動を変える
struct S<T> {
let name: String
}
func m<T>(_ v: S<T>) {
print(1)
}
class C {}
func m<T: C>(_ v: S<T>) {
@sinsoku
sinsoku / ComparableWithGenerics.swift
Last active June 28, 2016 13:50
Swift で Generics を使って Comparable の挙動を変更する
protocol P {
static func eq<T>(_ x: A<T>, _ y: A<T>) -> Bool
static func lt<T>(_ x: A<T>, _ y: A<T>) -> Bool
}
struct A<T: P>: Comparable {}
func == <T>(x: A<T>, y: A<T>) -> Bool {
return T.eq(x, y)
}
@sinsoku
sinsoku / AppleHashableExample.swift
Created June 30, 2016 04:19
Hashable よくわからん・・・
struct GridPoint {
var x: Int
var y: Int
}
func ==(lhs: GridPoint, rhs: GridPoint) -> Bool {
return lhs.x == rhs.x && lhs.y == rhs.y
}
extension GridPoint: Hashable {
struct Container<T> {
var obj: T
}
func factory<T>(objects: T...) -> Array<Container<T>> {
return objects.map { Container<T>(obj: $0) }
}
let c = factory("a", "b")
print(c.dynamicType)