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

method to apply a function upon a wildcard type #687

Open
gavinking opened this issue May 21, 2015 · 4 comments
Open

method to apply a function upon a wildcard type #687

gavinking opened this issue May 21, 2015 · 4 comments
Labels
Milestone

Comments

@gavinking
Copy link
Member

We could alleviate most of the pain involved in our lack of support for wildcard capture by supplying a couple of functions that accept a wildcarded object or objects, and a generic function, and apply the function to the object or objects. Something like this:

    R apply<R,C,B>(C<out B> target, 
            <T> given T satisfies B
                    => R(C<T>) fun)
            given C<E>
            => .... ;

    R applyToPair<R,C1,C2,B1,B2>(
            C1<out B1> target1, C2<out B2> target1, 
            <T1,T2> given T1 satisfies B1 given T2 satisfies B2 
                    => R(C1<T1>, C2<T2>) fun)
            given C1<E> given C2<E>
            => .... ;

Of course, these functions can't be implemented in Ceylon, they have to be native.

Also notice that they're pretty painful to use, since you will always need to explicitly specify all the type arguments.

@gavinking gavinking modified the milestones: 1.2, 1.3 May 21, 2015
@gavinking
Copy link
Member Author

Actually since you can nest calls inside anonymous functions, I guess you only need apply(), not applyToPair(). Good.

gavinking referenced this issue in ceylon/ceylon-model May 21, 2015
@RossTate
Copy link
Member

Uh, why not just support the simple form of capture that I proposed? It has nice error messages and everything.

@gavinking
Copy link
Member Author

@RossTate where is that proposal again?

The main reason I'm not proposing to support real wildcard capture is that in full generality the runtime side of it with reified generics looks like it will get pretty hairy. Not impossible, but not easy either.

@RossTate
Copy link
Member

It's pretty simple. You do wildcard capture but you ignore implicit constraints (i.e. you only constrain the captured type variable with the explicit given by the in or out annotation). Then you do subtyping as usual with constrained type variables. But if there's an error, then rather than giving types with the ugly capture of stuff, you give their tight approximations. That is, if sub <: super fails, then you report that upper(sub) <: lower (super) failed, where upper(sub) is the most precise supertype that doesn't contain captured variables and lower(super) is the least precise subtype that doesn't contain captured variables. To compute upper and super, you just replace capture variables with their upper or lower bound depending on whether they occur in a co or contravariant position. Except for recursive constraints, upper(sub) <: lower(super) holds/fails if and only if sub <: super holds/fails, so you're not losing any precision. The only corner case is invariant positions, for which you'd need out in use-site annotations to apply this technique.

So you get wildcard capture, but you never have to have capture of in your type errors.

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

No branches or pull requests

2 participants