I often write code like this:
<code>viewModel.containerSize = model.img != nil ? model.img!.size : .zero
</code>
<code>viewModel.containerSize = model.img != nil ? model.img!.size : .zero
</code>
viewModel.containerSize = model.img != nil ? model.img!.size : .zero
I would like to write code that looks like this:
<code>viewModel.containerSize = ~model.img ? .zero : model.img!.size
</code>
<code>viewModel.containerSize = ~model.img ? .zero : model.img!.size
</code>
viewModel.containerSize = ~model.img ? .zero : model.img!.size
or one that throws an error:
<code>enum ImageError: Error {
case notExist
}
viewModel.containerSize = try ~model.img ? .zero : model.img!.size !! throw ImageError.notExist
</code>
<code>enum ImageError: Error {
case notExist
}
viewModel.containerSize = try ~model.img ? .zero : model.img!.size !! throw ImageError.notExist
</code>
enum ImageError: Error {
case notExist
}
viewModel.containerSize = try ~model.img ? .zero : model.img!.size !! throw ImageError.notExist
This is a related topic, in which I recognise that “It is dangerous to be inside a function and wish to direct the order of code execution outside”.
I defined a prefixed static function ~
under Optional
, which comes from the actual writing of Noncopyable
as ~Copyable
, with the exact meaning of “Is this optional value nil?”, and more :
<code>// MARK: prefix IsNil
extension Optional {
/// isNil
@inlinable
public static prefix func ~ (value: Self) -> Bool {
switch value {
case .none:
true
case .some:
false
}
}
}
infix operator !!: NilCoalescingPrecedence
// MARK: unwrap or throw
extension Optional {
/// unwrap or throw
@inlinable
public static func !! (lhs: Self, rhs: Error) throws -> Wrapped {
guard let unwrapped = lhs else { throw rhs }
return unwrapped
}
}
### Is the code right?
</code>
<code>// MARK: prefix IsNil
extension Optional {
/// isNil
@inlinable
public static prefix func ~ (value: Self) -> Bool {
switch value {
case .none:
true
case .some:
false
}
}
}
infix operator !!: NilCoalescingPrecedence
// MARK: unwrap or throw
extension Optional {
/// unwrap or throw
@inlinable
public static func !! (lhs: Self, rhs: Error) throws -> Wrapped {
guard let unwrapped = lhs else { throw rhs }
return unwrapped
}
}
### Is the code right?
</code>
// MARK: prefix IsNil
extension Optional {
/// isNil
@inlinable
public static prefix func ~ (value: Self) -> Bool {
switch value {
case .none:
true
case .some:
false
}
}
}
infix operator !!: NilCoalescingPrecedence
// MARK: unwrap or throw
extension Optional {
/// unwrap or throw
@inlinable
public static func !! (lhs: Self, rhs: Error) throws -> Wrapped {
guard let unwrapped = lhs else { throw rhs }
return unwrapped
}
}
### Is the code right?
Usage:
<code>enum ImageError: Error {
case notExist
}
func giveContainerImgSize() throws {
viewModel.containerSize = try ~model.img ? .zero : model.img!.size !! ImageError.notExist
}
do {
giveContainerImgSize()
} catch {
print(error)
}
</code>
<code>enum ImageError: Error {
case notExist
}
func giveContainerImgSize() throws {
viewModel.containerSize = try ~model.img ? .zero : model.img!.size !! ImageError.notExist
}
do {
giveContainerImgSize()
} catch {
print(error)
}
</code>
enum ImageError: Error {
case notExist
}
func giveContainerImgSize() throws {
viewModel.containerSize = try ~model.img ? .zero : model.img!.size !! ImageError.notExist
}
do {
giveContainerImgSize()
} catch {
print(error)
}