Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Annotation for specifying that an implementation function must be pure #9

Closed
nedtwigg opened this issue Nov 1, 2016 · 1 comment
Closed

Comments

@nedtwigg
Copy link

nedtwigg commented Nov 1, 2016

I have two questions which I couldn't find in the docs:

Let's say I have a class like this:

abstract class Strict<State> {
    State state;

    @Pure
    protected abstract String functionThatMustBePure(State state, String input);

    public String apply(String input) {
        return functionThatMustBePure(state, input);
    }
}

Can I use the @Pure annotation in this way, to enforce that subclasses implement the function in a pure way?

Along the same lines, is it possible to annotate a @FunctionalInterface argument of a function to be pure?

public static Supplier<R> memoizeResult(T input, @Pure Function<T, R> result) { ... }
@robmoffat
Copy link
Member

Hi Ned,

Your first question: Yes, that's exactly how it works. Subclasses must respect the annotations of the interface / superclass.

Second question: It's a while since I looked at it, but I don't think you can say only a single argument should be pure - when you declare a pure method, all the arguments being passed in must be pure (checked at compile time or runtime).

cheers,

Rob

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants