Why do Maybe/Optional types use a Just/Some type instead of the actual type?

前端 未结 6 581
日久生厌
日久生厌 2020-12-19 02:16

In Idris, the Maybe type is defined as followed:

data Maybe a = Just a | Nothing  

It\'s defined similarly in Haskell:

6条回答
  •  悲&欢浪女
    2020-12-19 02:31

    The problem is that if Maybe was defined the way you propose, i.e. data Maybe a = a | Nothing there would be no way to differentiate a values from Maybe a values (and Maybe (Maybe a) for that matter).

    So you may ask, why do we need to have such a distinction? What are the benefits? To give you a concrete example, suppose that we have a SQL table with a integer NOT NULL column. We would represent that with an Int in haskell. Now if we later on changed the database schema to make the column optional by dropping the NOT NULL constraint, we would have to change the haskell representation of the column to Maybe Int. The clear distinction between Int and Maybe Int would make it very easy to refactor our haskell code to account for the new schema. The compiler would complain for things such as extracting a value from the db and treating it as an Int (it might not be an integer, it might be NULL).

提交回复
热议问题