Skip to content

PartialFunction doesn't compose well #9968

@scabug

Description

@scabug

I'd like to be able to say

{ case Good(whatever) => whatever }.andThen { case Even(num) => println("it's even!") }

But if I use this, although it makes a PartialFunction, it doesn't do what I want it to. In particular, it will tell me that it's defined at Good(3). This is because it inherits andThen from Function, and so it takes a Function as its argument.

I think overloading andThen to take a PartialFunction would be pretty disruptive, so I propose two better ways forward:

A. provide a compose method on the PartialFunction object (somewhat confusing because there is also a compose method on the PartialFunction instance, but its behavior is not what we want for composing two PFs)
B. change andThen to inspect the other function, and if it's a PartialFunction, upgrade its behavior. (this is a thrilling runtime change, and makes its behavior inconsistent).

What do you think?

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions