supporting built-in collections consistently in fd.dx()
- Dominant language
- Python
- Stars
- 150
- Forks
- 79
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 10
Description
### Description
Hello, this is a follow-up issue of #405, it is not a bug but rather an opinionated proposal to simplify a part of the API.
Currently, `fd.dx()` handles `None` and empty tuples, but rejects other empty collections like list and set. This is not intuitive and confusing. It can lead to edge cases. Taking these types and their conversions into account add complications to the user's codebase.
```python
fd.dx() ✅ # Equivalent to Measure('cell', subdomain_id='everywhere')
fd.dx(None) ✅ # Same as above
fd.dx([]) ❌ # Raises an error
fd.dx(()) ❓ # Accepted, but becomes Measure('cell', subdomain_id=()), not 'everywhere'
fd.dx(set()) ❌ # Raises an error
```
### Proposed solution
The simplest and the most intuitive way would be to act same as Python and its ecosystem does. This makes the API flexible and predictable, avoiding surprises for users.
#### Python
1. Supports built-in collections consistently.
```python
>>> sum([])
0
>>> sum(())
0
>>> sum(set())
0
```
2. It fails whem there is nothing to sum over.
```python
>>> sum()
Traceback (most recent call last):
File "", line 1, in
TypeError: sum() takes at least 1 positional argument (0 given)
```
#### Numpy
1. Supports built-in collections consistently.
```python
>>> np.sum([])
np.float64(0.0)
>>> np.sum(())
np.float64(0.0)
>>> np.sum(set())
set()
```
2. It fails whem there is nothing to sum over.
```python
>>> np.sum()
Traceback (most recent call last):
File "", line 1, in
TypeError: sum() missing 1 required positional argument: 'a'
```
I think this API change could make Firedrake/UFL more intuitive and user-friendly. It might also simplify the underlying implementation by reducing special cases, such as the differences between empty tuples and no-argument scenarios:
https://github.com/FEniCS/ufl/blob/760acc45a1803d9243567b390994140d8622f477/ufl/measure.py#L228
I’d appreciate hearing your thoughts. Does this approach seem reasonable to you?
Thank you in advance for your feedback
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.