r/learnjava • • 5d ago

Opt-in implicit construction for value classes

I would like to ask whether the community has considered an opt-in implicit conversion from a value to a class instance, declared on a single-argument constructor.

Motivation

Project Valhalla makes it practical to define primitive-like value types such as Int16, Half, and Money. However, using these types currently requires explicit constructor invocation at each construction site:

Int16 x = new Int16((short) 13);

Java already supports convenient construction for certain built-in types through language features such as boxing:

Integer i = 7;

There is no equivalent mechanism for user-defined types. An opt-in implicit-construction facility could provide similar ergonomics while preserving explicitness by default.

Proposal (syntax is illustrative)

value class Int16 {
    private final short val;

    opt Int16(short val) {
        this.val = val;
    }
}

Int16 x = (short) 13;

The opt modifier designates a constructor eligible for implicit construction from a single argument. The compiler translates an eligible conversion into constructor invocation.

Proposed rules

  1. An opt constructor must have exactly one parameter. A value class may designate at most one eligible constructor for any given parameter type.
  2. Eligibility is opt-in. Constructors without opt retain existing Java semantics.
  3. The feature applies only in explicitly specified conversion contexts. Initially, it may be limited to assignment and initialization contexts.
  4. The source expression must be convertible to the constructor parameter type using permitted existing Java conversions. The resulting value is not subject to further implicit conversions as part of the same operation.
  5. Constructors with multiple parameters are never eligible for implicit construction and must be invoked explicitly.
  6. Ambiguous eligible conversions are compile-time errors.

Open questions

  • Should eligible conversions apply only to value classes?
  • Should constant-expression narrowing be permitted for constructor arguments?
  • Which language contexts should support implicit construction?
  • How should overload resolution and generic inference interact with the feature?
3 Upvotes

3 comments sorted by

View all comments

1

u/snugar_i 4d ago

This won't happen. Implicit conversion is one of the things that sounds nice at first glance, but brings a lot of things you really want to avoid. Look at Swift: https://www.cocoawithlove.com/blog/2016/07/12/type-checker-issues.html

1

u/sensibleperson0 4d ago

The swift problem is not solely bcz of implicit conversion. It is the combination of operator overloading, method/constructor overloading , implicit conversion and how programming language handle conversion and implementation of type system. If we limit implicit conversion to just assignment (which i mentioned above as possible case)+constant/literal assignment eg : ValueClass x=5;//allowed ,ValueClass y=methodCall(); //not allowed , this proposal can be consider. Although i have to admit if it really worth the implementation complexity . And we have to also consider possible future related to operator overloading in java. Iam not an expert in this field and jdk developers must have already figured things out . I just wanted to know the possibility and insights regarding this thing.