# a caveat in Iconize

We got this nice Iconize function to shrink code in notebook from version 11.3. But, I noticed a caveat in reverting an Iconize-d object back: if the wrapped stuff is comment, then the reversion (by using Convert to Standard Form) will give a Null rather than the comment before.

Say for the comment below:

(*some nice comment;
Plot[Sin[x],{x,-10,10}]
*)


If you Iconize it and then Convert to Standard Form, you'll get

Null


I think it can hardly be considered as bug as to some sense the commented code should give Null; but it definitely doesn't give the comment back.

So, is there any workaround to avoid Null if we de-Iconize a comment?

It is not a bug. Iconize works with expressions. Comments / line breaks / indentation and other box/input features are stripped by the FrontEnd before sending it to the Kernel.

Iconize[
Hold@Range[1,
10
]
]
(*evaluate and convert to StandardForm*)

Hold[Range[1, 10]] (*no line breaks, indentation or @ *)

• Thanks for the explanation! I'll use the feature with your caution/suggestion in mind. Mar 15 '18 at 14:20
• @sunt05 how would you like to be able to use a feature like Iconize which would preserve comments etc?
– Kuba
Mar 15 '18 at 19:59
• the question was raised when I was planning to switch between several functions with the same name: I wrapped the alternative functions in comment so I can easily switch between them by un/commenting. As some of the alternative functions take large space, I was trying this new feature out and then fell into the Null scenario. Now I know I should just Iconize the content in the comment rather than the whole. Mar 15 '18 at 20:15
• @sunt05 To put it more explicitly, Iconize preserves expressions, not code. f@x, f[x] and (* look: *) x // f are different code, but represent the same expression, and therefore iconize to the same. Mar 15 '18 at 23:08
• @Szabolcs partially better, but saying that it preserves expressions suggests it could preserve code. It is not up to Iconize.
– Kuba
Mar 16 '18 at 7:26

Here's a follow up from Kuba's point. If you wanted to pass a few Cells around (say storing a function definition) you can cache them in an Unevaluated manner so that they auto-write when evaluated. For instance:

iconizeCell[c_CellObject] :=
Iconize[Unevaluated[NotebookWrite[EvaluationNotebook[], cont]]]
]
(* insert a cell break here *)
iconizeCell@PreviousCell[]


and evaluating that gets me a Cell with:

iconizeCell[c_CellObject] :=
Iconize[Unevaluated[NotebookWrite[EvaluationNotebook[], cont]]]
]


as its content

Note that this strategy could work for function definitions too. e.g.:

iconizeDefinition[fn_] :=

With[{defs = Association@LanguageExtendedFullDefinition[fn][[1, 2]]},
Iconize[
Unevaluated[
KeyValueMap[
Function[#[fn] = #2],
defs
];
]
]
]

iconizeDefinition[iconizeDefinition]


Then running ClearAll[iconizeDefinition]` and evaluating that blob gets me my definitions back.