Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple declarations of x

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} 
like image 466
Mickael Bergeron Néron Avatar asked Jun 22 '14 14:06

Mickael Bergeron Néron


People also ask

What is multiple declaration?

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.

What is multiple declaration error in C?

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.


2 Answers

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.

like image 50
GS - Apologise to Monica Avatar answered Sep 17 '22 09:09

GS - Apologise to Monica


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

like image 39
palik Avatar answered Sep 20 '22 09:09

palik