The following example
using Unitful
using Parameters
@with_kw struct Grid
nx::Int
Lx::Float64
Δx::Float64 = Lx / (nx-1)
x::StepRangeLen = range(0, length=nx, stop= Lx)
end
# convert Length unit
function Grid(nx::Int, Lx::Unitful.Length)
Grid(;nx=nx, Lx=ustrip(u"m", Lx))
end
@show Grid(101, 1000u"m")
@show Grid(101, 1000u"1000")
yields
Grid(101, 1000 * u"m") = Grid
nx: Int64 101
Lx: Float64 1000.0
Δx: Float64 10.0
x: StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}
ERROR: MethodError: no method matching Grid(::Int64, ::Int64)
which seems sensible to me. Your first example seems to break units, because you manage to construct a Grid from a non-length unit?