4 ms·
Re "data classes" example in this article - Common Lisp features I miss most in Java (besides macros): - multiple return values - basically doing what the auth
by TeMPOraL 9y ago
Re "data classes" example in this article - Common Lisp features I miss most in Java (besides macros):
- multiple return values - basically doing what the author is doing here, without introducing a new class for it,
- destructuring - assigning pieces of composite object into several variables (see e.g. destructuring-bind, or what pattern matching does in many languages).
Basically, I often find myself wanting to write this:
return {listWithResults, someBooleanFlag};
or
return {listWithOneGroup, listWithOtherGroup};
and then use it like this:
List<Foo> matchingResults;
List<Foo> nonMatchingResults;
boolean flag;
{matchingResults, nonMatchingResults, flag} = functionReturningMultipleValues();
or:
Foo first;
Foo third;
List<Foo> rest;
{first, _, third | rest} = someList;
etc.
- andrioni 9y agoIf you want to stay in the JVM, Scala supports some pattern matching, including your examples: def functionReturningMultipleValues() = (List(1), List(2), false) val (l1, l2, b) = functionReturningMultipleValues evaluates to: l1: List[Int] = List(1) l2: List[Int] = List(2) b: Boolean = false and val first :: _ :: third :: rest = List(1, 2, 3, 4, 5, 6) to first: Int = 1 third: Int = 3 rest: List[Int] = List(4, 5, 6) Of course, if you already are familiar with Common Lisp, Clojure might be a better choice.