question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Singleton bound and type projections don't play nice with each other

See original GitHub issue

If a value tasty has abstract type T and T is marked as being a singleton type (that is, currently, T <: Singleton), I’d expect tasty.type = T, because both types are singleton that contain tasty. This currently fails in Scalac and Dotty (the Scala report is scala/bug#10905 — this is a copy, with Scalac output swapped with Dotty output).

A minimized example:

scala> trait A { type T; val prod: T; val cons: T => Int = _ => 0 }; trait Foo { type U <: Singleton with A; val tasty: U }
// defined trait A
// defined trait Foo

scala> val v: A = new A { type T = Int; val prod: T = 1}; val u: Foo = new Foo { type U = v.type; val tasty: U = v }
val v: A = anon$1@5416f8db
val u: Foo = anon$2@64e1377c

scala> u.tasty.cons(u.tasty.prod: u.U#T)
1 |u.tasty.cons(u.tasty.prod: u.U#T)
  |             ^^^^^^^^^^^^^^^^^^^
  |             found:    u.U#T
  |             required: u.tasty.T
  |

/cc @milessabin

Background

I ran into this while prototyping a heavily path-dependent API in Dotty (an early version of Tasty reflection), in relation to https://github.com/lampepfl/dotty/pull/4577/files/b315dbfa9f2d206cc26da75779388fca98faaabd#r191016341; the workaround involved turning defs into vals, which I suspect is acceptable here but potentially annoying when trying to save memory.

Alternatives

For the example above, enabling the definition of idempotent methods might be more general, if we ever get there.

Soundness

Not formally investigated, since nobody yet added to DOT either singleton types, type projections or singleton type variables.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
milessabincommented, May 31, 2018

Is there a clear statement of the intended semantics of type projections, in Dotty or in DOT?

My intuitions in Scala were always that T#U ought to mean something like t.U forAll { val t: T } if there were such a thing as a forAll construct. But the implementation in Scala never aligned with that.

1reaction
Blaisorbladecommented, Sep 13, 2018

https://github.com/scalaz/scalaz-plugin/issues/17 requests a similar feature and points out problems with null, since it inhabits singleton types — which it ideally shouldn’t? If tasty: X and X <: Singleton but tasty = null, tasty.type isn’t X. That’s annoying.

Since we plan to forbid x.A for x = null if we get null tracking, we could think about (1) making singletons non-nullable (yay!), which would fix the issue; (2) forbidding x.type for x null (not needed for this issue, but seems highly questionable and a way to get back in x.A for x null).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Singleton bound and type projections don't play nice with each other ...
If a value member tasty has abstract type T and T is marked as being a singleton type (that is, currently, T <:...
Read more >
Singleton subtyping, and can we mark ... - Scala Contributors
I think the fundamental issue is that Singleton is not an appropriate kind of type to be considered an upper bound, as described...
Read more >
Singleton bound - Wikipedia
In coding theory, the Singleton bound, named after Richard Collom Singleton, is a relatively crude upper bound on the size of an arbitrary...
Read more >
1 The Singleton Bound 2 Reed Solomon codes - People
Singleton bound and Maximum Distance Separable (MDS) codes. ... Proof Let be the q-ary alphabet of C. Consider the projection map : n...
Read more >
design patterns - So Singletons are bad, then what?
Most apps are only designed to work with one configuration at a time, ... The Singleton design pattern is a very specific type...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found