Skip to content

Commit

Permalink
feat: Add Value Enum.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lguanghui committed Jan 21, 2024
1 parent 149f18d commit 0f716e0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
59 changes: 59 additions & 0 deletions Sources/FoundationX/Value.swift
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 ""
}
}
2 changes: 1 addition & 1 deletion Sources/FoundationX/XLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Logger.swift
// FoundationX
//
// Created by 梁光辉 on 2023/3/8.
// Created by Guanghui Liang on 2023/3/8.
// Copyright © 2023 Guanghui Liang. All rights reserved.
//

Expand Down

0 comments on commit 0f716e0

Please sign in to comment.