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.

Enum and equivalent class hierarchy do not generate same type signature

See original GitHub issue

Compiler version

Version 2.3.1

Minimized code

I have created two examples of an HList using this Enum:

enum Tup:
  case EmpT
  case TCons[H, T <: Tup](head: H, tail: T)

import Tup.*

and what I assume is the equivalent case class hierarchy:

sealed trait Tup
case object EmpT extends Tup
case class TCons[H, T <: Tup](head: H, tail: T) extends Tup

//import Tup.*

In both cases I use the same code:

val tup1: TCons[1, TCons[2,  EmpT.type]] = TCons(1, TCons(2,  EmpT))
val tup3 = TCons(3, TCons(3,  EmpT))

println(split(tup1))
println(split(tup3))

The full code can be found here:

Output

I expected both examples to compile and run. However in the Enum version, in the last line I get:

    cannot reduce inline match with
     scrutinee:  Playground.tup3 : (Playground.tup3 : Playground.Tup)
     patterns :  case _:Playground.Tup#EmpT.type
                 case cons @ _:Playground.Tup.TCons[_ @  >: Nothing <: Any, _ @  >: Nothing <: Any]

Expectation

I expect both versions to generate the same type inference. My IDE shows that for the Enum version, the HLIst will have the the generic type Tup. For the sealed trait version, the complete definition of HList will be retained.

I would also expect the HList to retain its value types. For example, My IDE shows, which I assume is what the compiler inferred:

   val tup2: TCons[Int, TCons[Int, EmpT.type]] = 1 +: 2 +: EmpT

in my opinion should be:

   val tup2: TCons[1, TCons[2, EmpT.type]] = 1 +: 2 +: EmpT

For the first issue this may not be a bug. If it is not, and maybe I missed it, this difference could be documented.

The second is a question. If it is warranted, I can open another issue.

TIA

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
bishaboshacommented, Dec 7, 2022

This is as documented image

Note that the type of the expressions above is always Option. Generally, the type of a enum case constructor application will be widened to the underlying enum type, unless a more specific type is expected. This is a subtle difference with respect to normal case classes. The classes making up the cases do exist, and can be unveiled, either by constructing them directly with a new, or by explicitly providing an expected type.

0reactions
bishaboshacommented, Dec 7, 2022

You can use a union type of the enum cases as an upper bound if you want precise types

Read more comments on GitHub >

github_iconTop Results From Across the Web

What would be different in Java if Enum declaration didn't ...
Well, first of all it would complain about using the raw type, but you could do: public class Enum<E extends Enum<?>> for the...
Read more >
Build Enumerations of Constants With Python's Enum
In this tutorial, you'll learn how to create and use enumerations of semantically related constants in Python. To do this, you'll use the ......
Read more >
When are enums NOT a code smell?
The client receives the DataContract and creates an object of a class in a hierarchy using the enum value to switch and create...
Read more >
Partial Classes and Methods - C# Programming Guide
Partial classes and methods in C# split the definition of a class, a struct, an interface, or a method over two or more...
Read more >
Chapter 8. Classes - Oracle Help Center
A class whose declaration lacks an implements clause has no direct superinterface types, with one exception: an anonymous class may have a superinterface...
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