Created
January 29, 2015 13:48
-
-
Save philsquared/e229f566d883d2ce9a5f to your computer and use it in GitHub Desktop.
Usage example for Result<T> in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() ) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uses https://gist.github.com/philsquared/1d267d823130eaf7641a