Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using Base.@irrational #18

Closed
hyrodium opened this issue Jan 25, 2023 · 7 comments
Closed

Stop using Base.@irrational #18

hyrodium opened this issue Jan 25, 2023 · 7 comments

Comments

@hyrodium
Copy link
Contributor

This macro should not be used outside of Base. See JuliaLang/julia#47103 and JuliaLang/julia#46531.

@devmotion
Copy link
Member

I'm well aware of this issue (see, e.g., #12 (comment) and TuringLang/MCMCDiagnosticTools.jl#63 (comment)). The main/only point of this package is that already existing definitions of irrationals, floating around in the ecosystem, are defined in a common package so it's a lightweight dependency and thereby makes it less likely that two packages define the same constants. Most constants have been defined in StatsFuns for a long time before this package was created and have been used in particular in different stats packages; IIRC then at some point someone wanted to use them in SpecialFunctions which does not want to add a dependency on StatsFuns (and StatsFuns even depends on SpecialFunctions). Of course, it's only the second best solution but I think it's definitely better than every package trying to redefine irrational constants.

@hyrodium
Copy link
Contributor Author

hyrodium commented Jan 25, 2023

I see. Sorry for bothering you.

There's one more thing I'd like to ask you about. Is there any problems if we define the constants with IrrationalConstant{sym} <: AbstractIrrational and @irrationalconstant (a macro to define IrrationalConstant just like Base.@irrational)? I thought this solution can resolve the type piracy and does not produce any other problems.

@devmotion
Copy link
Member

Yes, I assume the way to fix this are custom types that are not owned by base, as suggested in JuliaLang/julia#46531 (comment). I'm not sure how many methods in base are defined for Irrational explicitly - the annoying thing with custom types would be that we would miss functionality that does not support AbstractIrrational.

@devmotion
Copy link
Member

But at least a quick search does not reveal anything: https://github.com/JuliaLang/julia/search?p=1&q=Irrational

@hyrodium
Copy link
Contributor Author

I have not checked all of the code, but it seems we need to define these methods at least.

show(io::IO, x::IrrationalConstant{sym}) where {sym} = print(io, sym)

function show(io::IO, ::MIME"text/plain", x::IrrationalConstant{sym}) where {sym}
    if get(io, :compact, false)::Bool
        print(io, sym)
    else
        print(io, sym, " = ", string(float(x))[1:min(end,15)], "...")
    end
end

==(::IrrationalConstant{s}, ::IrrationalConstant{s}) where {s} = true
<(::IrrationalConstant{s}, ::IrrationalConstant{s}) where {s} = false
<=(::IrrationalConstant{s}, ::IrrationalConstant{s}) where {s} = true
hash(x::IrrationalConstant, h::UInt) = 3*objectid(x) - h  # I'm not much familiar with `hash`. just copied from Base.

@devmotion
Copy link
Member

I think that we would want to avoid the design of Irrational since that leads to exactly the same problems if some tries to extend our irrational type in a package. Basing irrationals on a type parameter seems flawed. Instead we would want separate singleton types for each irrational constants, as proposed in the issue linked above.

If you don't beat me to it, I might have time to look into it later this evening.

@devmotion
Copy link
Member

Fixed by #19, will be released as IrrationalConstants 0.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants