-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b061bdf
commit 0d6e5c2
Showing
2 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
## Enum subsets | ||
|
||
i.e. tagging a certain subset of enum cases as and then allowing conversion to/from that subset seamlessly | ||
|
||
``` | ||
enum Platform { | ||
subset ApplePlatform { | ||
func releaseYear() -> Int { | ||
switch self { | ||
case .macOS: 1998 // made up | ||
case .iOS: 2007 | ||
} | ||
} | ||
} | ||
case macOS[ApplePlatform] | ||
case iOS[ApplePlatform] | ||
case linux | ||
func name() -> String { | ||
switch self { | ||
case .macOS: "macOS" | ||
case .iOS: "iOS" | ||
case .linux: "Linux" | ||
} | ||
} | ||
} | ||
func doApple(_ platform: Platform.ApplePlatform) { | ||
print("{platform.name()} was released in {platform.releaseYear()}") | ||
} | ||
switch platform { | ||
case .macOS, .iOS: | ||
doApple(platform) | ||
case .linux: | ||
print("Linux has always been released and always will be") | ||
} | ||
``` | ||
|
||
## Enum relabelling | ||
|
||
Allow multiple versions of an enum with different labels; I often find myself with multiple | ||
versions of the same enum for different contexts, and there's tons of annoying glue code | ||
involved. | ||
|
||
``` | ||
enum Direction { | ||
case up | ||
case down | ||
case left | ||
case right | ||
} | ||
enum Edge relabelling Direction { | ||
case top[up] // would need to think about this syntax a bit longer | ||
case bottom[down] | ||
case leading[left] | ||
case trailing[right] | ||
} | ||
``` | ||
|
||
## Automatic 'kind' enums | ||
|
||
It can be useful to have both data enums and kind enums; yet there's usually a lot of boilerplate involved so | ||
they're avoided. | ||
|
||
``` | ||
enum OSVersion { | ||
case macOS(SemVer) | ||
case iOS(SemVer) | ||
case linux(KernelVersion) | ||
case windows(MSVersion) | ||
} | ||
enum OS = kind OSVersion // syntax needs work | ||
let version: OSVersion = .macOS(SemVer(11, 2, 5)) | ||
let os = OS(version) // OS.macOS | ||
``` | ||
|
||
## Follow Rust's lead with separating out conformance `impl`s from basic `impl`s | ||
|
||
From my experience with generics in the SwiftCrossUI project, I've found that it's quite common | ||
for conformances to silently become stale when default implementations are available which can | ||
cause some pretty subtle bugs. If `impl` blocks for conformances can only define members | ||
satisfying the conformance requirements then this whole source of bugs is eliminated. |
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