Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
2  
Tangentially related: mathematica.stackexchange.com/q/2793/121 –  Mr.Wizard Jan 21 '13 at 19:54
add comment

1 Answer

up vote 14 down vote accepted

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 *)
share|improve this answer
    
Perfect, thanks! –  ssch Jan 21 '13 at 19:49
    
@ssch Was glad to help. I think I've run into exactly such problem with ...Data before. Also a good example of when manual rule reordering is really needed. Thanks for the accept. –  Leonid Shifrin Jan 22 '13 at 10:14
    
@Leonid Shifrin from where do you know all that stuff!? Pretty impressing.manual rule reordering ... No shit Sherlock ;) –  Stefan Jan 22 '13 at 20:06
    
@Stefan Long experience... :-). This particular issue is covered in Mathematica book and also David Wagner's book on Mathematica programming. –  Leonid Shifrin Jan 23 '13 at 10:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.