Last active
November 1, 2020 04:03
-
-
Save inamiy/9b21451e16c9b547ea804c663ad3cf2a to your computer and use it in GitHub Desktop.
fileprivate enum cases in pseudo-Swift https://twitter.com/inamiy/status/1322750532197380096
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
/// Pseudo-Swift: public / fileprivate enum cases | |
public enum Action { | |
public case foo | |
public case bar(Int) | |
// fileprivate enum cases, where pattern matches are only allowed in this file. | |
// From outside, only using switch statement's `default` is possible. | |
// (NOTE: `private` won't make sense) | |
// | |
// This is useful to define private-Action values. | |
fileprivate case baz1 | |
fileprivate case baz2(Bool) | |
} | |
// is essentially equal (isomorphic) to the following workaround Swift code: | |
public enum Action { | |
case foo | |
case bar(Int) | |
case baz1(Signature) | |
case baz2(Bool, Signature) | |
/// Public type, but only initializable in this file | |
/// so that `.baz1` and `.baz2` are never initializable (never exists) from outside. | |
public struct Signature { | |
fileprivate init() {} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment