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

Propagation of literals and constants into functions #24011

Closed
ChrisRackauckas opened this issue Oct 5, 2017 · 5 comments · Fixed by #24362
Closed

Propagation of literals and constants into functions #24011

ChrisRackauckas opened this issue Oct 5, 2017 · 5 comments · Fixed by #24362

Comments

@ChrisRackauckas
Copy link
Member

It seems like in a lot of cases one wants to set a bunch of options. The options are usually set by constants from the user, and they can easily affect type-stability. A simple example is qrfact. In these cases the current standard is to have the API be qrfact(A,Val{true}). But it seems like if you have qrfact(A,true), Julia could in theory propagate the literal here. If the second argument is a constant in a function, then that could also happen at compilation time.

But you might not always want that because that could be too coarse of a compilation setup, so instead it might be nice to have an opt-in for that:

qrfact(A,pivot::Bool[Infer]=false)

or something like that to tell it to compile literals separately. Then APIs like qrfact(A,true) would allow optional arguments to change in a type-unstable way but keep type-stability when users are hardcoding the argument choice. When keyword dispatch is thing, it would be nice to allow this with keyword arguments as well.

@ChrisRackauckas
Copy link
Member Author

One related issue is that if you write a function that internally calls a function with lots of arguments and some infer like this, you'd want to pass splatted args... downwards, yet have it know to handle the literals at the top level because of how they are handled in the internal function. I wouldn't know how to solve that easily solve with this.

@Datseris
Copy link

Datseris commented Oct 5, 2017

I am posting this as a simple example:

function lyapunov(ds::ContinuousDynamicalSystem, T::Real = 10000.0,
  return_convergence::Val{B} = Val{false}; kwargs...) where {B}

  # code code code

  λts, ts = lyapunov(integ1, integ2, T; kwargs...)  # this function does all computations (and is type-stable)
  # this simply decides the return values:
  if B
    return λts, ts
  else
    return λts[end]
  end
end

Chris instructed me to do this like it is, so that the function is still type-stable but also can return different things based on user input.

@ChrisRackauckas
Copy link
Member Author

Maybe the less magical way to handle this would be a way to designate in a function that the values for a specific argument should turn into a value-type, and have the literal/constant automatically turn into the value type. That way passing the value-type works, passing the literal/constant works, and passing a variable would incur a dynamic dispatch (but you'd use this in the case where it's very rare for that option to not be hardcoded).

@yuyichao
Copy link
Contributor

yuyichao commented Oct 5, 2017

Dup of #5560

@StefanKarpinski
Copy link
Member

Very low hanging fruit here is to specialize on default values.

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

Successfully merging a pull request may close this issue.

4 participants