Yes I know there is no built-in native function called SymbolQ (but JavaScript does). However, could one be simulated to work for most cases? I often rely on objectName[symbol] and makeRuleRow[symbol] to return the name of a defined variable and its value in a ready-to-use row for structured Grid layouts of results to computations. However, sometimes an error is returned if a variable is not a Symbol which leaves me asking, "When is a symbol a Symbol?"
I would like to catch such errors and return as much useful information as possible. That is why I ask if there is an easy hack for determining if a variable is a symbol.
Here is some working code where I might use such a function...
SetAttributes[symbolQ, HoldAllComplete];
symbolQ[x_] := ResourceFunction["SymbolQ"][x];
SetAttributes[{objectName}, HoldFirst];
objectName = Function[Null, SymbolName[Unevaluated[#]], {HoldFirst}];
objectName::usage =
"objectName@# returns Unevaluated shortened SymbolName.";
SetAttributes[{makeRuleRow}, HoldFirst];
makeRuleRow[symbol_, altname_ : Null, desc_ : Null] :=
Module[{name = "", prepend = ""},
If[ResourceFunction["SymbolQ"][symbol] === False &&
altname === Null,
Return[Row[{Style["Argument ", Red], symbol,
Style[" is not a symbol. Use altname in makeRuleRow.", Red]}]
], False
];
name = If[StringQ[altname], altname, objectName[symbol]];
prepend = If[StringQ[desc], desc <> " ", ""];
{Row[{Style[prepend, Brown], name, rule}],
TraditionalForm[symbol]}
]
The following is how it would be used for most cases (including an error) expected to be encountered when setting up name-value pairs for Grid row elements...
xxx = 123;
makeRuleRow[xxx] (* this outputs name and value )
makeRuleRow[xxx, "alternate name"] ( this creates alternate name )
makeRuleRow[xxx, "alternate name", "this is a symbol"] ( this prepends a description and creates alternate name )
makeRuleRow[69] ( this generates an error message suggesting a fix )
makeRuleRow[69, "XXX"] ( bypasses error by creating alternate name )
makeRuleRow[69, "XXX", "not a symbol"] ( bypasses error by creating alternate name and prepend a description *)
The actual output when done correctly conveniently makes {name ->, value} rows ready to be inserted into two-column Grid layouts...
{xxx -> ,123}
{alternate name -> ,123}
{this is a symbol alternate name -> ,123}
Argument 69 is not a symbol. Use altname in makeRuleRow.
{XXX -> ,69}
{not a symbol XXX -> ,69}
SymbolQand paste it to my open notebook? As you can tell I am sort of new to Mathematica. – Jules Manson Jul 12 '20 at 20:35SymbolNameworks? Something like:symbolQ[x_]:=Quiet[Check[StringQ@SymbolName[x], False, General::sym], General::sym]– flinty Jul 12 '20 at 20:39ResourceFunction["SymbolQ"][x]to the clipboard. The function will download automatically. You can also download the source notebook if you want to see how it works internally. – Sjoerd Smit Jul 12 '20 at 20:50x=123you have to call it like thisResourceFunction["SymbolQ"][x]so i tried to make a short alias for it but it wouldn't worksymbolQ = ResourceFunction["SymbolQ"][#] &;. do you have ideas of how i could do that? – Jules Manson Jul 12 '20 at 20:56symbolQ = ResourceFunction["SymbolQ"]. My kernel init.m file is filled with definitions like that. – Jason B. Jul 12 '20 at 21:02SetAttributes[symbolQ, HoldAllComplete]; symbolQ[x_] := ResourceFunction["SymbolQ"][x]to make it work. I think I'll report a bug about this. – Sjoerd Smit Jul 12 '20 at 21:07foo[s_Symbol,..]:=...? – Michael E2 Jul 12 '20 at 21:53SubValues- based definitions hold their second group of arguments is to use theStack, more or less along the lines I described here - and if you look atDownValuesofResourceFunction, you will see that they use exactly that (which is an ugly hack that actually also adds significant overhead to the function call, potentially changing its complexity).This is why your method worked, but I just don't see an easy way to make it work as initially requested. – Leonid Shifrin Jul 12 '20 at 22:14Head@PlotandMatchQ[Plot, _Symbol]for instance. – Michael E2 Jul 12 '20 at 22:52symbolQto evaluate to the resource function and then evaluate the same asResourceFunction["SymbolQ"][x]. That's how it should behave and it doesn't. If it's fixable or not, I will leave up to whoever's in charge of that. – Sjoerd Smit Jul 13 '20 at 07:23symbolQ = ResourceFunction["SymbolQ", "Function"]to avoid this problem. See the answer I posted. – Sjoerd Smit Jul 28 '20 at 08:29