r-quantities / substances

Substance-aware unit conversions
MIT License
0 stars 2 forks source link

Object concept #5

Open billdenney opened 2 months ago

billdenney commented 2 months ago

I thought that it would be useful to have a discussion of how the substances object could be created to make best use of existing code in units and also the need for substances.

For that, I looked at what units uses for both the units and mixed_units objects:

library(units)
#> udunits database from C:/Users/wdenn/AppData/Local/R/win-library/4.3/units/share/udunits/udunits2.xml
dput(units::set_units(1, "km"))
#> structure(1, units = structure(list(numerator = "km", denominator = character(0)), class = "symbolic_units"), class = "units")
units_options(allow_mixed = TRUE)
dput(c(units::set_units(1, "km"), units::set_units(1, "kg")))
#> structure(list(structure(1, units = structure(list(numerator = "km", 
#>     denominator = character(0)), class = "symbolic_units"), class = "units"), 
#>     structure(1, units = structure(list(numerator = "kg", denominator = character(0)), class = "symbolic_units"), class = "units")), class = c("mixed_units", 
#> "list"))

Created on 2024-05-08 with reprex v2.1.0

A light cleanup of the dput representation of the mixed_units class looks like this:

structure(list(
  structure(1, units = structure(list(numerator = "km", denominator = character(0)), class = "symbolic_units"), class = "units"), 
  structure(1, units = structure(list(numerator = "kg", denominator = character(0)), class = "symbolic_units"), class = "units")),
  class = c("mixed_units", "list")
)

For my use cases, the base assumption is that most or all vectors of substances would have different substances per vector element (e.g. c(set_substance(1, "mol", "hydrogen"), set_substance(1, "g", "helium")) would be my typical use case. So, I would lean toward the base case being separate substances per item rather than one substance used throughout.

Building on that, my preference would be that we then use a structure almost identical to the units structure for a substances object:

structure(1, units = structure(list(numerator = "mol", denominator = character(0)), class = "symbolic_units"), substance = "hydrogen", class = "substance")

The only two differences between the units and proposed substance structure are adding an attribute named substance with the substance name and the class being substance.

How does that sound?