[pypy-dev] Returning a "default value" for a Rtyped function

Armin Rigo armin.rigo at gmail.com
Fri Apr 20 05:35:46 EDT 2018


Hi Timothy,

On 20 April 2018 at 01:01, Timothy Baldridge <tbaldridge at gmail.com> wrote:
> I have a function in RPython that returns a tuple, and based on different
> situations, one value or the other may "null":
>
> def decide(x):
>   if x:
>     return 42, None
>   else:
>     return 0, Object("some value")
>
>
> Here's the issue, this function is part of some meta-programming and is auto
> generated. So the issue is, when I return `0, Object("some value")` I really
> don't know what the first member of the tuple is, I want it to be some
> value, and it can be anything since the calling function is dispatching on
> the second value of the tuple being None (or not).
>
> Is there some sort of way in rpython to say "return any value that fits the
> type expected here?"
>
> Or perhaps there's some better way to do a tagged union of sorts in rpython.

RPython has got no "expected type" construction, because types are
always propagated forward, not backward.

I think indeed a better way is to use something more explicit.  You
could create a small abstract base class with two subclasses, but that
doesn't work if you want the 'int' above to be not always 'int' but
some other type of values in other contexts.  Maybe define a subclass
of Exception, and then either return a normal value (like an int) or
'raise HereIsAnObject(Object("some value"))'?


A bientôt,

Armin.


More information about the pypy-dev mailing list