This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Apparent inconsistency in definitions of structural types

3 replies
Maxime Lévesque
Joined: 2009-08-18,
User offline. Last seen 42 years 45 weeks ago.

The compiler seems to prefer methods defined with "()" ending, when
structural types are defined, giving it one method without is tolerated, more than one will get
it upset. Is this a bug ?


The following snipets compile :
====================================================================
scala>   type Z = { def isEmpty(): Boolean; def trim(): String}
defined type alias Z

scala>   "" : Z
res2: Z = ""

scala>   type Z = { def isEmpty: Boolean}
defined type alias Z

scala>   "" : Z
res3: Z =
====================================================================


The following don't :

====================================================================
scala>   type Z = { def isEmpty(): Boolean;  def trim: String }
defined type alias Z

scala>   "" : Z
<console>:9: error: type mismatch;
 found   : java.lang.String("")
 required: Z
                "" : Z
                ^


scala>   type Z = { def isEmpty(): Boolean; def trim: String}
defined type alias Z

scala>   "" : Z
<console>:9: error: type mismatch;
 found   : java.lang.String("")
 required: Z
                "" : Z
                ^
====================================================================


Detering Dirk
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
RE: Apparent inconsistency in definitions of structural types

> The compiler seems to prefer methods defined with "()" ending, when
> structural types are defined, giving it one method without is
> tolerated, more than one will get
> it upset. Is this a bug ?

Seems to be a bit more complicated than that:

scala> type Z = { def isEmpty:Boolean }
defined type alias Z

scala> "":Z
res3: Z =

scala> type Z = { def trim: String }
defined type alias Z

scala> "":Z
:9: error: type mismatch;
found : java.lang.String("")
required: Z
"":Z
^

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Apparent inconsistency in definitions of structural types

In structural types, methods without parameter lists are different
than methods with one empty parameter list.

What is happening here, I'd wager, is that String defines isEmpty(),
while WrappedString defines isEmpty, so the latter is available
through implicit conversion.

The same is not true for trim().

People (including Paul Phillips) have asked before for these two being
considered equal, but Odersky was pretty firm in that this cannot be
allowed.

2011/12/14 Maxime Lévesque :
>
> The compiler seems to prefer methods defined with "()" ending, when
> structural types are defined, giving it one method without is tolerated,
> more than one will get
> it upset. Is this a bug ?
>
>
> The following snipets compile :
> ====================================================================
> scala>   type Z = { def isEmpty(): Boolean; def trim(): String}
> defined type alias Z
>
> scala>   "" : Z
> res2: Z = ""
>
> scala>   type Z = { def isEmpty: Boolean}
> defined type alias Z
>
> scala>   "" : Z
> res3: Z =
> ====================================================================
>
>
> The following don't :
>
> ====================================================================
> scala>   type Z = { def isEmpty(): Boolean;  def trim: String }
> defined type alias Z
>
> scala>   "" : Z
> :9: error: type mismatch;
>  found   : java.lang.String("")
>  required: Z
>                 "" : Z
>                 ^
>
>
> scala>   type Z = { def isEmpty(): Boolean; def trim: String}
> defined type alias Z
>
> scala>   "" : Z
> :9: error: type mismatch;
>  found   : java.lang.String("")
>  required: Z
>                 "" : Z
>                 ^
> ====================================================================
>
>

Maxime Lévesque
Joined: 2009-08-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Apparent inconsistency in definitions of structural types

Thanks Daniel, I was a bit puzzled trying to deduce the rule from the observed behavior.

It isn't clear to me from reading this closed issue :

  https://issues.scala-lang.org/browse/SI-4506

what would be the consequence of making parameterless and nullary methods equivalent,
I can only see positive ones, but maybe that's a question for scala-language...

ML

2011/12/14 Daniel Sobral <dcsobral@gmail.com>
In structural types, methods without parameter lists are different
than methods with one empty parameter list.

What is happening here, I'd wager, is that String defines isEmpty(),
while WrappedString defines isEmpty, so the latter is available
through implicit conversion.

The same is not true for trim().

People (including Paul Phillips) have asked before for these two being
considered equal, but Odersky was pretty firm in that this cannot be
allowed.

2011/12/14 Maxime Lévesque <maxime.levesque@gmail.com>:
>
> The compiler seems to prefer methods defined with "()" ending, when
> structural types are defined, giving it one method without is tolerated,
> more than one will get
> it upset. Is this a bug ?
>
>
> The following snipets compile :
> ====================================================================
> scala>   type Z = { def isEmpty(): Boolean; def trim(): String}
> defined type alias Z
>
> scala>   "" : Z
> res2: Z = ""
>
> scala>   type Z = { def isEmpty: Boolean}
> defined type alias Z
>
> scala>   "" : Z
> res3: Z =
> ====================================================================
>
>
> The following don't :
>
> ====================================================================
> scala>   type Z = { def isEmpty(): Boolean;  def trim: String }
> defined type alias Z
>
> scala>   "" : Z
> <console>:9: error: type mismatch;
>  found   : java.lang.String("")
>  required: Z
>                 "" : Z
>                 ^
>
>
> scala>   type Z = { def isEmpty(): Boolean; def trim: String}
> defined type alias Z
>
> scala>   "" : Z
> <console>:9: error: type mismatch;
>  found   : java.lang.String("")
>  required: Z
>                 "" : Z
>                 ^
> ====================================================================
>
>



--
Daniel C. Sobral

I travel to the future all the time.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland