Pluto.jl icon indicating copy to clipboard operation
Pluto.jl copied to clipboard

Name collision when running a cell with a macro more than once

Open littlebenlittle opened this issue 1 year ago • 2 comments

I'm getting the following error when a cell containing a maco runs more than once

cannot assign a value to variable workspace#4.MyCategory from module workspace#5

This does not happen when the same code is run the REPL.

pluto-err.txt (used txt extension because github doesn't allow jl for uploads)

https://user-images.githubusercontent.com/31575792/180509820-12f8d2d2-68bc-4189-bbd5-6f3042351ff4.mp4

littlebenlittle avatar Jul 22 '22 19:07 littlebenlittle

Thanks for the report! The @theory macro does not currently seem Pluto friendly in that it actually evaluates code at runtime instead of expansion time. Therefore, Pluto cannot figure out that this is an assignment to MyCategory.

Pangoraw avatar Jul 24 '22 13:07 Pangoraw

This is a good thing to figure out as having Pluto work with Catlab would be really awesome.

What would we need to do to make it Pluto-friendly? We have a lot of macros that generate a call to eval because we need to intersperse runtime and compiletime information. For example, you can make programmatically make a database schema with runtime information and then generate Julia types to store instances of that database. Methods on those types use generated functions to eliminate runtime processing of the schema information. The generation of types depends on the runtime values in the program. This kind of interaction between compile- and run- time is critical to the design, so we can't just eliminate it.

Is the problem any time that you have a macro the emits a call to eval?

jpfairbanks avatar Aug 29 '22 13:08 jpfairbanks

What would we need to do to make it Pluto-friendly?

A common trick which does not require too much code change is to return an expression like this from the @theory macro:

if false
  MyCategory = nothing
end

Since pluto does not evaluate conditions to figure out assignments, the cell will count as assigning to MyCategory.

Pangoraw avatar Jan 24 '23 11:01 Pangoraw