I think the title is already self explanatory, but here's an example anyway to show my point:
class Foo a where
someFunction :: a -> a -> Bool
instance Foo Bool
When I compile this the compiler gives a warning:
Warning:
No explicit method or default declaration for `someFunction'
in the instance declaration for `Foo Bool'
Calling the function will now result in a runtime error. Why is this a warning, and not a compile-time error? And is there any way to make this a compile-time error instead?
The GHC documentation provides an example where a warning is sufficient:
-fwarn-missing-methods
:This option is on by default, and warns you whenever an instance declaration is missing one or more methods, and the corresponding class declaration has no default declaration for them.
The warning is suppressed if the method name begins with an underscore. Here's an example where this is useful:
class C a where _simpleFn :: a -> String complexFn :: a -> a -> String complexFn x y = ... _simpleFn ...
The idea is that: (a) users of the class will only call
complexFn
; never_simpleFn
; and (b) instance declarations can define eithercomplexFn
or_simpleFn
.The
MINIMAL
pragma can be used to change which combination of methods will be required for instances of a particular class. See Section 7.20.5, “MINIMAL pragma”.
That's the reason missing methods don't result in an error, but a warning. If you want to make warnings fatal, use -Werror
. Since there is no -ferr-missing-methods
, -Werror
is the only way to make -fwarn-missing-methods
a compiler error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With