Skip to content

Instantly share code, notes, and snippets.

dispatch_block_t RecursiveBlock(void (^block)(dispatch_block_t recurse))
{
// assuming ARC, so no explicit copy
return ^{ block(RecursiveBlock(block)); };
}
typedef void (^OneParameterBlock)(id parameter);
OneParameterBlock RecursiveBlock1(void (^block)(OneParameterBlock recurse, id parameter))
{
import Cocoa
enum CoroutineState {
case Fresh, Running, Blocked, Canceled, Done
}
struct CoroutineCancellation: ErrorType {}
class CoroutineImpl<InputType, YieldType> {
let body: (yield: YieldType throws -> InputType) throws -> Void
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
class Printer {
public:
const char *str;
~Printer() { puts(str); }
};
func effIt<Target, Result>(_ keypath: KeyPath<Target, Result>) -> (Target) -> Result {
return { $0[keyPath: keypath] }
}
struct Foo {
var x: Int
var y: Int
}
#import <Foundation/Foundation.h>
int main(int argc, char **argv) {
auto global = ^{ NSLog(@"Hello, world!"); };
auto captures = ^{ NSLog(@"argc is %d", argc); };
NSLog(@"global: %@ - captures: %@", global, captures);
NSLog(@"(void *)global: %p - (void *)captures: %p", (void *)global, (void *)captures);
id globalCopy = [global copy];
id capturesCopy = [captures copy];
func ??<T>(lhs: T?, rhs: @autoclosure () throws -> Never) rethrows -> T {
guard let value = lhs else { try rhs() }
return value
}
func ??<T>(lhs: T?, rhs: () throws -> Never) rethrows -> T {
guard let value = lhs else { try rhs() }
return value
}
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
Dl_info info;
int success = dladdr(main, &info);
if (success == 0) abort();
printf("main is at %p and the mach-o header that contains it is at %p\n", main, info.dli_fbase);
}
class C {}
protocol P where Self: C {
var x: Int { get }
var y: Int { get }
}
extension P {
var x: Int { return 42 }
}
// clang -fobjc-arc -framework Foundation runtime-class.m
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface Person : NSObject
- (id)initWithFirstName: (NSString *)firstName lastName: (NSString *)lastName age: (NSUInteger)age;
@mikeash
mikeash / gist:1132620
Created August 8, 2011 20:17
Namespaced constants in C
#import <Foundation/Foundation.h>
// .h file
struct MyConstantsStruct
{
NSString *foo;
NSString *bar;