CREATE CAST disallows creating a binary-coercible cast to a domain
(because that would bypass checking the domain constraints). But it
allows it if the domain is wrapped inside a range type:
CREATE DOMAIN mydomain AS int4 CHECK (VALUE > 0);
CREATE CAST (int4 AS mydomain) WITHOUT FUNCTION; -- error (ok)
CREATE TYPE mydomainrange AS range (subtype=mydomain);
CREATE CAST (int4range AS mydomainrange) WITHOUT FUNCTION; -- FIXME
SELECT int4range(-5,-4)::mydomainrange; -- this succeeds
This particular case seems straightforward to fix, but maybe there are
also cases with more nesting to consider.
(I just found this while exploring other range-over-domain issues in
some in-progress work.)