-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
2 changed files
with
60 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,59 @@ | ||
// | ||
// Value.swift | ||
// FoundationX | ||
// | ||
// Created by Guanghui Liang on 2024/1/21. | ||
// Copyright © 2024 Guanghui Liang. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol DefaultValue { | ||
associatedtype ValueType: Equatable | ||
static var defaultValue: ValueType { get } | ||
} | ||
|
||
public enum Value<T>: Equatable where T: Equatable { | ||
case none | ||
case some(value: T) | ||
} | ||
|
||
extension Value where T: DefaultValue, T.ValueType == T { | ||
public var realValue: T { | ||
if case let Self.some(value) = self { | ||
return value | ||
} else { | ||
return T.defaultValue | ||
} | ||
} | ||
} | ||
|
||
extension Bool: DefaultValue { | ||
public static var defaultValue: Bool { | ||
return false | ||
} | ||
} | ||
|
||
extension Int: DefaultValue { | ||
public static var defaultValue: Int { | ||
return 0 | ||
} | ||
} | ||
|
||
extension Float: DefaultValue { | ||
public static var defaultValue: Self { | ||
return 0.0 | ||
} | ||
} | ||
|
||
extension Double: DefaultValue { | ||
public static var defaultValue: Self { | ||
return 0.0 | ||
} | ||
} | ||
|
||
extension String: DefaultValue { | ||
public static var defaultValue: Self { | ||
return "" | ||
} | ||
} |
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