├── .gitignore ├── LICENSE ├── Package.swift ├── README.md ├── Sources └── Undefined │ └── undefined.swift └── undefined.podspec /.gitignore: -------------------------------------------------------------------------------- 1 | .build 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Johannes Weiß 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.0 2 | 3 | import PackageDescription 4 | 5 | let package = Package( 6 | name: "swift-undefined", 7 | products: [ 8 | .library( 9 | name: "Undefined", 10 | targets: ["Undefined"]), 11 | ], 12 | dependencies: [], 13 | targets: [ 14 | .target(name: "Undefined", dependencies: []), 15 | ] 16 | ) 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # undefined for Swift 2 | 3 | _Micro frameworks_ are popular now, so I'll go _nano framework_ :-). This is all the code: 4 | 5 | ``` 6 | /** 7 | * `undefined()` pretends to be able to produce a value of any type `T` which can 8 | * be very useful whilst writing a program. It happens that you need a value 9 | * (which can be a function as well) of a certain type but you can't produce it 10 | * just yet. However, you can always temporarily replace it by `undefined()`. 11 | * 12 | * Inspired by Haskell's 13 | * [undefined](http://hackage.haskell.org/package/base-4.7.0.2/docs/Prelude.html#v:undefined). 14 | * 15 | * Invoking `undefined()` will crash your program. 16 | * 17 | * Some examples: 18 | * 19 | * - `let x : String = undefined()` 20 | * - `let f : String -> Int? = undefined("string to optional int function")` 21 | * - `return undefined() /* in any function */` 22 | * - `let x : String = (undefined() as Int -> String)(42)` 23 | * - ... 24 | * 25 | * What a crash looks like: 26 | * 27 | * `fatal error: undefined: main.swift, line 131` 28 | * 29 | */ 30 | public func undefined(hint: String = "", file: StaticString = #file, line: UInt = #line) -> T { 31 | let message = hint == "" ? "" : ": \(hint)" 32 | fatalError("undefined \(T.self)\(message)", file:file, line:line) 33 | } 34 | ``` 35 | 36 | ## More information 37 | 38 | See my slides about [`undefined()`][fltts-undef] from my [Further Leveraging the Type System][fltts] talk at [Swift Summit 2015][ss15]. 39 | 40 | [fltts]: https://speakerdeck.com/johannesweiss/further-leveraging-the-type-system 41 | [fltts-undef]: https://speakerdeck.com/johannesweiss/further-leveraging-the-type-system?slide=6 42 | [ss15]: http://www.swiftsummit.com 43 | -------------------------------------------------------------------------------- /Sources/Undefined/undefined.swift: -------------------------------------------------------------------------------- 1 | /** 2 | * `undefined()` pretends to be able to produce a value of any type `T` which can 3 | * be very useful whilst writing a program. It happens that you need a value 4 | * (which can be a function as well) of a certain type but you can't produce it 5 | * just yet. However, you can always temporarily replace it by `undefined()`. 6 | * 7 | * Inspired by Haskell's 8 | * [undefined](http://hackage.haskell.org/package/base-4.7.0.2/docs/Prelude.html#v:undefined). 9 | * 10 | * Invoking `undefined()` will crash your program. 11 | * 12 | * Some examples: 13 | * 14 | * - `let x : String = undefined()` 15 | * - `let f : String -> Int? = undefined("string to optional int function")` 16 | * - `return undefined() /* in any function */` 17 | * - `let x : String = (undefined() as Int -> String)(42)` 18 | * - ... 19 | * 20 | * What a crash looks like: 21 | * 22 | * `fatal error: undefined: main.swift, line 131` 23 | * 24 | */ 25 | public func undefined(hint: String = "", type: T.Type = T.self, file: StaticString = #file, line: UInt = #line) -> T { 26 | let message = hint == "" ? "" : ": \(hint)" 27 | fatalError("undefined \(T.self)\(message)", file:file, line:line) 28 | } 29 | -------------------------------------------------------------------------------- /undefined.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = "undefined" 3 | s.version = "0.0.1" 4 | s.summary = "Nano framework which defines Haskell's undefined in Swift." 5 | s.homepage = "https://github.com/weissi/swift-undefined" 6 | s.license = "MIT" 7 | 8 | s.author = { "Johannes Weiss" => "public@tux4u.de" } 9 | s.social_media_url = "https://twitter.com/johannesweiss" 10 | 11 | s.ios.deployment_target = '8.0' 12 | s.osx.deployment_target = '10.9' 13 | s.requires_arc = true 14 | 15 | s.source = { :git => "https://github.com/weissi/swift-undefined.git", 16 | :tag => s.version } 17 | s.source_files = "Sources/Undefined/*.swift" 18 | end 19 | --------------------------------------------------------------------------------