Skip to content

Instantly share code, notes, and snippets.

val a: Int = 999
val b: Int? = a
val c: Int? = a
println(b === c) // false
extension CountableRange {
public func contains(_ range: CountableRange<Bound>) -> Bool {
return lowerBound <= range.lowerBound && range.upperBound <= upperBound
}
public func contains(_ range: CountableClosedRange<Bound>) -> Bool {
return lowerBound <= range.lowerBound && range.upperBound < upperBound
}
public func contains(_ range: Range<Bound>) -> Bool {
open class Animal
class Cat: Animal()
class Box<out Value> ( // `out` for covariance
val value: Value
)
fun main(args: Array<String>) {
val catBox: Box<Cat> = Box(Cat())
val animalBox: Box<Animal> = catBox // OK
class Animal {}
class Cat: Animal {}
let cats: Array<Cat> = [Cat()]
let animals: Array<Animal> = cats // OK
let cat: Optional<Cat> = .some(Cat())
let animal: Optional<Animal> = cat // OK
struct Box<Value> {
// Argo
struct User {
  let id: Int
  let name: String
  let email: String?
  let role: Role
  let companyName: String
  let friends: [User]
}
@koher
koher / each-pair-without-any-sequence.md
Created March 3, 2017 03:12
`eachPair` without `AnySequence`

eachPair without AnySequence

extension Sequence where Self.SubSequence: Sequence {
  func eachPair() -> Zip2Sequence<Self, Self.SubSequence> {
    return zip(self, self.dropFirst())
  }
}

(1...10).eachPair().forEach { print($0) }
@koher
koher / README.md
Last active February 26, 2017 14:39
SwiftScript Translation Catalog

SwiftScript Translation Catalog (Draft)

This is a catalog of translations by SwiftScript, a transpiler from Swift to JavaScript, for a hackathon. It supports only a subset of Swift and has some incompatibilities.

Supporting Library

SwiftScript tries to translate Swift codes to JS codes using builtin JS APIs. However some needs a supporting library to simulate behaviors of Swift. A typical example is ?..

If

throws : async = try : await = catch : wait = rethrows : reasync = Result<T> : Promise<T>

throws : async = try : await

// Asynchronous selection by an action sheet
func actionSheet(itemes: [String]) async -> Int { ... }

// `await` must be in an `async` function
func foo() async {
public class GenericReturnType {
public static void main(String[] args) {
// 引数の型は `Object` なので戻り値から型パラメータが推論される
Integer n = id(42);
String s = id("xyz");
System.out.println(n);
System.out.println(s);
}
func foo() -> Int { return 2 }
func foo() -> Float { return 3.0 }
let a: Int = foo()
let b: Float = foo()
print(a) // 2
print(b) // 3.0