I wouldn’t base an API on this though.
I assume this is because this is not guaranteed to remain like this, yes?
I feel a bit hampered when dealing with Function
objects because there is little information that I can access about them. They seem a bit black-box to me, as compared to standard types. methods
seems like the main point of access to these, but it’s not the first time I’ve read that we should not use this in our packages.
There is probably a cleaner and more idiomatic way to do what you want, but it is hard to say without more context.
The context is this. I have a function hamiltonian!(h; onsitefield = (o, i) -> o)
that takes a matrix h
representing a Hamiltonian of a quantum system, and changes the diagonal element h[i,i]
into onsitefield(h[i, i], i)
. Now, I also want to allow the user to say hamiltonian!(h; onsitefield = i -> some function of i)
, in which case h[i,i]
should become onsitefield(i)
. I don’t know how to do this, since in my code I have to hardcode either onsitefield(h[i, i], i)
or onsitefield(i)
. I would ideally like to use some form of dispatch to call one or the other, depending on the form of the onsitefield
kwarg the user provides.
I come from Mathematica, where solving this is very easy. An anonymous function like f = x -> cos(x)
, which in Mathematica is written as f = Cos[#1]&
, can be called with any number of arguments, and the excess ones are just dropped, i.e. I can write f[1,2,3,4,5]
and this returns Cos[1]
without complaining. This would solve the above problem, but I don’t know how to replicate that in Julia.