如何在不必为每种情况实现枚举功能的情况下访问特定案例的枚举值?
我在想这样的事情:
enum Result<S, F> {
case success(S)
case failure(F)
}
let resultSuccess = Result<Int, String>.success(1)
let resultFailure = Result<Int, String>.failure("Error")
let error: String? = case let .failure(error) = resultFailure
这样的事情也是可以接受的:
let error: String? = (case let .failure(error) = resultFailure) ? error : ""
显然这甚至都没有编译,但这是我想要的要点。
这可能吗?
编辑:解释为什么我的问题与Extract associated value of enum case into a tuple不一样
这很简单,该答案可以访问if
语句中的值,而不是我问题中的Optional
。要指定一点,想象一下我必须从多个枚举中提取值,并且可能在枚举中枚举,因为我需要嵌套的if
或guard
,但是guard
1}}我无法继续流程,因为它会强制return
。
答案 0 :(得分:1)
分别为成功案例和失败案例添加两个计算属性。
httpd_can_connect_ldap --> on
答案 1 :(得分:0)
我担心这是你能得到的最多:
#include <iostream>
class DerivedStuff1 {
public:
static void eval() { std::cout << "Evaluating DerivedStuff1\n"; }
};
class DerivedStuff2 {
public:
static void eval() { std::cout << "Evaluating DerivedStuff2\n"; }
};
class DerivedClass1; class DerivedClass2;
class BaseClassVisitor {
public:
virtual void visit(DerivedClass1&) = 0;
virtual void visit(DerivedClass2&) = 0;
};
class BaseClass {
public:
virtual void accept(BaseClassVisitor& v) = 0;
};
class DerivedClass1 : public BaseClass
{
public:
virtual void accept(BaseClassVisitor& v) { v.visit(*this); }
};
class DerivedClass2 : public BaseClass
{
public:
virtual void accept(BaseClassVisitor& v) { v.visit(*this); }
};
template <typename StuffType>
class EvalVisitor : public BaseClassVisitor
{
virtual void visit(DerivedClass1&) {
std::cout << "We are in DerivedClass1: ";
StuffType::eval();
}
virtual void visit(DerivedClass2&) {
std::cout << "We are in DerivedClass2: ";
StuffType::eval();
}
};
int main()
{
EvalVisitor<DerivedStuff1> e1;
EvalVisitor<DerivedStuff2> e2;
BaseClass* c1 = new DerivedClass1;
c1->accept(e1);
c1->accept(e2);
BaseClass* c2 = new DerivedClass2;
c2->accept(e1);
c2->accept(e2);
return 0;
}
答案 2 :(得分:0)
问题"Accessing an Enumeration association value in Swift"中的这种技术将会这样做:
/* general */
*, .border-box {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
/* tr.output */
tr.output {
vertical-align: top;
}
tr.output > td > * {
width: 100%;
}
这也将取代&#34;如果情况允许=&#34;构建体:
<table>
<tr class="input">
<td align="center" bgcolor="#D4D4D4">
<select id="inventory" size="1">
<option id="0" value="0">Seleccione un equipo</option>
<option id="bm260W" value="8060">Boviet Module(260W)</option>
<option id="bm300W/305W/310W" value="156">Boviet Module(300/305/310W)</option>
</select>
</td>
<td align="center" bgcolor="#D4D4D4">
<input type="number" id="cantidad" placeholder="Ex: 5" size="1">
</td>
</tr>
<tr class="actions">
<td colspan="2" align="center" bgcolor="#D4D4D4">
<!-- Añadir -->
<button type="button" id="anadir" onclick="anadirEquipo()">Añadir</button>
<!-- Retirar -->
<button type="button" id="retirar" onclick="retirarEquipos()">Retirar</button>
</td>
</tr>
<tr>
<th align="center" bgcolor="#2AD2C9">
<h4>Equipo(s) añadido(s):</h2>
</th>
<th align="center" bgcolor="#2AD2C9">
<h4>Total:</h2>
</th>
</tr>
<tr class="output">
<td>
<select id="equipos" multiple readonly>
</select>
</td>
<td>
<input type="number" id="quantity" value="0" readonly />
</td>
</tr>
在这里,1行并使用AggregationStrategy aggregationStrategy = ...
from("direct:start")
.enrich("direct:resource", aggregationStrategy)
.to("direct:result");
from("direct:resource")
:
enum Result<S, F> {
case success(S)
case failure(F)
}
let resultFailure = Result<Int, String>.failure("Error")
var error: String?
if case let Result.failure(value) = resultFailure {
error = value
}
print(error) // Optional("Error")