Overview
- A few attempts to write down the type
- A bash script that lists every attempt together with a somewhat cleaned up error message
- Edit 2018-04-11: Shouldn't the rhs be typed as
{ val y = new C; y.O }
?
I can't prove that it is impossible, but at least I wanted to make sure that I didn't give up without trying.
It's not really an answer, rather a list of failed attempts, maybe someone
finds it amusing, maybe someone finds the script (in the second part) for generating the list of error messages useful for reporting their findings...
A few attempts to write down the type
Systematic rewriting of AlexeyRomanov's attempt that looked most promising:
val o: x.O.type forSome { val x: C } = (new C).O
val o: x.type#O.type forSome { val x : C } = (new C).O
val o: t#O.type forSome { type t <: C with Singleton } = (new C).O
Some more or less unsystematic attempts with type-lambda like constructs:
// boring, does nothing, identity, just to recall lambda-syntax
val o: ({ type R <: c.O.type forSome { val c: C } })#R = (new C).O
val o: ({type L[A <: C] = c.O.type forSome { val c: A }})#L[c.type forSome{val c: C}] = (new C).O
val o: ({type L[A <: C] = c.O.type forSome { val c: A }})#L[A forSome {type A <: C}] = (new C).O
Experiment with nested forSome
:
val o: x.type forSome { val x: c.O.type forSome { val c: C }} = (new C).O
Experiments with additional type members in C
:
class C {
object O
type T = O.type
}
val o: C#T = (new C).O
This actually compiles, but modifies the right hand side, so I guess it doesn't count:
val o: c.O.type forSome { val c: C } = { val c = new C; c.O }
Conclusion: Looks impossible to me.
Bash script that generates all the error messages
To generate all the error messages (without file paths and feature warnings),
save the part of the post above this line as
saved_post.txt
, and then run the following script in the same directory:
Disclaimer: this script actually modifies files in your file system. Please make sure that you really understand what it does, and really want to run it, before running it. In particular, it destroys the file 'newCdotO.scala'.
#!/bin/bash
tempFile=newCdotO.scala
inputFile=saved_post.txt
grep -oE "^ *val o:.*$" $inputFile | \
while read codeLine
do
printf '=%.0s' {0..80}
echo ""
echo "" > $tempFile
echo "import scala.language.existentials" >> $tempFile
echo "import scala.language.higherKinds" >> $tempFile
echo "class C { object O; type T = O.type }" >> $tempFile
echo "$codeLine" | tee -a $tempFile
printf -- '-%.0s' {0..80}
echo ""
scala $tempFile 2>&1 | sed 's|^.*error:|error:|g'
done |
awk '{print " "$0}'
This generates the following wall of error messages (Hey, I've tried to clean it up!):
=================================================================================
val o: x.O.type forSome { val x: C } = (new C).O
---------------------------------------------------------------------------------
error: type mismatch;
found : this.C#O.type
required: x.O.type forSome { val x: this.C }
val o: x.O.type forSome { val x: C } = (new C).O
^
one error found
=================================================================================
val o: x.type#O.type forSome { val x : C } = (new C).O
---------------------------------------------------------------------------------
error: ';' expected but '.' found.
val o: x.type#O.type forSome { val x : C } = (new C).O
^
one error found
=================================================================================
val o: t#O.type forSome { type t <: C with Singleton } = (new C).O
---------------------------------------------------------------------------------
error: ';' expected but '.' found.
val o: t#O.type forSome { type t <: C with Singleton } = (new C).O
^
one error found
=================================================================================
val o: ({ type R <: c.O.type forSome { val c: C } })#R = (new C).O
---------------------------------------------------------------------------------
error: type mismatch;
found : this.C#O.type
required: AnyRef{type R <: c.type#O.type forSome { type c.type <: this.C }}#R
val o: ({ type R <: c.O.type forSome { val c: C } })#R = (new C).O
^
one error found
=================================================================================
val o: ({type L[A <: C] = c.O.type forSome { val c: A }})#L[c.type forSome{val c: C}] = (new C).O
---------------------------------------------------------------------------------
error: type mismatch;
found : this.C#O.type
required: c.type(in type L)#O.type forSome { type c.type(in type L) <: c.type(in value o) forSome { type c.type(in value o) <: this.C with Singleton } with Singleton }
val o: ({type L[A <: C] = c.O.type forSome { val c: A }})#L[c.type forSome{val c: C}] = (new C).O
^
one error found
=================================================================================
val o: ({type L[A <: C] = c.O.type forSome { val c: A }})#L[A forSome {type A <: C}] = (new C).O
---------------------------------------------------------------------------------
error: type mismatch;
found : this.C#O.type
required: c.O.type forSome { val c: A forSome { type A <: this.C } }
val o: ({type L[A <: C] = c.O.type forSome { val c: A }})#L[A forSome {type A <: C}] = (new C).O
^
one error found
=================================================================================
val o: x.type forSome { val x: c.O.type forSome { val c: C }} = (new C).O
---------------------------------------------------------------------------------
error: type mismatch;
found : this.C#O.type
required: x.type forSome { val x: c.type#O.type forSome { type c.type <: this.C } }
val o: x.type forSome { val x: c.O.type forSome { val c: C }} = (new C).O
^
one error found
=================================================================================
val o: C#T = (new C).O
---------------------------------------------------------------------------------
error: type mismatch;
found : this.C#O.type
required: _1.O.type forSome { val _1: this.C }
val o: C#T = (new C).O
^
one error found
=================================================================================
val o: c.O.type forSome { val c: C } = { val c = new C; c.O }
---------------------------------------------------------------------------------
EDIT 2018-04-11:
Just stumbled upon this here in 6.4 of the specification:
For other expressions e
, e.x
is typed as if it was { val y = e; y.x }
, for some fresh name y
.
The parts before this sentence describing the "not-other" expressions seem to refer to ordinary names and stable identifiers. Given this clause in the specification, it is not entirely clear to me why (new C).O
is not typed in exactly the same way as {val y = new C; y.O}
, because after this rewriting, the problematic code
val o: c.O.type forSome { val c: C } = (new C).O
would look just like the only working proposal that I could come up with in the above series of attempts:
val o: c.O.type forSome { val c: C } = { val c = new C; c.O }
Could the inferred type C#O.type
actually be a bug, and c.O.type forSome { val c: C }
be the type mandated by the specification? I wouldn't go so far claiming that it is a bug, I'm not familiar enough with the formulations used in the spec.
O
is different for each instance ofC
, so why should it beobject
in the first place? – RenieO
for eachC
. – Orenclass O
andclass C { final val o: O = new O }
? There's exactly oneo
for eachC
, andfinal val
will guarantee that derived classes can't modify this field. – Renieval x: c.O.type forSome { val c: C } = { val c = new C; c.O }
works, becausec
is stable, whereas(new C)
is not. Wouldn't this be at least a temporary workaround? – Maxama{ val c = new C; c.O }
and(new C).O
are not equivalently. Added an edit to my answer with more details and links. – Maxama