Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

method to apply a function upon a wildcard type #5565

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

method to apply a function upon a wildcard type #5565

CeylonMigrationBot opened this issue May 21, 2015 · 4 comments

Comments

@CeylonMigrationBot
Copy link

[@gavinking] 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.

[Migrated from ceylon/ceylon.language#687]

@CeylonMigrationBot
Copy link
Author

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

@CeylonMigrationBot
Copy link
Author

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

@CeylonMigrationBot
Copy link
Author

[@gavinking]
@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.

@CeylonMigrationBot
Copy link
Author

[@RossTate] 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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant