Skip to content

Commit 220fa74

Browse files
authored
Add docstring to NoUnits and add it to the manual (#618)
1 parent ded8b0b commit 220fa74

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

docs/src/conversion.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,21 @@ results in behavior identical to calling [`uconvert`](@ref).
4242

4343
### Dimensionless quantities
4444

45-
For dimensionless quantities, `uconvert` can be used to strip the units without
46-
losing power-of-ten information:
45+
For dimensionless quantities, `uconvert` can be used with the [`NoUnits`](@ref) unit to
46+
strip the units without losing power-of-ten information:
4747

4848
```jldoctest
49-
julia> uconvert(Unitful.NoUnits, 1.0u"μm/m")
49+
julia> uconvert(NoUnits, 1.0u"μm/m")
5050
1.0e-6
5151
52-
julia> uconvert(Unitful.NoUnits, 1.0u"m")
52+
julia> uconvert(NoUnits, 1.0u"m")
5353
ERROR: DimensionError: and m are not dimensionally compatible.
5454
```
5555

56+
```@docs
57+
Unitful.NoUnits
58+
```
59+
5660
You can also directly convert to a subtype of `Real` or `Complex`:
5761

5862
```jldoctest
@@ -299,8 +303,8 @@ For multiplication and division, note that powers-of-ten prefixes are significan
299303
in unit cancellation. For instance, `mV/V` is not simplified, although `V/V` is.
300304
Also, `N*m/J` is not simplified: there is currently no logic to decide
301305
whether or not units on a dimensionless quantity seem "intentional" or not.
302-
It is however possible to cancel units manually, by passing the dimensionless
303-
quantity to the [`NoUnits`](@ref) constructor. This takes into account different SI-prefixes:
306+
It is however possible to cancel units manually, by converting the dimensionless
307+
quantity to the [`NoUnits`](@ref) unit. This takes into account different SI-prefixes:
304308
```jldoctest
305309
julia> using Unitful
306310

src/types.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ struct FreeUnits{N,D,A} <: Units{N,D,A} end
9595
FreeUnits{N,D}() where {N,D} = FreeUnits{N,D,nothing}()
9696
FreeUnits(::Units{N,D,A}) where {N,D,A} = FreeUnits{N,D,A}()
9797

98+
"""
99+
NoUnits
100+
An object that represents "no units", i.e., the units of a unitless number. The type of
101+
the object is `Unitful.FreeUnits{(), NoDims}`. It is displayed as an empty string.
102+
103+
Example:
104+
```jldoctest
105+
julia> unit(1.0) == NoUnits
106+
true
107+
```
108+
"""
98109
const NoUnits = FreeUnits{(), NoDims}()
99110
(y::FreeUnits)(x) = uconvert(y,x)
100111

src/utils.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,8 @@ true
112112

113113
"""
114114
unit(x::Number)
115-
Returns a `Unitful.FreeUnits{(), NoDims}` object to indicate that ordinary
116-
numbers have no units. This is a singleton, which we export as `NoUnits`.
117-
The unit is displayed as an empty string.
115+
Returns the [`NoUnits`](@ref) object to indicate that ordinary numbers have no units. The
116+
unit is displayed as an empty string.
118117
119118
Examples:
120119

0 commit comments

Comments
 (0)