7

I have a function f which takes as one of its arguments an Association object, and I would like to produce a nice usage message for it, including a proper autocomplete template.

As an example, consider the function

f[association_] := {Keys[association], Values[association]}

which turns e.g.

f[<|a -> b, c -> d|>]

into {{a, c}, {b, d}}, and so on. The natural autocomplete template to use is then

f::usage = "f[\[LeftAssociation]a\[Rule]b,c\[Rule]d,…\[RightAssociation]] 
                returns {{a,c,…},{b,d,…}}.";

and it formats nicely when I ask for ?f. However, if I try to use the template for autocomplete using Ctrl+Shift+K, I get the following:

Note, in particular, that it's somehow turned the Association symbols <| and |> into patterns to be filled in, which is obviously not the desired behaviour. This is a bit strange, particularly since <| is described by its documentation as a

Matchfix operator with built-in evaluation rules.

much like { or , so it cannot mean a template for some other variable. Moreover, this is independent of whether the separated form <| (i.e. < then |) or the single-symbol \[LeftAssociation] is used in the usage message string.

Is there some way to make the produced template include correctly-formatted <|s and |>s?

Emilio Pisanty
  • 10,255
  • 1
  • 36
  • 69
  • But then is it really going to be better for your users to get an explicit <| a -> b... |> with autocompletion instead of a single assoc pattern? Sometimes it might be but in many cases they might want to pass a variable instead of an explicitly written out association. That said, I'd also like this feature and +1 ... – Szabolcs May 04 '16 at 18:10
  • 1
    (To be honest I won't ever be passing an explicit <| a -> b... |> either, but I want the template to be dead clear on what the function expects.) – Emilio Pisanty May 04 '16 at 19:15
  • Reported as a bug to WRI. Will update if I hear back from them. – Emilio Pisanty May 04 '16 at 21:53

1 Answers1

8

No, I don't think it's possible. The reason is that built-in symbols are plagued by the same problem.

enter image description here

I would actually consider this a bug. It's confusing and hinders usability. Plus, other operators, such as ->, are not turned into patterns.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • Thanks, this is a good find. I'll report it to WRI when I have time. – Emilio Pisanty May 04 '16 at 19:15
  • Note also that here is turned into a pattern, though in general this is not the case, particularly with options. – Emilio Pisanty May 04 '16 at 19:16
  • @EmilioPisanty Also, would you kindly send this link along with your report to WRI, too: http://mathematica.stackexchange.com/questions/58669/what-is-the-status-of-usage-and-templates-in-mathematica-v10-1?lq=1 ? It's a problem that's been around for years that needs to be fixed. – QuantumDot May 04 '16 at 19:55