Skip to content

Add Nullable fields#106

Open
FxMorin wants to merge 1 commit intoMojang:masterfrom
FxMorin:nullable-fields
Open

Add Nullable fields#106
FxMorin wants to merge 1 commit intoMojang:masterfrom
FxMorin:nullable-fields

Conversation

@FxMorin
Copy link

@FxMorin FxMorin commented Jan 29, 2026

This adds support for optional fields without needing to have your values wrapped in Optional.

Before:

private record Data(
    Optional<String> string,
    Optional<Integer> integer
) {
    public static final Codec<Data> CODEC = RecordCodecBuilder.create(i -> i.group(
            Codec.STRING.optionalFieldOf("string").forGetter(Data::string),
            Codec.INT.optionalFieldOf("integer").forGetter(Data::integer)
    ).apply(i, Data::new));
}

After:

private record Data(
    @Nullable String string,
    @Nullable Integer integer
) {
    public static final Codec<Data> CODEC = RecordCodecBuilder.create(i -> i.group(
            Codec.STRING.nullableFieldOf("string").forGetter(Data::string),
            Codec.INT.nullableFieldOf("integer").forGetter(Data::integer)
    ).apply(i, Data::new));
}

You can see the tests for more examples.

This was done since Optional is intended to be used as a method return type. Not as a field, nullability exists for that purpose.
For people following the standards and using annotations such as JSpecify with proper nullability tools, using Optional is just pointless wrapping that DFU is forcing you to use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant