Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MonoTouch: About value types as Dictionary Keys

It says that using value types as dictionary keys will "crashes and burns rather quickly on the device", does that means I can't use something like Dictionary<int, string> to make a string lookup table?

like image 665
horeaper Avatar asked Feb 17 '23 21:02

horeaper


1 Answers

It says that using value types as dictionary keys will "crashes and burns rather quickly on the device",

It's not so dramatic (no burn) or so automatic - but it can happen.

Apple does not allow JIT (just in time) compilation on devices. This means that everything must be pre-compiled (ahead of time) before being deployed to devices. This means a few limitations exists for MonoTouch that you would not have with .NET / Mono or Mono for Android.

In general the generated code for generics can be shared between different types. Sadly this is not possible for value types. This means the AOT (ahead of time) compiler must generate code for every value type being used.

In some cases the AOT compiler might not be able to detect every possible type that might be needed at runtime. This will cause an EngineExecutionException that will point you to the offending code.

At this stage you can try to:

  • hint the AOT compiler that the code is needed (i.e. add some code that will make the AOT compiler generates the required missing code); or

  • refactor the code, e.g. to avoid value types;

does that means I can't use something like Dictionary to make a string lookup table?

No, that will work. When you create an instance of Dictionary<int, string> then it's pretty straightforward for the AOT compiler to know what code needs to be generated.

Problems generally occurs when nesting generics or when using code that does (e.g. LINQ queries with value types can generate such code).

like image 64
poupou Avatar answered Mar 04 '23 01:03

poupou