Skip to content

Instantly share code, notes, and snippets.

@philsquared
Created January 29, 2015 13:48
Show Gist options
  • Save philsquared/e229f566d883d2ce9a5f to your computer and use it in GitHub Desktop.
Save philsquared/e229f566d883d2ce9a5f to your computer and use it in GitHub Desktop.
Usage example for Result<T> in Swift
var key : String?
let res = getPathComponents( file )
>>= { (let components ) -> Result<FileTypes> in
key = components.filenameWithoutExt
return FileTypes.fromExt( components.ext )
}
>>= { (let type ) -> Result<Void> in
switch type {
case .Video, .Image:
var caption : String?
switch( resourcesByName[key!] ) {
case .Some(.CaptionFile( let c )):
caption = c
case .Some(.MediaFile(_)):
return .Fail("multiple media files with the same name" );
case .None:
break;
}
let r = Resource( type: Resource.Types( type ), filename: file, caption: caption )
resourcesByName[key!] = MediaOrCaption.MediaFile( r )
case .Text:
let mf = resourcesByName[key!]
switch( mf ) {
case .Some(.CaptionFile(_)):
return .Fail( "multiple caption files" );
case .Some(.MediaFile( var r )):
resourcesByName[key!] = MediaOrCaption.MediaFile( Resource( type: r.type, filename: r.filename, caption: file ) )
case .None:
resourcesByName[key!] = MediaOrCaption.CaptionFile( file )
}
}
return .Ok( Box<Void>() )
}
@philsquared
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment