Skip to content

A "garbage" Optional utility that allows returning a default value

License

Notifications You must be signed in to change notification settings

nythepegasus/Defaultable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Defaultable

A "garbage" Optional utility that allows returning a default value instead of having to nil-coalesce.

Example:

// OLD

let ny: [UInt8] = [78, 121]
let s = String(bytes: ny, encoding: .ascii)
// returns `String?` even though we're pretty certain we have ascii :/
// NEW

import Defaultable

public extension String: Defaultable {
  static var DV: String { "Default String" } // Best for debug values/etc
}

let ny: [UInt8] = [78, 121]
let s = String(bytes: ny, encoding: .ascii)~
// returns `String`, which will either be "Default String" or the value we expect :3 

This package also contains some basic Foundation default types that you can utilize instead of having to define them yourself in DefaultableFoundation.

An example:

import Defaultable
import DefaultableFoundation

let ny: [UInt8] = [78, 121]
let s = String(bytes: ny, encoding: .ascii)~
// returns `String`, which will either be "" or the value we expect :3 

About

A "garbage" Optional utility that allows returning a default value

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages