Skip to content

Add support for wildcard in the upper bound to improve Java interop #1386

@pusolito

Description

@pusolito

Ceylon doesn't support wildcard in the upper bound which prevents interacts with Java like the following:

// Java class that operates on java.lang.Comparable

public class A<T extends Comparable> {}
// Ceylon code to work with above Java type

import java.lang {Comparable}
import java.time {LocalDateTime}

A<T> bar<T>(A<T> t) given T satisfies Comparable<T> => t;

void test() {
    bar<LocalDateTime>(A<LocalDateTime>()); //<============= Error [Type parameter T of declaration
    bar has argument LocalDateTime not assignable to upper bound Comparable<Object> of T]
}

From @gavinking:

Whoah:

public interface ChronoLocalDateTime<D extends ChronoLocalDate>  extends Temporal, >TemporalAdjuster, Comparable<ChronoLocalDateTime<?>>

OMG!

So it looks like what you're really wanting to write is this

A<T> bar<T>(A<T> t) given T satisfies Comparable<in T> => t; 

i.e. the wildcard in the supertype of ChronoLocalDateTime is obligating you to need a wildcard in the >upper bound, which is something Ceylon simply doesn't support, though it seems Java does. (I never >noticed that before.)

Open a feature request against ceylon-spec please, so that we can investigate if we can/should allow wildcards in upper bound type constraints.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions