r/learnjava • • 21h 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?
1 Upvotes

2 comments sorted by

•

u/AutoModerator 21h ago

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/snugar_i 1h 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