12

For convenience I'm trying to add an extra property to CountryData

Unprotect[CountryData];
CountryData[c_String, "MyProperty"] := 0; (* actually call to another function *)
Protect[CountryData];

But when using it I get:

CountryData["Greenland", "MyProperty"]

CountryData::notprop: "!(\"\\"MyProperty\\"\") is not a known property for CountryData. Use CountryData[\"Properties\"] for a list of properties."

What's the right way to do this?

There is high probability this is a duplicate, but I wasn't able to find how to do this, sorry in advance

VividD
  • 3,660
  • 4
  • 26
  • 42
ssch
  • 16,590
  • 2
  • 53
  • 88

1 Answers1

19

This is a matter of rules ordering for CountryData definitions. You have to do something like this:

Unprotect[CountryData];
CountryData[c_String, "MyProperty"] := 0;
(*actually call to another function*)
DownValues[CountryData] = RotateRight[DownValues[CountryData]];
Protect[CountryData];

This reorders the definitions so that yours is at the top (or close to the top). Then:

CountryData["Greenland","MyProperty"]

(* 0 *)
Leonid Shifrin
  • 114,335
  • 15
  • 329
  • 420