-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generating properties and closures for enums
- Loading branch information
1 parent
9e3f066
commit 92ac976
Showing
7 changed files
with
74 additions
and
47 deletions.
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,31 @@ | ||
/* | ||
Given the following enum | ||
|
||
``` | ||
enum ViewState<T>: EnumClosures { | ||
case start(data: T) | ||
case loading | ||
case completed(count: Int, message: String) | ||
} | ||
``` | ||
|
||
will generate the following code | ||
|
||
``` | ||
extension ViewState { | ||
internal func onStart(_ fn: (T) -> Void) { | ||
guard case let .start(data) = self else { return } | ||
fn(data) | ||
} | ||
internal func onLoading(_ fn: () -> Void) { | ||
guard case .loading = self else { return } | ||
fn() | ||
} | ||
internal func onCompleted(_ fn: (Int, String) -> Void) { | ||
guard case let .completed(count, message) = self else { return } | ||
fn(count, message) | ||
} | ||
} | ||
``` | ||
*/ | ||
public protocol EnumClosures {} |
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 |
---|---|---|
@@ -1,5 +1,2 @@ | ||
public protocol EnumClosures {} | ||
|
||
public protocol EnumProperties {} | ||
|
||
// See `EnumClosures` and `EnumProperties` | ||
public protocol EnumPoetry: EnumClosures, EnumProperties {} |
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,40 @@ | ||
/* | ||
Given the following enum | ||
|
||
``` | ||
enum ViewState<T>: EnumProperties { | ||
case start(T) | ||
case loading | ||
case completed(Int, String) | ||
} | ||
``` | ||
|
||
will generate the following code | ||
|
||
``` | ||
extension ViewState { | ||
internal var isStart: Bool { | ||
guard case .start = self else { return false } | ||
return true | ||
} | ||
internal var isLoading: Bool { | ||
guard case .loading = self else { return false } | ||
return true | ||
} | ||
internal var isCompleted: Bool { | ||
guard case .completed = self else { return false } | ||
return true | ||
} | ||
|
||
internal var start: T? { | ||
guard case let .start(data) = self else { return nil } | ||
return (data) | ||
} | ||
internal var completed: (count: Int, message: String)? { | ||
guard case let .completed(count, message) = self else { return nil } | ||
return (count, message) | ||
} | ||
} | ||
``` | ||
*/ | ||
public protocol EnumProperties {} |
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
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
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
This file was deleted.
Oops, something went wrong.