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.

method with singleton type mismatches same singleton type

See original GitHub issue

As the below example shows, a method that takes a singleton type Int(1) cannot be applied with 1.

scala> def id(x: 1): 1 = x
id: (x: Int(1))Int(1)
scala> id(1)
-- [E007] Type Mismatch Error: <console> -----------------------------------
6 |id(1)
  |   ^
  |   found:    Int(1)
  |   required: Int(1)
  |

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
rethabcommented, Nov 27, 2016

I have analyzed this a little.

What I have noticed is that in Typer.scala:1841 where the types are compared

else if (tree.tpe <:< pt) {

it returns false when my code from the first example is run in the REPL, but it returns true if the example from @liufengyun is run (ie. w/o REPL).

Actually, in both cases both sides are equal (ConstantType(Constant(4))) however in the REPL, the two don’t refer to the same object in memory whereas otherwise they do. It seems that the TypeComparer doesn’t return true unless they point to the same object in memory.

My assumption is that the types are usually cached, but not between runs in the REPL. This is supported by the following snippet, which works in the REPL:

scala> def id(x: 4): 4 = x; id(4)
def id(x: 4.type): Int(4)

What I am unsure about is whether all types are always supposed to be cached and therefore the REPL needs to ensure that it re-uses types between runs (ie. several evaluated lines) or the TypeComparer should return true in topLevelSubType if they are the same but don’t point to the same object.

0reactions
rethabcommented, Nov 27, 2016

Thanks a lot! I’m actually mostly using the debugger to step through and try to make sense out of it 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Strange type mismatch with a macro: found - Stack Overflow
Strange type mismatch with a macro: found: singleton type with underlying type A, required: A ... Which makes no sense to me: y.type...
Read more >
Typetag of a singleton type - Google Groups
At least, it is according to the spec: "A singleton type is of the form p.type, where p is a path pointing to...
Read more >
SIP-23 - Literal-based singleton types - Scala Documentation
A singleton type is of the form p.type . Where p is a path pointing to a value which conforms to scala.AnyRef ,...
Read more >
Pattern match claims `x.type` is erased · Issue #12569 - GitHub
Spec says "A singleton type p.type. This type pattern matches only the value denoted by the path p (the eq method is used...
Read more >
Making Singleton More Effective in Java - DZone
Want to learn more about when to use the singleton design pattern in Java? ... a ClassCastException will be thrown when the 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