A phantom type is a custom type that has one or more unused type parameters.

Overview

PhantomTypes

A phantom type is a custom type that has one or more unused type parameters.

Phantom types allow you to enforce type-safety without sacrificing readability. It also reduces the amount of tests we need to write because silly mistakes are forbidden by the compiler.

If you're keen to know more, I recommend you read my medium article.

Example

Consider we have a User object like this...

struct User {
  var email: Email = ""
  var name: Name = ""
  var age: Age = 18
  var isAdmin: IsAdmin = false
}

If a developer tries to set a name value to the email property we will get a type-mismatch error from the compiler.

How can we enforce type-safety?

enum Types {
  enum Email {}
  enum Name {}
  enum Age {}
  enum IsAdmin {}
}

typealias Email = Phantom<Types.Email, String>
typealias Name = Phantom<Types.Name, String>
typealias Age = Phantom<Types.Age, Int>
typealias Location = Phantom<Types.IsAdmin, Bool>

Adding even more context

We might even consider making them even more explicit, so a User's email can't be assigned to another Email field in the code, such as the Login email...

protocol EmailHaving {}
extension EmailHaving {
  typealias Email = Phantom<Phantom<Self, Types.Email>, String>
}
extension User: EmailHaving {}
extension Login: EmailHaving {}

User().email = Login().email // type-mismatch error!
You might also like...
A type-safe packet processor framework in Swift

PacketProcessor The Swift PacketProcessor provides a simple, type-safe way of handling structured packets given a data stream. PacketProcessor handles

How to use swiftlint to identify unused code or unused imports in a Swift codebase

Swift compilation database This repository demonstrates how to use swiftlint to identify unused code or unused imports in a Swift codebase. How to run

Protected is a Swift Package that allows you to specify the read and write rights for any type, depending on context by using Phantom types
Protected is a Swift Package that allows you to specify the read and write rights for any type, depending on context by using Phantom types

Protected is a Swift Package that allows you to specify the read and write rights for any type, depending on context by using Phantom types

iOS routing done right. Handles both URL recognition and controller displaying with parsed parameters. All in one line, controller stack preserved automatically!
iOS routing done right. Handles both URL recognition and controller displaying with parsed parameters. All in one line, controller stack preserved automatically!

Developed and Maintained by Ipodishima Founder & CTO at Wasappli Inc. (If you need to develop an app, get in touch with our team!) So what is this lib

Phantom Anonymous Messenger for iOS

๐Ÿ‘ป Phantom for iOS This repository contains the complete source code of Phantom for iOS. Table of Contens ๐Ÿ›  Report Bugs/Feature Requests/Security Iss

TwilioChat_iOS - Twilio iOS SDK Implementaion Chat one-one Chat One-Many (Group)

TwilioChat_iOS - Twilio iOS SDK Implementaion Chat one-one Chat One-Many (Group) - Add Participant - Remove Participant Send Attachment Image Android - iOS Tested iOS - iOS Tested iOS - Android Tested React to Message, Delete a Message Read, Delivered, Sent Delete a Conversation Unread Messages Filter

This little app aims to help teach me how to implement more then one API in one single application in a reusable and properly structured manner.

LilAPI App News & Weather This little API app combines two of Jordan Singers Lil Software API's into one app. The goal with this app was to learn how

A tool for finding missing and unused NSLocalizedStrings

nslocalizer This is a command line tool that is used for discovering missing and unused localization strings in Xcode projects. Contributing and Code

Command line program that detects unused resource strings in an iOS or OS X application.

Abandoned Resource String Detection This command line program detects unused resource strings in an iOS or OS X application. Updated to Swift 3, thank

๐ŸŒŠ A clean wave - report unused localized strings
๐ŸŒŠ A clean wave - report unused localized strings

Ripple ๐ŸŒŠ Ripple - a command line tool that reports unused localization strings in your Xcode project. Install Clone from repo git clone cd Ripple sw

Menubar app to remove link tracking parameters automatically
Menubar app to remove link tracking parameters automatically

TrackerZapper Website and more info TrackerZapper is a Mac app that sits in your menubar and silently removes tracking parameters from any links you c

URLScheme router than supports auto creation of UIViewControllers for associated url parameters to allow creation of navigation stacks

IKRouter What does it do? Once you have made your UIViewControllers conform to Routable you can register them with the parameters that they represent

๐Ÿ”ป Dropdown Menu for iOS with many customizable parameters to suit any needs
๐Ÿ”ป Dropdown Menu for iOS with many customizable parameters to suit any needs

