ToString exception on print listing

Given that I have an enumeration:

type Cool = A = 'a' | B = 'b' 

And I'm trying to print it like this

 printf "%A" Cool.A 

I get the following exception:

ToString exception: the value passed must be an enumeration base or a base type for an enumeration, such as Int32. Parameter Name: Value

Why is this happening and (if possible) how can I fix this at the enumeration level?

+6
source share
2 answers

I can reproduce this on a machine targeting the F # 2.0.NET 4.0 compiler, but not on targeting F # 2.NET 3.5 or less, or on the machine VS2012 (F # 3.0) (where .Net 4.5 replaces 4.0).

I'm not sure right now about the reason, but hopefully this information can help you or others narrow it down or find a workaround.

+3
source

I think it is better not to use char as the base value for enum .
Although char seems valid ( MSDN article on F # enumerations ), the corresponding article for C # suggests otherwise. Moreover, an older article says specifically:

base type (optional)
The main type that defines the storage allocated for each enumerator. It can be one of integral types , except char . The default value is int.

Looking at the source code of System.Enum.ToObject(Type,Object) , it is also obvious that char not supported.

So, even if F # performs special enum<char> processing, it is best to avoid this while preserving yourself from future integration issues.

+2
source

Source: https://habr.com/ru/post/923153/


All Articles