LLS
LLS

Reputation: 2228

What is the nature of F# exception type

I am learning F# and I found an interesting question. We can use F# type exception

exception MyExpetion of string

However, when we match it, we can use the same syntax as matching a discriminated union. At the same time, it is a System.Exception. How is this done? What is the type of F# exception? I tried to search but no clear answer is found. Thank you.

Upvotes: 1

Views: 107

Answers (1)

kvb
kvb

Reputation: 55184

You can think of exception types as a sort of "open" discriminated union, where additional constructors for the Exception type can be added at any time. Therefore, as you might expect, the static type of MyException "test" is Exception, and values of type Exception can be matched against the pattern MyException _.

Upvotes: 5

Related Questions