FlexUnits & Structs

In creating a struct with units I don’t have a problem if each field of the struct has units. I am running into an error when not all the fields have units. Can someone show me where I am going wrong.

# Test unit structures
using FlexUnits, .UnitRegistry
import FlexUnits: QuantFieldVector, DimsMod

mutable struct Foo{T} <: QuantFieldVector{2,T}
    num::Integer
    leng::Quantity{T, D"m"}
end

num = 2
leng = 2.2u"m"
F = Foo(; num, leng)
ERROR: MethodError: no method matching Foo(; num::Int64, leng::Quantity{Float64, StaticDims{m}})
This method does not support all of the given keyword arguments (and may not support any).

Closest candidates are:
  Foo(::Integer, ::Quantity{T, StaticDims{m}}) where T got unsupported keyword arguments "num", "leng"
   @ Main REPL[5]:2
  (::Type{FA})(::Tuple) where FA<:StaticArraysCore.FieldArray got unsupported keyword arguments "num", "leng"
   @ StaticArrays C:\Users\jakez\.julia\packages\StaticArrays\SXG7G\src\FieldArray.jl:2
  (::Type{SA})(::Base.Generator) where SA<:StaticArraysCore.StaticArray got unsupported keyword arguments "num", "leng"
   @ StaticArrays C:\Users\jakez\.julia\packages\StaticArrays\SXG7G\src\SArray.jl:57
  ...

Stacktrace:
 [1] top-level scope
   @ REPL[8]:1

ERROR: MethodError: no method matching Foo(; num::Int64, leng::Quantity{Float64, StaticDims{m}})
This method does not support all of the given keyword arguments (and may not support any).

Closest candidates are:
  Foo(::Integer, ::Quantity{T, StaticDims{m}}) where T got unsupported keyword arguments "num", "leng"
   @ Main c:\data\st\Julia\WinderCurves\examples\addseries\test.jl:44
  (::Type{FA})(::Tuple) where FA<:StaticArraysCore.FieldArray got unsupported keyword arguments "num", "leng"
   @ StaticArrays C:\Users\jakez\.julia\packages\StaticArrays\SXG7G\src\FieldArray.jl:2
  (::Type{SA})(::Base.Generator) where SA<:StaticArraysCore.StaticArray got unsupported keyword arguments "num", "leng"
   @ StaticArrays C:\Users\jakez\.julia\packages\StaticArrays\SXG7G\src\SArray.jl:57
  ...

Stacktrace:
 [1] top-level scope
   @ c:\data\st\Julia\WinderCurves\examples\addseries\test.jl:50

julia> 

I think perhaps it’s just an errant semicolon. Try

F = Foo(num, leng)

That works. What confuses me is that for another struct the syntax above does work. I was trying to figure out why it works for one struct and not another. The syntax (; num, leng) does work for tuple construction. Perhaps I just stumbled across an idiosyncracity? Oh I think I figured out the difference, the case where it does work, the struct definition starts with @kwdef struct ..... I guess the @kwdef allows the syntax starting with the semi-colon.

(num, leng) constructs a Tuple. (; num, leng) constructs a NamedTuple. structs always have a Tuple constructor method, and using @kwdef adds an additional NamedTuple constructor method. (@kwdef also allows you to set default values for some fields.)

julia> (num, leng)
(2, 86.61417322834647 in)

julia> (; num, leng)
(num = 2, leng = 86.61417322834647 in)

julia> (num, leng) |> typeof
Tuple{Int64, Quantity{Float64, StaticDims{m}}}

julia> (; num, leng) |> typeof
@NamedTuple{num::Int64, leng::Quantity{Float64, StaticDims{m}}}

The semicolon separates arguments from keyword arguments (see here: Functions · The Julia Language · Keyword Arguments)

When you type the name of a struct with parentheses it works just like calling a function, and you can make new ‘constructors’ (see here: Constructors · The Julia Language)

Here are a few examples:

# Define a new struct
struct MyStruct
    a::Int
    b::Float64
end

# Make an instance of the struct using the default constructor
my_struct = MyStruct(1, 2.0)

# Define a new constructor using keywords
MyStruct(; a = -2, b = 3.0) = MyStruct(a, b)

# Call the new constructor 
my_struct_2 = MyStruct(; a = 3, b = 4.0)

# If you have variables whose names match the keys you can use shorthand for keyword arguments
a = 7
b = 5.0
my_struct_3 = MyStruct(; a, b) # shorthand for MyStruct(; a = a, b = b)

The @kwdef macro automates the process of making a keyword constructor for your struct.

Thanks I renamed the post to make it more searchable for others.