Discussion:
[ceylon-dev] Generic constraints and compiler errors
Paco Soberón
2016-03-31 22:01:11 UTC
Permalink
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.
--
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 ceylon-***@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.
'Lucas Werkmeister' via ceylon-dev
2016-03-31 22:04:22 UTC
Permalink
Looks like this is https://github.com/ceylon/ceylon/issues/3641
(originally https://github.com/ceylon/ceylon-spec/issues/535)
Post by Paco Soberón
value tuple = Tuple<Integer, Boolean>(true, []); // Boolean is not
assignable to upper bound Integer
void foo(Tuple<Integer, Boolean> tuple) {}
Tuple<Integer, Boolean> bar() => nothing;
Narrowing my observation to a simpler generic class, this code doesn't
class Generic<Parent, Child>()
given Child satisfies Parent {}
value generic = Generic<Integer, Boolean>();
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.
--
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
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
<https://groups.google.com/d/msgid/ceylon-dev/CAMfU2ZQrF8FC9oYPkCNo1963Bub1wFP%3DGiqiK9ah%3Dy_Jn9joKA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.
--
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 ceylon-***@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/56FD9EE6.1030406%40googlemail.com.
For more options, visit https://groups.google.com/d/optout.
Paco Soberón
2016-03-31 22:37:24 UTC
Permalink
Interesting stuff. Thanks for answering!

2016-04-01 0:04 GMT+02:00 'Lucas Werkmeister' via ceylon-dev <
Post by 'Lucas Werkmeister' via ceylon-dev
Looks like this is https://github.com/ceylon/ceylon/issues/3641
(originally https://github.com/ceylon/ceylon-spec/issues/535)
value tuple = Tuple<Integer, Boolean>(true, []); // Boolean is not
assignable to upper bound Integer
void foo(Tuple<Integer, Boolean> tuple) {}
Tuple<Integer, Boolean> bar() => nothing;
Narrowing my observation to a simpler generic class, this code doesn't
class Generic<Parent, Child>()
given Child satisfies Parent {}
value generic = Generic<Integer, Boolean>();
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.
--
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
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?utm_medium=email&utm_source=footer>
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.
--
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
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/56FD9EE6.1030406%40googlemail.com
<https://groups.google.com/d/msgid/ceylon-dev/56FD9EE6.1030406%40googlemail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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 ceylon-***@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/CAMfU2ZTHnoxdPEpJ_xtDegpYO%3DGaaOOtd_TRMEnEhFvexjpYWQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...