google / google/j2objc

New Adventures in Obj-C Lightweight Generics...

Open
#2,096 15 comments 0 reactions 1 assignee Claimed by @litstrong View on GitHub
Dominant language
Java
Stars
6k
Forks
999
Avg merge
19h 20m
Merged PRs (30d)
14

Description

Following on from #1947

I can now see that the converted source headers have generic information where applicable, e.g. a

```private Monkey[] monkeys```

field in the java source becomes:

```IOSObjectArray monkeys```

which is great!

The problem is that non-Collection classes are also being translated with generics... or perhaps the problem is that their generics are not being stripped?

I'm seeing two types of error when compiling our Obj-C library project with the `--objc-generics` flag enabled:

```Type arguments cannot be applied to non-parameterized class 'Foo'```

For example, we have a 'Resource' class, defined in Java as:

```public class Resource```

with a constructor which takes a `Class` argument:

```public Resource(String key, Request request, Class target) {```

In the generated source, i'm seeing errors around this, e.g.:

```Type arguments cannot be applied to non-parameterized class 'IOSClass'``` from this line:

```- (instancetype __nonnull)initWithASWRequest:(ASWRequest *)request withIOSClass:(IOSClass *)target;```

Another issue shows up in a method on a class that uses the Resource class, defined in the java source as:

```
public void putResource(final String key, final Resource resource) {
resourcesMap.put(key, resource);
}
```

the translation looks like:

```
- (void)putResourceWithNSString:(NSString *)key
withASWPresentersResource:(ASWPresentersResource *)resource;
```

which gives:

```Type arguments cannot be applied to non-parameterized class 'ASWPresentersResource'```

I'm not totally sure what is wrong here, I think some things are being mistranslated, and the `` references are being copied as-is, maybe?

As I understand it, in Obj-C, you're free to parameterise any class, whereas Swift will ignore any lightweight generics other than those on collection classes. Which is fine, but the project in question is Obj-C, not Swift (our app is Swift, but uses the translated code in the form of a static library).

I hope that makes some sort of sense. Please let me know if i can be of any assistance. I could possibly try to put together an example project that shows the problem.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.