Created
March 4, 2019 02:43
-
-
Save superarts/8bdedc39a8516e0628eb236a06e79e37 to your computer and use it in GitHub Desktop.
Extension Computed Property Behavior
This file contains hidden or 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
// To maintain state of extension computed property, use static variable | |
// A console logger | |
protocol ConsoleLoggable { | |
var logger: ConsoleLoggerProtocol { get } | |
} | |
extension ConsoleLoggable { | |
var logger: ConsoleLoggerProtocol { | |
print("New instance...") | |
return DefaultConsoleLogger() | |
} | |
} | |
protocol ConsoleLoggerProtocol { | |
func log() | |
} | |
struct DefaultConsoleLogger: ConsoleLoggerProtocol { | |
func log() { | |
print("CONSOLE log...") | |
} | |
} | |
// A file logger | |
protocol FileLoggable { | |
var logger: FileLoggerProtocol { get } | |
} | |
extension FileLoggable { | |
var logger: FileLoggerProtocol { | |
return DefaultFileLogger() | |
} | |
} | |
protocol FileLoggerProtocol { | |
func log() | |
} | |
struct DefaultFileLogger: FileLoggerProtocol { | |
func log() { | |
print("FILE log...") | |
} | |
} | |
// Test | |
struct LoggerTest: ConsoleLoggable { | |
//struct LoggerTest: FileLoggable, ConsoleLoggable { | |
func test() { | |
logger.log() | |
logger.log() | |
//clogger.log() | |
//flogger.log() | |
} | |
} | |
LoggerTest().test() | |
/** | |
New instance... | |
CONSOLE log... | |
New instance... | |
CONSOLE log... | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment