Skip to content

[Element][Parameter] Align the element lookups with First and FirstOrDefault - #122

Merged
Nice3point merged 3 commits into
developfrom
element/121-add-element-or-default
Oct 29, 2026
Merged

Nice3point merged 3 commits into
developfrom
element/121-add-element-or-default

Conversation

@Nice3point

@Nice3point Nice3point commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

Summary of the Pull Request

What is this about:

Element lookups follow LINQ First and FirstOrDefault: ToElement and AsElement return the element or throw, and their OrDefault counterparts return the element or null.

Description:

What changed

Member No element Element of another type
ToElement, ToElement<T> InvalidOperationException InvalidCastException
AsElement, AsElement<T> InvalidOperationException InvalidCastException
ToElementOrDefault, ToElementOrDefault<T> null null
AsElementOrDefault, AsElementOrDefault<T> null null
  • ToElementOrDefault, ToElementOrDefault<T>, AsElementOrDefault, and AsElementOrDefault<T> are new.
  • ToElement, ToElement<T>, AsElement, and AsElement<T> return non-nullable types.
  • The changelog lists the change under Breaking changes with the migration to the OrDefault counterpart.

Where to look

  • A missing element throws InvalidOperationException, the exception collector.First() throws for an empty collector. ArgumentException was the alternative, with the id as the invalid argument.
  • AsElement throws for a parameter that holds no element, such as an empty material parameter or a category id. Code that reads an optional parameter moves to AsElementOrDefault.
  • The change is breaking at run time only: the return types lose nullability, and existing null checks still compile.

How verified

  • Build: the library under Release.R19, R21, R23, R24, R25, and R27, zero errors.
  • Tests in Revit 2027, against every family sample: ElementIdExtensionsTests 92 passed, ParameterExtensionsTests 30 passed, none skipped.
  • Not covered: test runs on Revit 2026 and earlier.

Open questions

  • The breaking change sits in the unreleased 2027.0.6 section of the changelog. A minor version is the alternative.
  • ToElements<T> and ToOrderedElements<T> keep their behavior: a collection that mixes types throws InvalidCastException.

Quality Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings

@Nice3point Nice3point added feature request 📬 A request for new changes to improve functionality revit-api 📡 Revit API usage and the supported-version matrix labels Oct 29, 2026
@Nice3point Nice3point changed the title [Element][Parameter] Add typed element lookups that return null for an element of another type [Element][Parameter] Align the element lookups with First and FirstOrDefault Oct 29, 2026
@Nice3point Nice3point added the introduce breaking changes 💥 Changes a public contract label Oct 29, 2026
@Nice3point
Nice3point marked this pull request as ready for review October 29, 2026 14:48
Copilot AI balanced review requested due to automatic review settings October 29, 2026 14:48
@Nice3point
Nice3point merged commit 19c303e into develop Oct 29, 2026
1 check passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@Nice3point
Nice3point deleted the element/121-add-element-or-default branch October 29, 2026 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature request 📬 A request for new changes to improve functionality introduce breaking changes 💥 Changes a public contract revit-api 📡 Revit API usage and the supported-version matrix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add ToElementOrDefault<T>, which returns null for an element of another type

2 participants