我有一个类似于以下的 Swift 单元测试:
var firstArgumentInClosure: Bool?
someFunc { firstArgumentInClosure = $0 }
XCTAssertTrue(firstArgumentInClosure?)
Run Code Online (Sandbox Code Playgroud)
我试图断言闭包是用第一个参数调用的 true。
这不会编译并显示错误消息:
'?必须后跟调用、成员查找或下标
我可以解决此问题的一种方法是将断言更改为:
XCTAssertEqualTrue(firstArgumentInClosure ?? false)
Run Code Online (Sandbox Code Playgroud)
有没有更简洁的方法来做到这一点?看到这个有点奇怪??
false。我也不想隐式地解开可选的 with!因为我不希望它使测试崩溃,我只想让测试失败 if firstArgumentInClosureis nilor false。我也想避免任何if lets 以保持简洁。
同样,当我尝试使用XCTAssertFalsea Bool?is时,我将如何编写这样的断言false?
如何在 Java 8 中完成?
if (var1 != null && var2!=null) {
callMethod(var1,var2);
}
Run Code Online (Sandbox Code Playgroud)
我在想这样的事情,但有两个变量:
Optional.ofNullable(var1).ifPresent(var1 -> callMethod(var1));
Run Code Online (Sandbox Code Playgroud) 是否可以记录返回值不是nullJava 的Optional?
大多数工具和框架只关心参数,但我想用返回值不是的类型来表达null(而不是在 JavaDoc 中这样做)。
UPDATE Looks line 你可以同意团队使用Optional作为返回值,如果你想表达可能null和直接的对象,当它绝对不为空时:
public Optional<Job> getJob() { ... }
public Job extractJob(NonNullJobHolder<Job> holder) { ... }
Run Code Online (Sandbox Code Playgroud) 我有一个这样的 JPA 查询。
PersonRepository.java
public Optional<List<PersonEntity>> findByStatus(int status);
Run Code Online (Sandbox Code Playgroud)
人员服务.java
System.out.println(“Optional value is ”+findByStatus(1));
Run Code Online (Sandbox Code Playgroud)
输出是 Optional value is Optional.empty
现在我改变了我的查询 PersonRepository.java
public List<PersonEntity> findByStatus(int status);
Run Code Online (Sandbox Code Playgroud)
人员服务.java
Optional<List<PersonEntity>> optional = Optional.of(findByStatus(1));
System.out.println("Optional value is "+optional);
Run Code Online (Sandbox Code Playgroud)
输出是 Optional value is Optional[[]]
在我的数据库中,状态 1 没有值。我想要我的输出作为Optional[[]]第一个查询。我如何实现这一目标?
现在,我想实现这一点,因为每当optional.get()抛出 a 时NoSuchElementException,我将使用我的异常控制器处理它并将其作为 404 公开给 REST 层。但是,当List<Object>获取a 时,响应只是一个空 List,但optional.get()仍然抛出 a NoSuchElementException。我想避免这种情况。简而言之,NoSuchElementException如果没有找到值,那么从 Repository 中可选的 fetch 就会抛出,这是完美的。但是对于空实体列表的可选提取,应该返回为空而不是抛出,NoSuchElementException因为它不是 404 错误。这只是意味着该列表目前为空。
我在操场上运行一些代码。
var optional: Int?
guard let unwrapped = optional else {
print("optional is nil")
return
}
print("Optional is not nil”)
Run Code Online (Sandbox Code Playgroud)
使用此当前代码,我收到一条错误消息,指出“在 func 之外返回无效”。如果我更换return用break,我得到一个错误,指出break只允许一个循环或交换机的内部。如果我在 print 语句之后没有任何其他语句,我会收到一条错误消息,指出“保护主体不能落空,请考虑使用 return 或 throw 退出范围”。
我有这段代码,我想在没有找到资源的情况下抛出异常
Menu menu = menuService.findById(addMenuAmount.getMenuId())
.orElseThrow(com.tdk.web.exception.ResourceNotFoundException(“menu " +
addMenuAmount.getMenuId() + " not found "));
Run Code Online (Sandbox Code Playgroud)
但我得到了一个编译错误:
com.tdk.web.exception cannot be resolved to a type
Run Code Online (Sandbox Code Playgroud) 如果我有一个可选字符串数组,并且我想以 nils 开头的升序对其进行排序,我可以在一行中轻松完成:
["b", nil, "a"].sorted{ $0 ?? "" < $1 ?? "" } // [nil, "a", "b"]
Run Code Online (Sandbox Code Playgroud)
但是似乎没有任何类似的简单解决方案可以将 nil 排序到数组的末尾。使用大多数其他简单数据类型可以轻松完成,例如:
[2, nil, 1].sorted{ $0 ?? Int.max < $1 ?? Int.max } // [1, 2, nil]
Run Code Online (Sandbox Code Playgroud)
对于双打,您可以使用greatestFiniteMagnitude,对于日期,您可以使用distantFuture。是否有任何类型的字符串等价物,或任何其他简洁的方法来做到这一点,这样我就可以避免编写一堆凌乱的条件?
我正在学习 Spring Boot,当服务没有在数据库中找到项目时,我试图抛出异常,因此,我尝试使用 optional 但当我测试它时,除了异常之外,我只得到一个空响应
@GetMapping(value = "/compras", produces = "application/json")
public Optional<Compras> retrieveAllCompras(@RequestParam String id) {
return Optional.of(compraRepository.findById(id)).orElseThrow(RuntimeException::new);
Run Code Online (Sandbox Code Playgroud)
当在数据库中找不到该项目时,我预计会出现异常
我正在尝试在 F# 中构建一个类型,当我获得该类型的对象时,我可以确定它处于有效状态。
该类型被调用JobId,它只包含一个Guid.
业务规则是:它必须是一个 Guid - 但不能是空的 Guid。
我已经在 C# 中实现了该类型,但现在我想将它移植到 F# 类库中。
那是 C# 类型:
public sealed class JobId
{
public string Value { get; }
private JobId(string value)
=> Value = value;
public static JobId Create()
=> new JobId(Guid.NewGuid().ToString("N"));
public static Option<JobId> Create(Guid id)
=> id == Guid.Empty
? None
: Some(new JobId(id.ToString("N"));
public static Option<JobId> Create(string id)
{
try
{
var guid = new Guid(id);
return Create(guid);
}
catch (FormatException)
{
return …Run Code Online (Sandbox Code Playgroud) Optional<Long>totalLanding= ....(get it from somewhere);
Optional<Long>totalSharing = ...(get it from somewhere);
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情不是在语法上而是在逻辑上
Optional<Long>total = totalLanding+totalSharing;
Run Code Online (Sandbox Code Playgroud)
这样,如果两者都为空,则总计应该为空,如果其中一个具有该值,则总计应该具有该值,如果它们都具有该值,那么它们应该被添加并存储在总计中
optional ×10
java ×5
java-8 ×3
swift ×3
spring-boot ×2
arrays ×1
c# ×1
f# ×1
non-nullable ×1
options ×1
response ×1
sorting ×1
unit-testing ×1