MKDropdownMenu Dropdown Menu for iOS with many customizable parameters to suit any needs. Inspired by UIPickerView. Installation CocoaPods MKDropdownM

A powerful Circular Slider. It's written in Swift, it's 100% IBDesignable and all parameters are IBInspectable.
A powerful Circular Slider. It's written in Swift, it's 100% IBDesignable and all parameters are IBInspectable.

CircularSlider A powerful Circular Slider. It's written in Swift, it's 100% IBDesignable and all parameters are IBInspectable. Demo Installation Circu

ParametersCloneSwiftUI - Parameters of iOS in iPhone

ParametersCloneSwiftUI Made by myself during the week-end, only made for light m

PagingKit provides customizable menu UI. It has more flexible layout and design than the other libraries.
PagingKit provides customizable menu UI. It has more flexible layout and design than the other libraries.

PagingKit provides customizable menu & content UI. It has more flexible layout and design than the other libraries. What's this? There are many librar

LabeldStepper - A native SwiftUI stepper that shows the current value and has more features
LabeldStepper - A native SwiftUI stepper that shows the current value and has more features

LabeledStepper A native SwiftUI Stepper that shows the value with more features!

A simple shake-one-shake, Convenient for us to integrate the shake-one-shake.

MGSwiftShaker Example To run the example project, clone the repo, and run pod install from the Example directory first.

Simple class to check if app has been cracked, being debugged or enriched with custom dylib

iOS-App-Security-Class Simple class to check if iOS app has been cracked, being debugged or enriched with custom dylib and as well detect jailbroken e

Releases(v1.0.8)
Owner
https://medium.com/@cjnevin
null
A lightweight, one line setup, iOS / OSX network debugging library! ๐ŸฆŠ

Netfox provides a quick look on all executed network requests performed by your iOS or OSX app. It grabs all requests - of course yours, requests from

Christos Kasketis 3.4k Dec 28, 2022
Digger is a lightweight download framework that requires only one line of code to complete the file download task

ไธญๆ–‡่ฏดๆ˜Ž Digger is a lightweight download framework that requires only one line of code to complete the file download task. Based on URLSession, pure Swif

Ant 543 Oct 29, 2022
A tool to build projects on MacOS and a remote linux server with one command

DualBuild DualBuild is a command line tool for building projects on MacOS and a remote Linux server. ##Setup Install the repository git clone https://

Operator Foundation 0 Dec 26, 2021
๐ŸŒ Makes Internet connectivity detection more robust by detecting Wi-Fi networks without Internet access.

Connectivity is a wrapper for Apple's Reachability providing a reliable measure of whether Internet connectivity is available where Reachability alone

Ross Butler 1.6k Dec 30, 2022
Server-side Swift. The Perfect core toolset and framework for Swift Developers. (For mobile back-end development, website and API development, and moreโ€ฆ)

Perfect: Server-Side Swift ็ฎ€ไฝ“ไธญๆ–‡ Perfect: Server-Side Swift Perfect is a complete and powerful toolbox, framework, and application server for Linux, iO

PerfectlySoft Inc. 13.9k Jan 6, 2023
Deal with query items, HTTP headers, request body and more in an easy, declarative way

Reusable system for complex URL requests with Swift. Deal with query items, HTTP headers, request body and more in an easy, declarative way. Check out our engineering blog to learn more!

Parable Health 19 Sep 5, 2022
A custom wrapper over AFNetworking library that we use inside RC extensively

AFNetworkingHelper A very simple wrapper over the most amazing networking library for objective C, AFNetworking. We extensively use it inside RC and i

Betacraft 16 Aug 3, 2021
A type-safe, high-level networking solution for Swift apps

What Type-safe network calls made easy Netswift offers an easy way to perform network calls in a structured and type-safe way. Why Networking in Swift

Dorian Grolaux 23 Apr 27, 2022
Commonly available global error type packages

ErrorKit ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋Š” ์ „์—ญ ์˜ค๋ฅ˜ ํƒ€์ž… ํŒจํ‚ค์ง€. Requirements Xcode 13.2.1+ Swift 5.5+ Ins

Jaemyeong Jin 0 Nov 6, 2022
Swift-multipart-formdata - MultipartFormData: Build multipart/form-data type-safe in Swift

MultipartFormData Build multipart/form-data type-safe in Swift. A result builder

Felix Herrmann 21 Dec 29, 2022