Generic constraints and compiler errors

27 views
Skip to first unread message

Paco Soberón

unread,
Mar 31, 2016, 6:01:12 PM3/31/16
to ceylo...@googlegroups.com
I just found that, while this code raises a compiler error, as expected:

    value tuple = Tuple<Integer, Boolean>(true, []); // Boolean is not assignable to upper bound Integer

This other code compiles without problems:

    void foo(Tuple<Integer, Boolean> tuple) {}
    Tuple<Integer, Boolean> bar() => nothing;

Narrowing my observation to a simpler generic class, this code doesn't compile:

    class Generic<Parent, Child>()
        given Child satisfies Parent {}
   
    value generic = Generic<Integer, Boolean>();

But this code does:

    void foo(Generic<Integer, Boolean> generic) {}
    Generic<Integer, Boolean> bar() => nothing;

Of course, I'm aware that even if it compiles, the code is completely useless, since you're not going to be able to create a valid object to pass to foo() or return from bar().

But I am still curious... why the compiler doesn't complain? Doesn't it check the generic constraints when declaring a type? Does it only do it when instantiating an object?

Regards.

Lucas Werkmeister

unread,
Mar 31, 2016, 6:04:25 PM3/31/16
to ceylo...@googlegroups.com
--
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/CAMfU2ZQrF8FC9oYPkCNo1963Bub1wFP%3DGiqiK9ah%3Dy_Jn9joKA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Paco Soberón

unread,
Mar 31, 2016, 6:37:25 PM3/31/16
to ceylo...@googlegroups.com
Interesting stuff. Thanks for answering!

Reply all
Reply to author
Forward
0 new messages