I have this code and it won't compile, highlighting the x and y right of Point3 and writing: "Multiple declarations of x" and "Multiple declarations of y". What's wrong? Can't Point2 and Point3 have the same member name?
data Point2 = Point2 {x :: Float, y :: Float} data Point3 = Point3 {x :: Float, y :: Float, z :: Float}
All function declarations for a particular function must have the same number and type of parameters, and must have the same return type. These return and parameter types are part of the function type, although the default arguments and exception specifications are not.
E2238 Multiple declaration for 'identifier' (C++)An identifier was improperly declared more than once. This error might be caused by conflicting declarations, such as: int a; double a; A function declared in two different ways. A label repeated in the same function.
No, this is not currently supported. The standard approach is to prefix each field with something unique to the particular datatype, e.g. p2x
, p2y
, p3x
etc.
The reason this isn't supported is that each record field name implicitly generates a "selector" function, e.g. x :: Point2 -> Float
. Having two fields with the same name in the same scope would generate a clash.
This is a long-standing bugbear for many people and will be addressed by the upcoming language extension OverloadedRecordFields
, which will hopefully be part of GHC 7.12 (due early 2016).
When enabled, this extension will allow the same field name to be used in multiple records. The field selector will have an overloaded type which will generally be resolved by type inference.
The
DuplicateRecordFields
extension permits existing Haskell records to use duplicate field labels.
See DuplicateRecordFields
OverloadedRecordFields doesn't work for me with ghc-8.0.2
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