[英]Java 16 pattern matching problem with Switch in Intellij IDEA 2020.3.3 (Expression expected)
正如我們所知,Java 16 帶來了諸如記錄、密封接口和類以及模式匹配等新特性。
今天我想在我的培訓項目中使用它們。 但是,我遇到了一個問題,也許我不明白一些事情。
因此,在代表我的 Intellij Idea 項目的給定代碼中,情況看起來像: 我有包:客戶端和訂單 客戶端 class 是一個密封接口,具有三個實現:Regular、Vip 和 SuperVip:
package com.example.records.domain.client;
public sealed interface Client permits Client.Regular, Client.SuperVip, Client.Vip {
Limit currentLimit();
record Regular() implements Client {
@Override
public Client.Limit currentLimit() {
return Client.Limit.of((short) 10);
}
}
record Vip() implements Client {
@Override
public Client.Limit currentLimit() {
return Client.Limit.of((short) 20);
}
}
record SuperVip() implements Client {
@Override
public Client.Limit currentLimit() {
return Client.Limit.of((short) 100);
}
}
record Limit(short value) {
public Limit {
if (value < 0) throw new IllegalArgumentException("Values below 0 not allowed!");
}
static Limit of(short value) {
return new Limit(value);
}
}
}
命令 class 是從 DDD 知道的簡單聚合: 但是 switch 語句存在問題:
package com.example.records.domain.order;
import com.example.records.domain.OrderId;
import com.example.records.domain.client.Client;
import com.example.records.domain.client.Client.*;
import com.example.records.shared.Money;
import com.example.records.shared.Result;
import java.util.LinkedList;
import java.util.List;
public class Order() {
private final OrderId orderId;
private final Client client;
private final LinesItems linesItems = LinesItems.empty();
Order(OrderId orderId, Client client) {
this.orderId = orderId;
this.client = client;
}
public Result add(LineItem lineItem) {
return switch (client) {
case Regular r -> addAnItemIfTheLimitIsNotExceeded(r.);
case Vip v -> addAnItemIfTheLimitIsNotExceeded(v.);
case SuperVip sv -> addAnItemIfTheLimitIsNotExceeded(sv.);
};
}
private Result addAnItemIfTheLimitIsNotExceeded(Limit limit) {
// TODO need impl
return Result.OK;
}
private static class LinesItems {
private final List<LineItem> lines = new LinkedList<>();
private LinesItems() {}
void add(LineItem lineItem) {
this.lines.add(lineItem);
}
Money cost() {
return lines.stream().map(LineItem::cost).reduce(Money.ZERO, Money::add);
}
static LinesItems empty() {
return new LinesItems();
}
}
}
record LineItem(Money cost) {}
其他類:
package com.example.records.domain;
public record OrderId(String value) {
public OrderId {
validate();
}
private void validate() {
if (value == null)
throw new IllegalArgumentException("Null value not allowed!");
if (value.isBlank())
throw new IllegalArgumentException("Blank value not allowed!");
}
}
package com.example.records.shared;
import java.math.BigDecimal;
public record Money(BigDecimal value) {
public static final Money ZERO = new Money(new BigDecimal("0.00"));
public Money add(Money money) {
return new Money(this.value.add(money.value));
}
}
package com.example.records.shared;
public sealed interface Result {
Success OK = new Success();
record Success() implements Result{}
record Failure(String message) implements Result {}
}
我得到“(預期表達)”我在哪里 go 錯了? (實驗功能開啟,我已經安裝了Java 16 open jdk)
instanceof
中的類型模式是 Java 16 中的最終(非預覽)功能。但是, switch
中的類型模式尚未在 Java 16 中; 他們預計很快就會到達。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.