Skip to content

Instantly share code, notes, and snippets.

View hborders's full-sized avatar

Heath Borders hborders

View GitHub Profile
@hborders
hborders / KotlinExtensions.kt
Created September 27, 2018 21:23
Some confusion over Kotlin extension methods
open class D {}
class D1: D() {}
fun D.foo() {
println("D.foo")
}
// this seems like an override, but isn't
// It's good that the compiler doesn't let
// me add `override`, but it's still not clear
@hborders
hborders / MissingReturn.m
Last active May 14, 2018 20:11
An example of the compiler error when get when we miss a return that a block requires
#import "Example.h" // https://gist.github.com/hborders/2af9b39e27b62ef9e68c65085126fe4a
- (void)missingReturn {
Example * _Nonnull fooExample = [[ExampleFoo alloc] initWithF:@"foo"
g:@"goo"];
NSNumber * _Nonnull number =
[ExampleSwitcher<NSNumber *> nonnullExampleFrom:fooExample
switchFoo:^(ExampleFoo * _Nonnull foo_) {
return @(foo_.f.length + foo_.g.length);
}
@hborders
hborders / GenericIsKindOfClass.m
Created October 23, 2017 14:27
How to use Objective-C Generics with -isKindOfClass:
#import "MyNonClusteredClass.h"
@interface GenericIsKindOfClass<ObjectType : MyNonClusteredClass *> : NSObject
+ (instancetype _Nonnull)new NS_UNAVAILABLE;
- (instancetype _Nonnull)init NS_UNAVAILABLE;
- (instancetype _Nonnull)initWithInstanceOfObjectType:(ObjectType _Nonnull)instanceOfObjectType NS_DESIGNATED_INITIALIZER;
- (ObjectType _Nullable)typeSafeDowncastOrNil:(NSObject * _Nonnull)object;
@hborders
hborders / JiveCommon.podspec
Created October 24, 2015 16:10
Problems integrating Crashlytics and Fabric into a cocoapods project that consumes a Swift pod.
#
# Be sure to run `pod lib lint JiveCommon.podspec' to ensure this is a
# valid spec and remove all comments before submitting the spec.
#
# Any lines starting with a # are optional, but encouraged
#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
@hborders
hborders / gist:7701826
Last active March 17, 2017 13:56
How to safely work with unicode strings in Java: Inspired by http://mortoray.com/2013/11/27/the-string-type-is-broken/
import java.text.BreakIterator;
public class UnicodeTest {
public static void main(String[] args) {
String noel = "noe\u0308l";
System.out.println("noel=" + noel);
System.out.println("Broken (java.lang.String methods):");
System.out.println("noel.length=" + noel.length());
System.out.println("noel.substring(0,3)=" + noel.substring(0, 3));
@hborders
hborders / BadUnpack.m
Created March 13, 2017 19:08
Trying to unpack the wrong distinct values from a sum type
static void badPrintExample(Example * _Nonnull example) {
[example switchFoo:^(ExampleBar * _Nonnull bar_) {
// ^
// error: incompatible block pointer types sending 'void (^)(ExampleBar * _Nonnull __strong)' to parameter of type 'void (^ _Nonnull)(ExampleFoo * _Nonnull __strong)'
NSLog(@"Bar: %@, %@", @(bar_.b), @(bar_.c));
}
bar:^(ExampleFoo * _Nonnull foo_) {
// ^
// error: incompatible block pointer types sending 'void (^)(ExampleFoo * _Nonnull __strong)' to parameter of type 'void (^ _Nonnull)(ExampleBar * _Nonnull __strong)'
NSLog(@"Foo: %@, %@", foo_.f, foo_.g);
@hborders
hborders / BadInstantiation.m
Created March 13, 2017 19:06
Trying to instantiate a distinct type of a sum type with the wrong parameters
- (void)testWrongValues {
Example * _Nonnull fooExample = [[ExampleFoo alloc] initWithB:2 c:3];
// ^
// error: no visible @interface for 'ExampleFoo' declares the selector 'initWithB:c:'
Example * _Nonnull barExample = [[ExampleBar alloc] initWithF:@"foo" g:@"goo"];
// ^
// error: no visible @interface for 'ExampleBar' declares the selector 'initWithF:g:'
}
@hborders
hborders / ExampleSwitcher.m
Last active March 13, 2017 18:45
An Objective-C pattern for making sum types - ExampleSwitcher.m
#import "ExampleSwitcher.h" // https://gist.github.com/hborders/f378b4e0d0f1d74ff7c8149cb04c6268
@implementation ExampleSwitcher
+ (NSObject * _Nonnull)nonnullValueFrom:(Example * _Nonnull)example
switchFoo:(NSObject * _Nonnull (^ _Nonnull)(ExampleFoo * _Nonnull foo_))fooBlock
bar:(NSObject * _Nonnull (^ _Nonnull)(ExampleBar * _Nonnull bar_))barBlock {
NSParameterAssert(example);
NSParameterAssert(fooBlock);
NSParameterAssert(barBlock);
@hborders
hborders / MissingAssignment.m
Created March 13, 2017 18:05
Only assigning to a __block variable in one of the blocks. I wish this was an error.
#import "Example.h" // https://gist.github.com/hborders/2af9b39e27b62ef9e68c65085126fe4a
- (void)missingAssignment {
Example * _Nonnull fooExample = [[ExampleFoo alloc] initWithF:@"foo"
g:@"goo"];
NSNumber * _Nonnull __block number;
[fooExample switchFoo:^(ExampleFoo * _Nonnull foo_) {
number = @(foo_.f.length + foo_.g.length);
}
bar:^(ExampleBar * _Nonnull bar_) {
@hborders
hborders / ExampleTestsTestTransformExample.m
Created March 13, 2017 16:51
An Objective-C pattern for making sum types - ExampleTestsTestTransformExample.m
#import "Example.h" // https://gist.github.com/hborders/e00d7ff965ca8f8df8f7e762a2269efb
#import "ExampleSwitcher.h" // https://gist.github.com/hborders/f378b4e0d0f1d74ff7c8149cb04c6268
#import "ExampleFoo.h" // https://gist.github.com/hborders/094f05d932b3d7a1e389184ba525b0c3
#import "ExampleBar.h" // https://gist.github.com/hborders/f1c231a89de11157adf46ee85880733a
static NSNumber * _Nonnull transformExample(Example * _Nonnull example) {
return [ExampleSwitcher<NSNumber *> nonnullValueFrom:example
switchFoo:^NSNumber * _Nonnull (ExampleFoo * _Nonnull foo_) {
return @(foo_.f.length + foo_.g.length);
}