Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance javaslang.match.Match by caze(Pattern p, Function<Object, Boolean> o) #8

Closed
danieldietrich opened this issue Aug 20, 2014 · 2 comments
Assignees
Labels

Comments

@danieldietrich
Copy link
Member

Invent a Pattern interface which adds more mature pattern matching capabilities to Match API.
Focus lies on object decomposition using type and value information. Wildcards would be great.

Brainstorming:

// class to match by pattern
class A {
  String val1;
  int val2;
  boolean irrelevantForTheFollowingMatch;
}

// tagging interface
interface Pattern {
}

// example: Proxy creation to get a View of A
interface APattern<A> extends Pattern {
  String val1(A obj); // matches an A with any val1
  default int val2(A obj) { return 2; } // matches an A with val2 == 2
}

// caze will create a Proxy for p of type APattern
// which also gives access to the runtime object of type A
// and the attributes defined in
Matchs.caze(APattern p, (A obj) -> a.val1)

Additionally, it would be nice to provide a programatic way to create Patterns instead of an interface declaration, e.g. via Annotation, Builder, ...

The constraint is, as always for Javaslang, to use only JVM (+ Javaslang) classes and no 3rd party libs.

@danieldietrich
Copy link
Member Author

Pattern Matching with Java 8

Motivation

In Scala it is possible to decompose and match complex objects:

val option = Option("hi")
option match {
   case Some(a : String) => "str value: " + a
   case None => "no value"
}

How cool would it be, if something similar were possible with Java!

Approach

  1. Decompose object
  2. Match decomposition result
  3. Provide decomposition result

Decomposition could take place in two flavors:

1) Using Tuples

@FunctionalInterface
interface Decomposition<T, R extends Tuple> {
   R unapply(T obj);
}

Example:

class A {
   String val1;
   int val2;
}

final Decomposition<A, Tuple2> deA = a -> Tuple.of(a.val1, a.val2);

Matchs.caze(deA, Tuple.of("1", any()), t -> t._2);
Matchs.caze(deA, Tuple.of("1", any()), (Tuple2 t) -> t._2);

Further, the decomposition of objects could be simplified by native support:

interface Decomposable<T, R extends Tuple> {
   Decomposition<T, R> decomposition();
}

Example:

class A implements Decomposable<A, Tuple2> {
   String val1;
   int val2;

   @Override
   Decomposition<A, Tuple2> decomposition() {
       return a -> Tuple.of(a.val1, a.val2);
   }
}

_// would decompose any object_
Matches.caze(Tuple.of("1", any()), t -> t._2);
Matches.caze(Tuple.of("1", any()), (Tuple2 t) -> t._2);

_// would decompose objects of type A_
Matches.caze(A.class, Tuple.of("1", any()), t -> t._2);
Matches.caze(A.class, Tuple.of("1", any()), (Tuple2 t) -> t._2);

Pro: simple value object impl
Con: losing property names

Using View interface

TODO

Pro: explicite types & names
Con: overhead of defining new types

danieldietrich added a commit that referenced this issue Aug 21, 2014
danieldietrich added a commit that referenced this issue Aug 21, 2014
Added missing javadoc.
@danieldietrich danieldietrich changed the title Enhance javaslang.match.Match by caze(Pattern p, Function<Object, Boolean> o) [match] Enhance javaslang.match.Match by caze(Pattern p, Function<Object, Boolean> o) Aug 22, 2014
@danieldietrich
Copy link
Member Author

Prototypically implemented. Depends of https://bugs.eclipse.org/bugs/show_bug.cgi?id=442245.

Need to implement Pattern.of(...):

public static <T, P1, P2, ..., Pn, R1, R2, ..., Rn> Pattern<T, Tuple2<P1, P2, ..., Pn>, Tuple2<R1, R2, ..., Rn>>
    of(Decomposition<T, Tuple2<R1, R2, ..., Rn>> decomposition, Tuple2<P1, P2, ..., Pn> prototype)

for 3 <= n <= 13.

@danieldietrich danieldietrich changed the title [match] Enhance javaslang.match.Match by caze(Pattern p, Function<Object, Boolean> o) Enhance javaslang.match.Match by caze(Pattern p, Function<Object, Boolean> o) Oct 4, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant