Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Scala have syntax for projection of a nested singleton type?

Tags:

scala

class C {
  object O
}

val x: C#O.type = (new C).O // error: ';' expected but '.' found
val y: C#(O.type) = (new C).O // error: identifier expected but '(' found

Is there a legal syntax for this type?

EDIT:

val x: c.O.type forSome { val c: C } = (new C).O

gives

ScalaFiddle.scala:4: error: type mismatch;
 found   : ScalaFiddle.this.C#O.type
 required: c.type#O.type forSome { type c.type <: ScalaFiddle.this.C with scala.this.Singleton{} }
  val x: c.O.type forSome { val c: C } = (new C).O
                                                 ^

So Scala compiler understands this type and shows it as C#O.type. It seems this case was just overlooked when creating the grammar.

like image 936
Alexey Romanov Avatar asked Apr 06 '18 07:04

Alexey Romanov


1 Answers

Overview

  1. A few attempts to write down the type
  2. A bash script that lists every attempt together with a somewhat cleaned up error message
  3. 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.

like image 196
Andrey Tyukin Avatar answered Sep 23 '22 03:09

Andrey Tyukin