question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make @This work on type parameters

See original GitHub issue

Manifold version: 0.45-alpha JDK version: 11.0.2

Example of requested feature:

package utilities.extensions.java.lang.Object;

import manifold.ext.api.Extension;
import manifold.ext.api.This;

import java.util.function.Function;

@Extension
public class MyObjectExt {

    public static <T extends Object, R> R let1(@This Object thiz, Function<T, R> mapper) {
        return mapper.apply((T) thiz);
    }

    // Cannot compile, Type of thiz must be Object. **Please make this work**
    public static <T extends Object, R> R let2(@This T thiz, Function<T, R> mapper) {
        return mapper.apply((T) thiz);
    }

    public static <T extends Object, R> R let3(T thiz, Function<T, R> mapper) {
        return mapper.apply((T) thiz);
    }
    
    public static void test() {
        String str = "abc";
        String x11 = str.let1((String it) -> it.trim());     // This is ok, but I have to specify type of 'it'
        Object x12 = str.let1(it -> it.trim());            // Error, type of 'it' is deduced as Object

        String x21 = str.let2(it -> it.trim());            // Ideal state, type of 'it' should be deduced as String, if method let2 works
        
        String x31 = let3(str, it -> it.trim());        // without extension method, it's type deduction is working
    }
}

When I call method let1 with argument of some type T, it’s type info is lost in method body, so type parameter T of mapper cannot get its correct type. In my eyes extension method is just syntax sugar, so if let3 is working, so should let1.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rsmckinneycommented, Mar 8, 2019

Hi @WilliamStone.

The functionality you’re looking for is best expressed in terms of the self type. With the latest alpha release (0.47-alpha), Manifold now fully supports the self type via @Self.

Your example modified to use @Self:

@Extension
public class MyObjectExt {
    public static <R> R let(@This Object thiz, Function<@Self Object, R> mapper) {
        return mapper.apply(thiz);
    }
}

String str = "abc";
String result = str.let(s -> s.trim());

Use the self type to express the type of the invoker (aka the type of this). Use it in super classes and extension methods anywhere in a method signature or field type. It is especially useful as a means to simplify architecture that otherwise involves recursive generic types. Other applications involve extension methods and structural interfaces.

Note the accompanying v0.47-alpha IntelliJ plugin is awaiting JetBrains approval, which can take a couple of days.

0reactions
WilliamStonecommented, Mar 30, 2019

Thanks! I understand that the extension class is for Object not generic type T. I still feels a little inconvenience that if the actuaI type is some class extending Object, I cannot refer to its type by defining a generic type T extending Object, instead I have to regard it as Object itself. No big difference till now, and I’ll try to get to it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type parameters - Java Programming - Mooc.fi
You can create classes of your own that make use of generic type parameters. Since we began using lists, we have given data...
Read more >
Documentation - Generics - TypeScript
Just as with interface, putting the type parameter on the class itself lets us make sure all of the properties of the class...
Read more >
Generic Type Parameters - C# Programming Guide
In a generic type or method definition, a type parameter is a placeholder for a specific type that a client specifies when they...
Read more >
How to use type parameters for generic programming - JetBrains
By using type parameters, you can write functions that handle incoming parameters without depending on the type specified in the function ...
Read more >
Generic Methods - Java™ Tutorials
isn't going to work either. Recall that you cannot just ... It will generally infer the most specific type argument that will make...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found