Skip to content

Instantly share code, notes, and snippets.

View chris-hatton's full-sized avatar
👦
Intrepid

Chris Hatton chris-hatton

👦
Intrepid
View GitHub Profile
@chris-hatton
chris-hatton / example.swift
Last active August 3, 2017 17:57
Swift Protocol Extension and Generics
protocol MyProtocol
{
typealias GenType
func doSomething(param: GenType)
}
class MyObject<T> : MyProtocol
{
typealias GenType = T
@chris-hatton
chris-hatton / SelfGenericTypeTest.swift
Last active November 9, 2015 19:27
A test to see whether Swift's Generics system can carry through a self-derived type, which is possible, and often useful, in Java.
class A<SelfType : A<SelfType>>
{
}
class B : A<B> // Line fails to compile with: "'A' requires that 'B' inherit from 'A<B>'" even though it does
{
}
@chris-hatton
chris-hatton / GenericOverloadedFunctionTest.swift
Last active December 31, 2015 21:36
Significance of top-level definition of overloaded functions
protocol ProtocolA {}
protocol ProtocolB {}
func overloadingTest <T where T: ProtocolA>( obj: T ) -> String
{
return "It's an implementation of Protocol A"
}
func overloadingTest <T where T: ProtocolB>( obj: T ) -> String // This compiles fine, and the appropriate 'topLevelTest' function is bound
protocol ProtocolA {}
protocol ProtocolB {}
func overloadingTest ( obj: String ) -> String
{
return "It's a String"
}
func overloadingTest ( obj: Int ) -> String // This compiles fine, and the appropriate 'topLevelTest' function is called
@chris-hatton
chris-hatton / nillableReturnTypeTest.swift
Last active February 8, 2016 04:28
A test of inferred return types in Swift, both generic and overloaded nillable
import UIKit
class A : CustomDebugStringConvertible
{
let message: String
required init( message: String ) { self.message = message }
var debugDescription: String { return message }
}
@chris-hatton
chris-hatton / HTTPStatus.swift
Last active May 17, 2016 04:43
Implementation of HTTP status codes as Swift enumerations, with descriptions
//
// HTTPStatus.swift
//
// Created by Chris Hatton on 17/05/2016.
//
// Transcribed from: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
//
import Foundation
@chris-hatton
chris-hatton / SteamVR_Controller_Extension_GetPressedDPadButton.cs
Last active January 29, 2020 22:23
Fragment to get pressed D-Pad button from SteamVR controller in Unity
static class SteamVR_Controller_Extension
{
const float threshold = 0.3f;
/*
* You might expect that pressing one of the edges of the SteamVR controller touchpad could
* be detected with a call to device.GetPress( EVRButtonId.k_EButton_DPad_* ), but currently this always returns false.
* Not sure whether this is SteamVR's design intent, not yet implemented, or a bug.
* The expected behaviour can be achieved by detecting overall Touchpad press, with Touch-Axis comparison to an edge threshold.
*/
@chris-hatton
chris-hatton / MonoBehaviourInvokeExtension.cs
Last active July 18, 2016 12:57
Adds a better co-routine + delegate based Invoke to MonoBehaviour
using System;
using System.Collections;
using UnityEngine;
/*
* Use by explicit 'this' e.g. this.Invoke( 3.0f, delegate { doSomething(); } );
*/
static class MonoBehaviourInvokeExtension
{
public static void Invoke( this MonoBehaviour monoBehaviour, float delay, Action action )
@chris-hatton
chris-hatton / AuthModel.swift
Created September 9, 2016 02:55
Associated types and Generic constraints not playing nice in Swift 3.0
import UIKit
protocol HTTPRequest
{
associatedtype ResultType
func start( callback: (ResultType)->() )
}
@chris-hatton
chris-hatton / fatalErrorTest.swift
Last active September 15, 2016 22:46
Test of fatalError() backed by @NoReturn (Swift2) vs. Never (Swift3)
// This compiles in both Swift 2 and 3
func test1() -> Int {
fatalError()
}
// This compiled in Swift2 but no longer does in Swift 3
let test2 : () -> Int = {
fatalError()
}