Take the 2-minute tour ×

Given I have an enum:

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

And i try to print it like so

printf "%A" Cool.A

I get the following exception:


Why does this happen and (if possible) how I can fix this at the enum level?

share|improve this question
Works fine for me on fsi v2.0.0.0 (no exception, prints a). Your definition is slightly unidiomatic - F# doesn't tend to use the ocaml style - it would normally be type Cool = |A='a'|B='b' –  John Palmer Aug 17 '12 at 21:30
Cannot reproduce the behaviour with F# 2.0/.NET Framework 4.0 either. –  pad Aug 17 '12 at 21:36
are you sure that you are printing with the %A placeholder? It's there that lies the problem i think –  Paul Nikonowicz Aug 17 '12 at 21:52
I'm using F# 2.0 Interactive build 4.0.40219.1 –  Paul Nikonowicz Aug 17 '12 at 21:53
This was what I ran / got - type Cool = | A = 'a' | B = 'b' > printf "%A" Cool.A - ;; aval it : unit = () –  John Palmer Aug 17 '12 at 21:57
show 2 more comments

2 Answers

up vote 2 down vote accepted

I can repro this on a machine with F# 2.0 compiler targeting .Net 4.0, but not with F# 2 targeting .Net 3.5 or less, or on a VS2012 (F# 3.0) machine (where .Net 4.5 supersedes 4.0).

I am not sure right now of the cause, but hopefully this info may help you or others narrow it down or find a workaround.

share|improve this answer
add comment

I think it's better not to use char as base value for enum.
Although char seems to be allowed (MSDN article on F# enums), the corresponding article for C# says the opposite. Moreover, an older article says specifically:

base-type (Optional)
The underlying type that specifies the storage allocated for each enumerator. It can be one of the integral types except char. The default is int.

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

So, even if F# performs special handling of enum<char>, better to avoid it, keeping yourself from future integration problems.

share|improve this answer
add comment

Your Answer


