Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Base(child) class object upcast #16426

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

Conversation

ntrel
Copy link
Contributor

@ntrel ntrel commented Apr 29, 2024

FundamentalType(arg) works, e.g. int(2).

Enum(Enum.foo) works, which does nothing - see compilable/b16346.d below and/or https://issues.dlang.org/show_bug.cgi?id=16346.

Let's allow class instance upcasting without having to use cast - Base(child). cast can be bug-prone, and upcasting is always fine. In generic code it's not always clear what a type resolves to, so avoiding cast makes code reviews easier.

Here upcasting takes precedence over a static opCall, but that could be swapped.

`FundamentalType(arg)` works, e.g. `int(2)`.

`Enum(Enum.foo)` works, which does nothing - see:
https://issues.dlang.org/show_bug.cgi?id=16346

Let's allow class instance upcasting without having to use `cast`.
`cast` can be bug-prone, and upcasting is always fine.

Here upcasting takes precedence over a static opCall.
@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @ntrel! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#16426"

@zopsicle
Copy link
Contributor

Should I(c) where I is an interface that typeof(c) implements be supported as well?

@atilaneves
Copy link
Contributor

Base b = child; already works.

@ntrel
Copy link
Contributor Author

ntrel commented May 3, 2024

@atilaneves The idea is to be able to do conversion as an expression, without a cast. Requiring a declaration breaks the flow of an expression, and pollutes the scope when the declaration is only used once.
Uniform scalar type construction was added, yet it could be argued to just use a declaration for that instead.

@atilaneves
Copy link
Contributor

Which expressions would be common enough to warrant the enhancement?

@PetarKirov PetarKirov closed this May 8, 2024
@PetarKirov PetarKirov reopened this May 8, 2024
@PetarKirov
Copy link
Member

Which expressions would be common enough to warrant the enhancement?

T(val) in generic code.

@atilaneves
Copy link
Contributor

Which expressions would be common enough to warrant the enhancement?

T(val) in generic code.

I still don't understand when/why/how this would be used/useful.

@zopsicle
Copy link
Contributor

zopsicle commented May 8, 2024

Requiring a declaration breaks the flow of an expression, and pollutes the scope when the declaration is only used once.

This can be a simple library function:

T identity(T)(T value) =>
    value;

Then, use without template argument deduction:

identity!Base(derived)

It is even usable with UFCS:

f.identity!Base.g;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants