[Macaulay2] avoid warning for mutable symbol shadowing?

57 views
Skip to first unread message

Andrew Critch

unread,
Jan 3, 2012, 4:04:32 PM1/3/12
to maca...@googlegroups.com
Short version: Is it possible / acceptable to disable warnings about *mutable* symbol shadowing?  OR, if I use a certain symbol in two different packages, but only as a hash key, how can I avoid getting a "symbol is shadowed" warning?

Long version: When I use a nice intuitive symbol like "mean" as a hash key, M2 wants me to export it.  There is no need to protect "mean" (right?) if it will only ever be used in symbol form as a hash key, so I use exportMutable.  But nonetheless if I write two packages that use "mean" as a hash key symbol, M2 gives a warning, like:

--warning: symbol "mean" in Foo.Dictionary is shadowed by a symbol in Bar.Dictionary
--  use the synonym Foo$mean

Here are two packages that produce that warning when loaded:

Foo.m2:
--------
newPackage "Foo"
export{foo}
exportMutable{mean}
foo=new MutableHashTable;
foo.mean="something";
--------
Bar.m2:
--------
newPackage "Bar"
export{bar}
exportMutable{mean}
bar=new MutableHashTable;
bar.mean="something else";
--------

I know I could also just use #"mean" everywhere instead of .mean, but that would end up being much less ergonomic / natural for the user to type...  So I keep wondering if I can/should just disable mutable symbol shadow warnings.  Hence the question :)

Thanks,
--
Critch, Andrew
UC Berkeley
http://math.berkeley.edu/~critch/

Grayson, Daniel R.

unread,
Jan 3, 2012, 5:54:38 PM1/3/12
to maca...@googlegroups.com
If symbols with the same name are exported from two packages, then there
are two symbols, and one will not serve as the other as a key in a hash
table. So there could be confusion, and thus the warning messages are probably
appropriate.

What about creating a new symbol in the user's own dictionary for this purpose,
this way?:

newPackage "Foo"
export{foo}
mean = getSymbol "mean"
foo=new MutableHashTable;
foo#mean="something";

Don't export "mean". Then the user can type

foo.mean

and there will be no problem if multiple packages do the same thing, or
if this package is reloaded.

> --
> You received this message because you are subscribed to the Google Groups "Macaulay2" group.
> To post to this group, send email to maca...@googlegroups.com.
> To unsubscribe from this group, send email to macaulay2+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/macaulay2?hl=en.

Andrew Critch

unread,
Jan 3, 2012, 11:03:29 PM1/3/12
to maca...@googlegroups.com
Ah, that is exactly what I needed!  Thank you also for patiently correcting my confusion about the constancy of the symbols; this understanding is a huge help. I hope my questions will eventually result in me producing a useful package for something :)

On Tue, Jan 3, 2012 at 2:54 PM, Grayson, Daniel R. <d...@math.uiuc.edu> wrote:
If symbols with the same name are exported from two packages, then there
are two symbols, and one will not serve as the other as a key in a hash

Andrew Critch

unread,
Jan 5, 2012, 12:24:50 AM1/5/12
to maca...@googlegroups.com
Okay, I made the following method for setting mutable symbols that need not be exported:

smus=setMutableUnexportedSymbol=method()
smus String := s -> (
     getGlobalSymbol(currentPackage#"private dictionary",s) <- getSymbol s;
     protect (currentPackage#"private dictionary")#s)
smus Symbol := s -> smus toString s
smus List := L -> smus \ L

Then, if I have short symbols like {foo, bar} that I will use *only* as hash keys in the actual package, instead of export{foo,bar}, I do

smus{foo,bar}

Dan, as you said, for hashing within the package I write H#foo and H#bar to effectively define the keys getSymbol"foo" and getSymbol"bar" for H, so then the user can write H.foo and H.bar and get the expected behavior.  Great!


--
Critch, Andrew
UC Berkeley
http://math.berkeley.edu/~critch/


kroeker

unread,
Jan 5, 2012, 6:23:53 AM1/5/12
to maca...@googlegroups.com
Dear Andrew,



> smus{foo,bar}

good, but watch out for using table.bar instead of table#bar inside the package (you will get a different hash table entry).
It seems that a perfect solution for the problem is not possible...


Best,


Jakob
Reply all
Reply to author
Forward
0 new messages