[clean-list] Selecting record fields with same names

Maarten de Mol maartenm@cs.kun.nl
Mon, 11 Feb 2002 08:36:05 +0100


Hallo Claudio,

> Hello everybody,
> I have question about the selection of record fields:
>
> if I create two record types having fields with the same name, I seem
> to be incapable to select the fields (I am using the new Clean 2.0).
>
> Example:
>
>  1:  module test
>  2:
>  3:  import StdEnv
>  4:
>  5:  :: Rec1 = {  i:: Int, s:: String, r:: Real  }
>  6:  :: Rec2 = {  i:: Int, s:: String }
>  7:
>  8:  appendStrings :: Rec1 Rec2 -> String
>  9:  appendStrings r1 r2 = r1.s +++ r2.s
> 10:
> 11:  Start=appendStrings {Rec1 | i=0,s="str1",r=0} {Rec2 | i=0,s="str2"}
>
>
> in line 9 the compiler should be able to tell that 'r1' is of type 'Rec1'
> (the function type will tell this), but I get the following error:
>
>    Error [test.icl,9,]: s ambiguous selector specified
>
> Am I forced to have different names for the fields? (The language report
> says that this is not the case)

You can have the same field names in different records. However, as you
already found out, you sometimes have to specify explicitly which record
type the field name belongs to. In this case, the function appendStrings
can be fixed as follows:

   appendStrings :: Rec1 Rec2 -> String
   appendStrings r1 r2 = r1.Rec1.s +++ r2.Rec2.s
                            ^^^^          ^^^^

I don't know exactly why the compiler isn't able to figure this out
itself (although I have run into this several times myself); it has
something
to do with the typing algorithm I'm sure.

>
> Claudio Potenza
> claudio.potenza@mclink.it
>

Maarten de Mol