Stream: helpdesk (published)

Topic: ✔ Dispatching on ScientificTypes


view this post on Zulip Davi Sales Barreira (Jan 24 2022 at 19:39):

Does anybody knows if it's possible to somehow dispatch with ScientificTypes.jl? I mean, I wanted to do something like function dosomething(x::Continuous), which does not work.

view this post on Zulip Júlio Hoffimann (Jan 24 2022 at 19:42):

It is possible and we use it in downstream packages. What exactly are you trying to achieve with dispatch?

view this post on Zulip Júlio Hoffimann (Jan 24 2022 at 19:42):

You can find examples of dispatch in TableDistances.jl: https://github.com/JuliaML/TableDistances.jl/blob/deaf10257ce1de34137ad7829391b8bf22817746/src/distances.jl#L27-L32

view this post on Zulip Expanding Man (Jan 24 2022 at 19:43):

You can do it with separate arguments, e.g.

function f(::Type{Continuous}, x::Real)
end

function f(::Type{Count}, x::Real)
end


f(elscitype(v), v[1])

view this post on Zulip Davi Sales Barreira (Jan 24 2022 at 19:45):

Júlio Hoffimann said:

It is possible and we use it in downstream packages. What exactly are you trying to achieve with dispatch?

I'm coding a plotting package, and I was using it to infer the datatype as Ordinal, Continuous or Nominal (per VegaLite standards.

view this post on Zulip Júlio Hoffimann (Jan 24 2022 at 19:46):

Would love to see plot recipes all taking scitypes into account. That has been a dream of mine for a while.

view this post on Zulip Mason Protter (Jan 24 2022 at 19:46):

This is known as trait based dispatch.

using ScientificTypes

f(x) = f(scitype(x), x)

f(::Type{Continuous}, x) = x + 1
f(::Type{Count}, x) = x - 1
julia> f(1.0)
2.0

julia> f(1)
0

view this post on Zulip Expanding Man (Jan 24 2022 at 19:46):

The whole idea of scientific types is that the implementation type does not fully specify the domain of the argument, so you should not expect to be able to dispatch on scientific types based on on the argument alone. It might be nice to combine or incorporate it in other packages that try to specify the domain of arguments, such as Unitful.jl.

view this post on Zulip Davi Sales Barreira (Jan 24 2022 at 19:47):

Mason Protter said:

This is known as trait based dispatch.

using ScientificTypes

f(x) = f(scitype(x), x)

f(::Type{Continuous}, x) = x + 1
f(::Type{Count}, x) = x - 1

julia> f(1.0)
2.0

julia> f(1)
0

This is very nice... When I thought I new "everything" on Multiple Dispatch...

view this post on Zulip Mason Protter (Jan 24 2022 at 19:47):

This is yet another case where better trait support would be nice to have

view this post on Zulip Davi Sales Barreira (Jan 24 2022 at 19:48):

Expanding Man said:

The whole idea of scientific types is that the implementation type does not fully specify the domain of the argument, so you should not expect to be able to dispatch on scientific types based on on the argument alone. It might be nice to combine or incorporate it in other packages that try to specify the domain of arguments, such as Unitful.jl.

Since it's for plotting, I don't think I'd actually need the units... At least not if I consider the GG standard from VegaLite.

view this post on Zulip Expanding Man (Jan 24 2022 at 19:49):

I'd think plotting would be a really good place to integrate with Unitful.jl, I think some of that has been done already? You ought to be able to infer ScientificTypes from units

view this post on Zulip Mason Protter (Jan 24 2022 at 19:49):

The downside of the trick I showed above is that you can't add methods like that to functions you don't own without type piracy. E.g. I couldn't add a method like that to Base.sin without potentially causing a lot of breakage.

view this post on Zulip Mason Protter (Jan 24 2022 at 19:51):

Expanding Man said:

I'd think plotting would be a really good place to integrate with Unitful.jl, I think some of that has been done already? You ought to be able to infer ScientificTypes from units

Needs a bit of glue, but could be done.

julia> using Unitful

julia> scitype(1u"eV")
Unknown

julia> ScientificTypes.scitype(x::Quantity) = scitype(ustrip(x))

julia> scitype(1u"eV")
Count

view this post on Zulip Mason Protter (Jan 24 2022 at 19:52):

I think it'd definitely be a good idea if ScientificTypes were unit aware.

view this post on Zulip Expanding Man (Jan 24 2022 at 19:52):

Sorry, I didn't mean to imply that I thought it was already implemented, just that it could be unambiguously in principle.

view this post on Zulip Davi Sales Barreira (Jan 24 2022 at 19:53):

Expanding Man said:

I'd think plotting would be a really good place to integrate with Unitful.jl, I think some of that has been done already? You ought to be able to infer ScientificTypes from units

Really? I'd love to see some examples of plotting packages using Unitful.jl. I'm mean, for sure incorporating Unitful would be nice, but for a plot I dont "need" to know the unit, only if it's continuous, ordered and so on. But of course, if I knew the unit, I could reverse that. I just don't want to burden the user too much having to specify everything

view this post on Zulip Expanding Man (Jan 24 2022 at 19:53):

I thought Plots.jl had some kind of integration? I could be wrong.

view this post on Zulip Mason Protter (Jan 24 2022 at 19:53):

https://github.com/jw3126/UnitfulRecipes.jl

view this post on Zulip Davi Sales Barreira (Jan 24 2022 at 19:55):

Mason Protter said:

https://github.com/jw3126/UnitfulRecipes.jl

Very nice. But this seems more like an extra. I mean, ScientificType would be more of a tool to inspect the datatypes. In VegaLite, one has to provide the datatype explicitly as :Quantitative, Ordinal or Nominal. This is what I'm trying to "replicate" with ScietificType.

view this post on Zulip Júlio Hoffimann (Jan 24 2022 at 19:57):

I feel that ScientificTypes.jl and Unitful.jl are somewhat orthogonal, but it is indeed a shame that we don't have traits to accomodate both concepts simultaneously with ease.

view this post on Zulip Júlio Hoffimann (Jan 24 2022 at 19:58):

I feel that the former concept is extremely underused in Julia ecosystem. Hopefully it will get fixed in the future.

view this post on Zulip Notification Bot (Jan 24 2022 at 20:01):

Davi Sales Barreira has marked this topic as resolved.

view this post on Zulip Ari Katz (Jan 24 2022 at 21:04):

Júlio Hoffimann said:

I feel that the former concept is extremely underused in Julia ecosystem. Hopefully it will get fixed in the future.

I think it needs better language trait support for that to happen

view this post on Zulip Brian Chen (Jan 24 2022 at 21:33):

Off the main topic, but @Davi Sales Barreira would you mind posting a link to this scitypes + vega integration if/when it's operational? As a somewhat frequent vega lite user, the idea sounds wonderful!

view this post on Zulip Davi Sales Barreira (Jan 24 2022 at 21:37):

@Brian Chen , I'm actually not working on VegaLite. I'm working on a separate package, but I was a heavy vegalite user, and I quite like the design for generating the visualizations

view this post on Zulip Davi Sales Barreira (Jan 24 2022 at 21:38):

So I'm taking it as inspiration. Specially the grammar. But it's not at all related :X

view this post on Zulip Davi Sales Barreira (Jan 24 2022 at 21:38):

But once I have a beta for my package, I for sure intend to share it with the community.


Last updated: Oct 02 2023 at 04:34 UTC