manifold-systems / manifold

Manifold is a Java compiler plugin, its features include Metaprogramming, Properties, Extension Methods, Operator Overloading, Templates, a Preprocessor, and more.
http://manifold.systems/
Apache License 2.0
2.43k stars 125 forks source link

Suggestion: Class Aliases #374

Open rixy87 opened 2 years ago

rixy87 commented 2 years ago

Concept - class aliases.

  1. Aliases for final classes are allowed
  2. Aliases can't affect base classes
  3. Alias types behave like different classes when you write code
  4. Alias types behave like base type in runtime
  5. Alias types still can have additional code (may be working like extensions do now)
  6. Any conversions between aliases of the same class should be explicit A a = new B() -> error and for example A a = new B().convertTo(A.class)

Here is the most simple and obvious use case:

method(String a, String b, String c, String d, String e, .....) It's very easy to make a mistake in parameters order - and all will go totally wrong.

method(A a,B b, C c, D d, E e, .....) A bit more type safety - and you have show ball chance to break this call. Also code completion will be far more precise.

Of cause that could be solved by wrappers:

class A {
  String aValue;
}

But that decreases performance. And this is just first step of the dance. Others are to teach all (de)serializers to handle them (json, orm, mapstruct to DTOs like protobuf generated classes, ...) - that seems to be quite painful, or can reduce performance even more when using class Wrapper as base class for all wrappers and using reflection to make things working.

Class aliases seem to be a perfect solution:

class A alias of String {}

or

@AliasOf(String.class)
class A {}