Skip to content

Add TryGetValue to RowRef{T}, SubrowRef{T}#129

Merged
NotAdam merged 3 commits into
NotAdam:masterfrom
Haselnussbomber:trygetvalue
Jun 9, 2026
Merged

Add TryGetValue to RowRef{T}, SubrowRef{T}#129
NotAdam merged 3 commits into
NotAdam:masterfrom
Haselnussbomber:trygetvalue

Conversation

@Haselnussbomber

Copy link
Copy Markdown
Contributor

Just for supporting the TryGet pattern in the typed RowRef/SubrowRef.
The untyped RowRef already has it.

CC: @WorkingRobot

@WorkingRobot

Copy link
Copy Markdown
Contributor

Is there a reason why we should have this interface? This feels more like a potential footgun rather than a nice-to-have.

@Haselnussbomber

Haselnussbomber commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

I currently have this as extension method and am using this to return early.
Not sure what footgun you mean?

public bool CanTryOn(ItemHandle item)
{
    if (!TryGetItem(item, out var itemRow))
        return false;

    // not equippable, Waist or SoulCrystal => false
    if (itemRow.EquipSlotCategory.RowId is 0 or 6 or 17)
        return false;

    // any OffHand that's not a Shield => false
    if (itemRow.EquipSlotCategory.RowId is 2 && itemRow.FilterGroup != (int)ItemFilterGroup.Shield)
        return false;

    if (!_playerState.IsLoaded)
        return false;

    var race = _playerState.Race.RowId;
    if (race == 0)
        return false;

    if (!itemRow.EquipRestriction.TryGetRow(out var equipRaceCategoryRow)) // <--- here
        return false;

    if (!equipRaceCategoryRow.Races[(int)race - 1])
        return false;

    return _playerState.Sex switch
    {
        Sex.Female => equipRaceCategoryRow.Female,
        Sex.Male => equipRaceCategoryRow.Male,
        _ => false
    };
}

Though I coded it a bit differently:

extension<T>(RowRef<T> rowRef) where T : struct, IExcelRow<T>
{
    public bool TryGetRow(out T row)
    {
        if (rowRef.IsValid)
        {
            row = rowRef.Value;
            return true;
        }

        row = default;
        return false;
    }
}

If the user is disregarding the return value when using the TryGet pattern, that's entirely on them. It's the same as not checking HasValue.

I also don't mind to continue using my extension method (rewritten to the one PRd), if I have to.
Just thought this might be useful.

@NotAdam

NotAdam commented Jun 8, 2026

Copy link
Copy Markdown
Owner

looks good to me, not sure where the footgun is here

@WorkingRobot

Copy link
Copy Markdown
Contributor

I suppose so, its fine if someone needs that exact interface.

@NotAdam NotAdam merged commit 3e91b02 into NotAdam:master Jun 9, 2026
2 checks passed
@Haselnussbomber Haselnussbomber deleted the trygetvalue branch June 9, 2026 12:30
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.

3 participants