- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Apparent inconsistency in definitions of structural types
Wed, 2011-12-14, 03:35
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
^
====================================================================
Wed, 2011-12-14, 14:51
#2
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
> ^
> ====================================================================
>
>
Wed, 2011-12-14, 15:11
#3
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.
> 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
^