halirutan / Wolfram-Language-IntelliJ-Plugin-Archive

Wolfram Language and Mathematica plugin for IntelliJ IDEA.
https://wlplugin.halirutan.de/
Other
193 stars 26 forks source link

Function definition with pattern match: support for Set #179

Closed smao-astro closed 3 years ago

smao-astro commented 3 years ago

Is your feature request related to a problem? Please describe. I am not sure is that I am doing something uncommon or this is an unimplemented feature. Basically rather than defining function using pattern match + SetDelayed, sometimes I prefer to use Set. While the plugin works smoothly for the first case, in the second case when I move my cursor onto parameters inside my function, I get warning below

Could not resolve symbol definition Inspection info: Reports symbols for which the definition could not be found. This usually means that you used a symbol that has no value, a usage message, options or other values and which are not locally bound by, e.g. Module, Table, Compile, etc. In the Wolfram Language, such symbols exist when they are used as, e.g. inert wrappers that don't have functionality. However, in package-code, it is likely that such symbols are bugs and they should be brought to the attention of the developer. To prevent this warning for a particular symbol, one possible way is to give it a usage message which has the advantage of letting others know how a symbol is intended to be used. In this example, the inert symbols Node and Leaf would lead to a warning without the usage messages Node::usage = "Node[...] represents a node in a tree"; Leaf::usage = "Leaf is a symbol representing a leaf in a tree"; tree = Node[Node[Leaf,Leaf],Leaf]

mini template to reproduce

( ::Package:: )

BeginPackage[ "KeplerianRingInitialCondition`" ];

AzimuthalVelocityInitial::usage = "Function of azimuthal velocity distribution at t = 0, assuming Keplerian Disk.";

Begin["Private"];

AzimuthalVelocityInitial[r_] = r^(-1 / 2); End[];

EndPackage[ ]

Describe the solution you'd like If move the cursor to r in the function, r would be recognized as function parameter.

Thanks!

halirutan commented 3 years ago

The thing is that r is not necessarily the "function parameter" when you use Set. Definitions with = tell Mathematica to evaluate the right side before doing the assignment. Consider the following situation where you evaluated a simple test-code prior to defining your function:

r = 1.2;
phi = 1.5;
r*{Sin[phi], Cos[phi]}

If you now do

AzimuthalVelocityInitial[r_] = r^(-1/2);

you will get the following function definition for AzimuthalVelocityInitial

image

So Mathematica is using the definition of r from the outer scope. Only if such a definition does not exist, then r will evaluate to r and you will get the same AzimuthalVelocityInitial you'd get with :=.

This is why the plugin tells you that it can't find a definition for r because it really isn't supposed to use r_ here. Therefore, the behavior you're seeing is correct. In general, it is a hard problem for the plugin to determine if a symbol has a definition because to do it right, it would have to evaluate your code which it cannot do.

If you have good reasons for using = (aka, you know what you're doing), you could use

f[...] := Evaluate[...]

With this, you

  1. get the arguments of f recognized by the plugin
  2. get the same behavior as using =