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.

Allow types with the same name at different kinds.

See original GitHub issue

I’d like Scala to allow abstract and alias types with the same name but different kinds. E.g.

class C {
  type This
}
class D[A] {
  type This[X]
}
object O extends C with D {
   type This = o.type
   type This[X] = o.type
}

Benefits:

  • It gives us a way to address #2887, because ill-kinded types in that issue would translate to overloaded types, so no conflicting kinds can arise.
  • It could avoid namespace pollution in high-level encodings such as the encodings of type classes that we study.

The proposal would be to distinguish same-named types by their kind class, where the kind class in this case only talks about arities, but not about bounds and variances. So

type T
type T[_]
type T[_, _]
type T[_[_]]

are all in different kind classes (and therefore can be defined in the same scope), but

type T[X]
type T[+X]
type T[X <: AnyRef]

are all in the same kind class, so defining them all in the same scope gives double definition errors. The reason for this distinction is that there is no subtype relation between types in different kind classes, so we can use the kind class as a partition criterion.

One tempting generalization of this scheme is to extend it to traits and classes. E.g. where we write Function1, …, Function22 now, maybe we should allow instead:

trait Function[-T1, R] { ... }
trait Function[-T1, -T2, R] { ... }

and so on. But that one is much harder to define and implement. First, what about the companion object? Of which trait is object Function { ... } the companion? Second, what about binary names? What are the names of the Java interfaces that these Function traits map to? So for the moment, this generalization is not considered as part of this issue.

Besides, one can get a long way already with overloaded aliases. I.e. keep the definitions of the traits Function1, …, Function22 as they are now, but add aliases:

type Function[-T1, R] = Function1[T1, R]
type Function[-T1, -T2, R] = Function2[T1, T2, R]

and so on.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
sjrdcommented, Mar 21, 2018

Aren’t you constantly saying that overloading is a pain to deal with in a compiler? And now you want to add type overloading?

Also,

[…] But that one is much harder to define and implement. First, what about the companion object?

With opaque type aliases, types are about to gain companion objects too. What then?

0reactions
smartercommented, Jan 26, 2019

Closing as I think this proposal has been abandoned, but feel free to reopen if there’s still interest.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can two Java methods have same name with different return ...
In Java (at least) two method can have the same name and different return types provided that they also have different argument types;...
Read more >
Types with same name but different type parameters #3478
I was wondering: Are there any plans to support multiple types with the same name, but with a different number of type parameters, ......
Read more >
Fixing modules that contain a type with the same name
Prevent modules from containing a type with the same name. Allow modules to be imported under different names (`import BTree as BTreeModule`, ...
Read more >
Naming a Package (The Java™ Tutorials > Learning the Java ...
awt package. Still, the compiler allows both classes to have the same name if they are in different packages. The fully qualified name...
Read more >
9. Classes — Python 3.11.1 documentation
Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances...
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