Giter VIP home page Giter VIP logo

Comments (5)

rmsrosa avatar rmsrosa commented on June 24, 2024

I was in fact working on this yesterday, but i didn't quite got what i wanted.

I was trying to include @eqrelation in @equivalence, and have it figure out whether or not the equivalence exists or not. If it does, then proceeds as in current @eqrelation , otherwise, proceeds as in current @equivalence. But it was having problems with the docstring. i will try a little more, but if i do not succeed, i would say i prefer option 1.

from unitfulequivalences.jl.

sostock avatar sostock commented on June 24, 2024

I was trying to include @eqrelation in @equivalence, and have it figure out whether or not the equivalence exists or not.

Figuring out whether the type exists or not seems not easily possible. @isdefined only works if the type is defined/imported in the current module, it won’t work with a module qualifier (e.g., UnitfulEquivalences.MassEnergy). I am also not a big fan of doing different things depending on whether a variable is defined or not.

But it was having problems with the docstring.

Do you mean attaching the docstring to the struct in the macro? That’s what Base.@__doc__ is for.

i will try a little more, but if i do not succeed, i would say i prefer option 1.

Okay, then I will implement option 1.

from unitfulequivalences.jl.

rmsrosa avatar rmsrosa commented on June 24, 2024

Well, i think i managed to do that with

macro equivalence(name, relation)
    quote
        try
            isa($name, DataType)
            $(_eqrelation(name, relation))
        catch
            Base.@__doc__ struct $(esc(name)) <: Equivalence end
            $(_eqrelation(name, relation))
        end
    end
end

and then we don't need to bother whether a relation has been defined or not and don't need to bother whether to use @equivalence or @eqrelation (with which i was quite confused at first). We can just write:

"""
    Spectral

Equivalence that relates the energy of a photon to its frequency and wavelength according to
the relation ``E = hf = hc/λ``, where
* ``E`` is the photon energy,
* ``f`` is the frequency,
* ``λ`` is the wavelength,
* ``h`` is the Planck constant and
* ``c`` is the speed of light in vacuum.

!!! Note
    The `Spectral` equivalence does not include the wavenumber. This is to avoid mistakes,
    since there are two competing definitions of wavenumber (``1/λ`` and ``2π/λ``).
"""
@equivalence Spectral Energy/Frequency  = h
@equivalence Spectral Energy*Length     = h*c0
@equivalence Spectral Length*Frequency  = c0

This worked well with the test.

Do you think this is "stable", i mean, do you think it will suffer from the same problem that isdefined has, that you mentioned?

I like this way, so the user just need to bother with one macro, but if you think option 1 is better (or safer), it is fine with me.

from unitfulequivalences.jl.

sostock avatar sostock commented on June 24, 2024
macro equivalence(name, relation)
    quote
        try
            isa($name, DataType)
            $(_eqrelation(name, relation))
        catch
            Base.@__doc__ struct $(esc(name)) <: Equivalence end
            $(_eqrelation(name, relation))
        end
    end
end

This version does work, but I would still prefer one of the two options above. Some comments on your solution:

  • isa($name, DataType) is unnecessary, the subsequent method definition (in _eqrelation) will also throw an error if $name is undefined.
  • The catch block is also executed if _eqrelation in the try block fails due to a wrong relation. In that case, the code will throw an error that the $name type is redefined instead of the error that relation is wrong, which is the actual problem. I think this is not optimal and I would generally not use try … catch for something like this.
  • $name needs to be escaped in isa($name, DataType).

Since you are ok with option 1, I would merge #3.

from unitfulequivalences.jl.

rmsrosa avatar rmsrosa commented on June 24, 2024

Ok, I understand the problems with my solution. Let's go with option 1, then!

from unitfulequivalences.jl.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.