Giter Club home page Giter Club logo

wheretraits.jl's Introduction

WhereTraits.jl

Stable Dev Build Status Coverage

Welcome to WhereTraits.jl. This package exports one powerful macro @traits with which you can extend Julia's where syntax in order to support traits definitions.

In addition, WhereTraits comes with a standardized way how to resolve ambiguities among traits, by defining an order among the traits with @traits_order.

Installation & Import

Install by running

using Pkg
pkg"add WhereTraits"

Then use this package by loading

using WhereTraits

which brings @traits into your namespace, and in addition also @traits_order for resolving ambiguities.

Usage @traits

@traits supports the following three extensions to Julia's where-syntax:

  • dispatch on functions returning Bool
@traits f(a) where {isodd(a)} = (a+1)/2
@traits f(a) where {!isodd(a)} = a/2
f(4) # 2.0
f(5) # 3.0
  • dispatch on functions returning anything
@traits g(a) where {Base.IteratorSize(a)::Base.HasShape} = 43
@traits g(a) = 1
g([1,2,3]) # 43
g(Iterators.repeated(1)) # 1
  • dispatch on upperbounds on functions returning Types
@traits h(a) where {eltype(a) <: Number} = true
@traits h(a) = false
h([1.0]) # true
h([""]) # false

And all this works with arbitrary many where expressions and creates optimal code where possible via standard Julia compiler.

Usage @traits_order - Resolving Ambiguities

WhereTraits comes with special support for resolving ambiguities among traits dispatch.

Under the hood @traits uses normal function dispatch to achieve the speed and flexibility, however, julia function dispatch can lead to ambiguities. With traits these can easily happen if someone defines @traits for the same standard dispatch but using different traits. Let's take a look how this looks like

using WhereTraits
# let's say someone defined this version
@traits conflict(a) where {eltype(a) <: Number} = "eltype wins"
# and another this one
@traits conflict(a) where {Base.IteratorSize(a) :: Base.HasShape} = "IteratorSize wins"

You can still use these traits definition as long as there is no ambiguity.

julia> conflict(Iterators.countfrom(42))
"eltype wins"

julia> conflict(["hello", "world"])
"IteratorSize wins"

If you use something ambiguous, e.g. a Vector of Numbers, you get a proper ambiguity error, stating what you can do in order to fix it.

julia> conflict([1,2,3,4])
ERROR: Disambiguity found. Please specify an ordering between traits, like the following.

    @traits_order (Main).conflict(a1::T1) where T1 begin
        eltype(a1)
        Base.IteratorSize(a1)
    end

Stacktrace:
[...]

What is needed in order to resolve the ambiguity is an order between the traits. This can be defined with @traits_order, which takes the respective function signature followed by a begin-end block of ordered traits (most dominant one should be at the top).

Hence just executing the example @traits_order will make eltype be the winning trait.

@traits_order (Main).conflict(a1::T1) where T1 begin
    eltype(a1)
    Base.IteratorSize(a1)
end

Let's take a look that everything is resolved

julia> conflict([1,2,3,4])
"eltype wins"

Alternatively to the @traits_order you can always define your own custom resolution

@traits function conflict(a) where {eltype(a) <: Number, Base.IteratorSize(a) :: Base.HasShape}
    "custom implementation"
end

which immediately will resolve correctly

julia> conflict([1,2,3,4])
"custom implementation"

For more details, take a look at the documentation.

Limitations

Optimal Code

Warning: While the dispatch works for dynamic functions, it will only be able to create optimal code if your traits function supports proper type-inference. E.g. you can use Base.isempty, however type-inference cannot see whether it will return true or false by static inspection. Hence it will use slower dynamic code.

Keyword arguments

Keyword arguments are at the moment not support for WhereTraits dispatch. They are just passed through.

Symbol Level

The extended where syntax is currently implemented on symbol level, which is why traits functions like Base.IteratorSize and the non-qualified IteratorSize (assuming you imported import Base: IteratorSize) are treated as two different functions, despite being the same. So for now try to only use the one style or the other.

Top Level Only

Currently only top-level functions are supported, as the syntax stores and needs information about previous function definitions, which it stores globally. If macros would get informed about whether they are defined within another function, WhereTraits could also support innerfunctions.

Test package

The @traits macro currently does not work well within the Test.@testset macro. Usually you won't encounter this, as standard dispatch is probably enough for your tests.

Nevertheless there is a workaround. WhereTraits.jl exports a @traits_test macro variant which works better, but still might have cases where it fails. This needs to be investigated further, and maybe needs a change on Test.@testset.

Other traits packages

There are many different attempts to add traits to Julia. Everyone puts a different emphasis on different aspects of traits interfaces.

  • SimpleTraits.jl
  • BinaryTraits.jl
  • CanonicalTraits.jl

wheretraits.jl's People

Contributors

fingolfin avatar github-actions[bot] avatar masonprotter avatar schlichtanders avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

wheretraits.jl's Issues

Where does the compilation time come from?

What the title says, mostly; I was interested in using this while writing a package of mine, but the compilation time was just a touch over what I'd be willing to add to a package. A useful tool here.

Why @traits plural?

It seems like I am dispatching on a trait (singular) not traits (plural). Is @trait a better name?

[DOC] Other packages section updates

It should be worth in your "other traits packages" section to also reference the work of Taine Zhao (aka thautwarm ) in MLStyle.jl , more precisely :

IMO among the 3 others traits pkgs , 2.5 are quite data oriented , while yours is more functional oriented thru dispatch and matches very closely this work

Runtime overhead

There appears to be some runtime overhead from using this package, it's not especially onerous, but it's definitely present:

julia> using WhereTraits, BenchmarkTools

julia> @traits f(x) = x

julia> @btime f(1);
  22.215 ns (0 allocations: 0 bytes)

compare against

julia> g(x) = x;

julia> @btime g(1);
  1.356 ns (0 allocations: 0 bytes)

Doing some inspection, I think this is due to the conflict resolution system which should be resolved at compile time instead of runtime. Is there anything we can do about this?

TagBot trigger issue

This issue is used to trigger TagBot; feel free to unsubscribe.

If you haven't already, you should update your TagBot.yml to include issue comment triggers.
Please see this post on Discourse for instructions and more details.

If you'd like for me to do this for you, comment TagBot fix on this issue.
I'll open a PR within a few hours, please be patient!

add nightly to CI

as nightly was failing for unclear reasons it was taken out from the CI

somewhen nightly should again be put to WhereTraits.jl CI

Traits with keyword arguments does not work

First of all. Great package! The dispatch in Julia is truly great, and this extension makes it even better. I was playing around with the package and I realize that it didn't seem to work with keyword arguments, e.g:

@traits bestprice(b; units) where {units == "cm"} = b

Returns the error

LoadError: MethodError: Cannot `convert` an object of type Symbol to an object of type Expr
Closest candidates are:
  convert(::Type{Expr}, ::ExprParsers.ExprParsed) at ~/.julia/packages/ExprParsers/snVTb/src/ExprParsers.jl:135
  convert(::Type{T}, ::T) where T at /opt/julia-1.7.1/share/julia/base/essentials.jl:218
  Expr(::Any...) at /opt/julia-1.7.1/share/julia/base/boot.jl:263
in expression starting at In[107]:3

The very same code works if I do bestprice(b, units). Is this behavior intended? Is there a way around it?

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.