Why is this entry being registered?

Given the data type

data Foo = Foo { one :: Int, two :: String } deriving (Show)

      

An incomplete expression passes the type check - for example,

foo :: Foo
foo = Foo { one = 5 }

main = print foo

      

Typechecks (emitting incomplete write warning), then (obviously) crashing when an expression is encountered. Why does it pass? Without notation syntax, it is not (i.e. bar = Foo 5 :: Foo

).

+3


source to share


1 answer


Haskell 2010 report says section 3.15.2 Construct using field shortcuts

The flagged constructor can be used to construct a value that specifies components by name rather than position. Unlike the parentheses used in declaration lists, they don't qualify for layout; characters {and} must be explicit. (This also applies to field updates and field templates.) Design using field labels is subject to the following restrictions: [...]

  • Fields not mentioned are initialized with ⊥.

  • Compilation error when any strong fields (fields whose declared types are prefixed!) Are omitted during construction.

So part of the language specification and the compiler must accept the code. All fields are initialized, only some of them are initialized with undefined

.

foo = Foo{ one = 5 }

      



equivalent to

foo = Foo 5 undefined

      

A good compiler will warn you about this if you ask for it. If you want an error, make the fields strict.

+10


source







All Articles