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

Extension methods with @Self doesn't work sometimes. #490

Open
pangwa opened this issue Sep 8, 2023 · 3 comments
Open

Extension methods with @Self doesn't work sometimes. #490

pangwa opened this issue Sep 8, 2023 · 3 comments

Comments

@pangwa
Copy link

pangwa commented Sep 8, 2023

Describe the bug
I wrote an extension class which returns the ApiResult<T> which T was supposed to be the type of the caller. I wrote the extension method exactly as below.

@Extension
public class ObjectExtension {
  public static ApiResult<@Self Object> toApiResult(@This Object thiz) {
    return ApiResult.success(thiz);
  }

It works in most cases, but in some files, the code can't be compiled.

 public ApiResult<User> getUser() {
   User u = ...;
    return u.toApiResult();
  }

The same code could be compiled in some files, but in a little files, it can't, I don't know why.

To Reproduce
Implement an object extension as above.

Expected behavior
Code could be compiled.

Screenshots
image

Desktop (please complete the following information):

  • OS Type & Version: Windows 11
  • Java/JDK version: jdk 17
  • IDE version (IntelliJ IDEA or Android Studio): IntelliJ IDEA
  • Manifold version: 2023.1.19
  • Manifold IntelliJ plugin version: 2023.1.19
@rsmckinney
Copy link
Member

Hi @pangwa. Can you provide the compile error message or stack trace? Also the class where it fails? That would help me diagnose this issue. Thanks :)

@pangwa
Copy link
Author

pangwa commented Sep 11, 2023

below is the error message:
java\ai\myproject\controllers\UserController.java:52:25 java: 不兼容的类型: ai.myproject.dto.ApiResult<@manifold.ext.rt.api.Self java.lang.Object>无法转换为ai.myproject.dto.ApiResult<ai.myproject.model.User> java\ai\myproject\controllers\UserController.java java: 某些输入文件使用了未经检查或不安全的操作。 C:\work\ai-platform\src\main\java\ai\myproject\controllers\UserController.java java: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。

It happens in several places , such as spring controller/services(annotated with @RestController or @service).

@superxiao
Copy link

superxiao commented Oct 1, 2024

@rsmckinney

Hey Scott, I created an example project to demo this issue.

@Extension
public class ObjectExt {
    public static Optional<@Self Object> asOpt(@This Object it) {
        return Optional.ofNullable(it);
    }
}

then

Optional<String> optionalString = "Hello, World!".asOpt();

This extension works in our production project using jdk 17, but stops to compile in jdk 21. In the example project, it does not work even in jdk 17. So it does seem flaky even in the same jdk version.

*/src/main/java/com/example/demo/DemoApplication.java:12: error: incompatible types: Optional<@manifold.ext.rt.api.Self Object> cannot be converted to Optional<String>
                return "Hello, World!".asOpt();

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

No branches or pull requests

3 participants