Feature request: broader argument refinement and multiple satisfies clause to same interface

38 views
Skip to first unread message

Michele Sollecito

unread,
Jun 23, 2016, 9:04:38 PM6/23/16
to ceylon-dev
Hi guys,

I really appreciate your efforts on Ceylon, which I find brilliant.

I particularly enjoy being able to refine a method using a narrower return type but, unfortunately, a broader argument type does not work.

The cases that are relevant in my opinion are:

- broader single type e.g., refining a method taking a Boolean with one that accepts Anything
- broader type using union e.g., refining a method taking a String with one that accepts  String | Integer
- broader type using union e.g., satisfying multiple cases of the same generic interface, with different types (class Listener12 satisfies Listener<Event1> & Listener<Event2>), and being able to accept Event1 | Event2 in the refining method (this is not the same as satisfying Listener<Event1 | Event2>, for there can be cases where a class or interface satisfies or extend an interface already, making it impossible to satisfy it again - both inside the same project or coming from another module)
- broader type using streams or iterables e.g., refining a method that takes a String with one that accepts {String+} or [String+]
- broader type using additional arguments with default e.g., refining a method that takes an Integer with one that accepts an Integer as first arguments, and additional arguments of any type, with defaults.

Please let me know if you're planning to do this, and maybe when.

Kind regards and again thumbs up for the awesome work you're doing,

Michele

Lucas Werkmeister

unread,
Jun 23, 2016, 9:07:12 PM6/23/16
to ceylo...@googlegroups.com

There’s a note in the spec about it, in §4.7.8:

Note: in a future release of the language, we would like to support contravariant refinement of method parameter types.

I’m not sure why it isn’t allowed currently… perhaps it’s difficult to implement in the backend?
--
You received this message because you are subscribed to the Google Groups "ceylon-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceylon-dev+...@googlegroups.com.
To post to this group, send email to ceylo...@googlegroups.com.
Visit this group at https://groups.google.com/group/ceylon-dev.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceylon-dev/4a9b2b32-d35a-4b75-ba5f-9449af5c7464%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Vasileff

unread,
Jun 23, 2016, 10:03:28 PM6/23/16
to ceylo...@googlegroups.com
I’d really like that too. But there are two problems I’m aware of:

1) The JVM does not allow this sort of contravariant parameter widening (I image it would conflict with Java's method overloading approach), and

2) even if #1 was worked around by using ‘Object’ everywhere in the generated bytecode, there’d still be a problem with primitive types like Integer and Boolean, since they need to be be used in their unboxed forms (i.e. Java long & boolean) for performance.

John

Gavin King

unread,
Jun 26, 2016, 4:07:27 PM6/26/16
to ceylo...@googlegroups.com
That's this issue:

https://github.com/ceylon/ceylon/issues/3673

Note that the *only* reason we don't already support this is that it's
not especially easy to implement on the JVM.
> --
> You received this message because you are subscribed to the Google Groups
> "ceylon-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ceylon-dev+...@googlegroups.com.
> To post to this group, send email to ceylo...@googlegroups.com.
> Visit this group at https://groups.google.com/group/ceylon-dev.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ceylon-dev/4a9b2b32-d35a-4b75-ba5f-9449af5c7464%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Gavin King
ga...@ceylon-lang.org
http://profiles.google.com/gavin.king
http://ceylon-lang.org
http://hibernate.org
http://seamframework.org

Lucas Werkmeister

unread,
Jun 26, 2016, 6:26:28 PM6/26/16
to ceylo...@googlegroups.com
Note that you can already do this today with callable members:
http://try.ceylon-lang.org/?gist=8e5d5f76c80764855c4b71744a3d8b22 – the
downside is that they are a bit more awkward to define and can’t be
invoked with named arguments.
Reply all
Reply to author
Forward
0 new messages