Skip to content

Instantly share code, notes, and snippets.

@inamiy
Last active November 1, 2020 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inamiy/9b21451e16c9b547ea804c663ad3cf2a to your computer and use it in GitHub Desktop.
Save inamiy/9b21451e16c9b547ea804c663ad3cf2a to your computer and use it in GitHub Desktop.
fileprivate enum cases in pseudo-Swift https://twitter.com/inamiy/status/1322750532197380096
/// 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