I’m trying to replicate the following trick that I found in SpecialFunctions.jl/src/bessel.jl:
## Bessel functions
# besselj0, besselj1, bessely0, bessely1
for jy in ("j","y"), nu in (0,1)
jynu = Expr(:quote, Symbol(jy,nu))
jynuf = Expr(:quote, Symbol(jy,nu,"f"))
bjynu = Symbol("bessel",jy,nu)
if jy == "y"
@eval begin
$bjynu(x::Float64) = nan_dom_err(ccall(($jynu,libm), Float64, (Float64,), x), x)
$bjynu(x::Float32) = nan_dom_err(ccall(($jynuf,libm), Float32, (Float32,), x), x)
$bjynu(x::Float16) = Float16($bjynu(Float32(x)))
end
else
@eval begin
$bjynu(x::Float64) = ccall(($jynu,libm), Float64, (Float64,), x)
$bjynu(x::Float32) = ccall(($jynuf,libm), Float32, (Float32,), x)
$bjynu(x::Float16) = Float16($bjynu(Float32(x)))
end
end
@eval begin
$bjynu(x::Real) = $bjynu(float(x))
$bjynu(x::Complex) = $(Symbol("bessel",jy))($nu,x)
end
end
Here’s the code snippet that gives my MWE:
const Phases = @enum solid liquid vapor
struct CpData1{T}
Tmin::T
Tmax::T
a::T
b::T
c::T
d::T
phase::Phases
end
function minimumtemp(dat::CpData1, T) end
function maximumtemp(dat::CpData1, T) end
for aphase in [:solid, :liquid, :vapor]
for acond in [:maximum, :minimum]
@eval begin
fname1 = Symbol(acond, "temp_", aphase)
fname2 = Symbol(acond, "temp")
$fname1(dat::MultiCp, T) = $fname2(dat, dat.phaseranges[Phases::$aphase])
end
end
end
This gives the following errors:
UndefVarError: CpData1 not defined
and UndefVarError: fname1 not defined
.
I have a few questions about this. First, does the first error mean that I can’t use a struct defined in the same file because @eval
executes before Julia has executed the struct definition? Second, I don’t understand the fname
error at all because I think I’m using fname1
EXACTLY the same way that bjynu
is being used in SpecialFunctions.jl. What am I missing here?
Thanks!