v2.0.0 [How Not to Be Seen]
Coconut v2 is here! This is Coconut's first ever major version release since Coconut was first published, and it comes with some breaking changes, detailed below. Care has been taken to keep v2 as backwards-compatible as possible, but Python 3.10 adding pattern-matching of its own that conflicted with some of Coconut's old pattern-matching rules meant that breaking changes were going to be inevitable to ensure full compatibility.
Additionally, v2 introduces some pretty major new features such as multidimensional array literal/concatenation syntax! Check it all out below and see Coconut's documentation for more information on all of the features listed.
Breaking changes:
- #605: Coconut pattern-matching is now fully unified with Python 3.10 pattern-matching: pattern-matching always works the same way regardless of where it is used, always uses Python 3.10 rules, but also always supports all additional Coconut pattern-matching features. Some implications of this:
- Coconut now fully supports class patterns everywhere, including in pattern-matching function definition and destructuring assignment.
- As a result,
x is int
type-checking syntax is now deprecated in favor of using theint(x)
class pattern. For cases where there is no rewriting as a class pattern,x `isinstance` int
syntax is also supported. - Pattern-matching for dictionaries has been changed so that
{"a": a}
now matches a dictionary with at least"a"
as a key rather than exactly"a"
as its only key to be consistent with Python 3.10. Coconut will warn if such syntax is detected and suggest the explicit{"a": a, **_}
or{"a": a, **{}}
syntax instead.
- #639: Coconut
data
types no longer support adding or multiplying them like tuples unless explicitly added via__add__
/__mul__
methods. - #623:
fmap
over a Mapping (e.g.dict
) now does a normal map instead of astarmap
. Thus, you'll need to dofmap$(def ((k, v)) -> (new_k, new_v))
instead offmap$((k, v) -> (new_k, new_v))
. Old behavior can also be recovered withfmap$(starmap_over_mappings=True)
. - #615:
($[])
spelling for the iterator slicing operator function has been removed in favor of.$[]
and the normal getitem operator has been added as.[]
. - #613: Precedence of the function composition operator
..
has been lowered such thatf x .. g y
is now equivalent to(f x) .. (g y)
rather than((f x) .. g) y
. - #635:
initializer
keyword argument inscan
changed toinitial
to match the function signature ofreduce
.
New language features:
- #628, #670: New multidimensional array literal and concatenation syntax! Concatenate two arrays with
[a ; a]
, write out 2D array literals as[1, 2;; 3, 4]
, with the number of semicolons denoting the axis from the end on which to concatenate. - #258, #669: New syntax for partially applying operator functions:
(.+1)
,(a+.)
,(.<|x)
, including support for custom operators as(. `plus` x)
. - #663: Better syntax for typing tuples: instead of
x: typing.Tuple[int, str]
, just:x: (int; str)
. - #633: New pattern-matching for loops, allowing for arbitrary destructuring pattern-matching in for loops.
- #622, #650: New anonymous named tuples for giving names to fields in temporary tuples. Just:
(a=1, b=2)
. - #626: New syntax for using partial application to convert a keyword argument into a positional argument. Just:
func$(kwd_arg=?)
. - #612: Combinators! Use
lift
to "lift" a function up so that its arguments become unary functions. Also: ident, flip, const, of. - #616, #620: New
all_equal
andcollectby
built-ins for working with iterables. - #124:
fmap
now supports asynchronous iterators. - #365 (thanks @ArneBachmann!): Equality checking with
=<expr>
has been deprecated in favor of==<expr>
; the former syntax will now raise an error in--strict
mode. - #648: Class patterns now support a strict
.attr=<match>
syntax that raises an error if theattr
isn't present rather than failing the match. - #641: Iterable destructuring patterns now support a substantially wider array of allowable destructuring constructs (e.g.
[1] + x + [2] + y + [3]
). - #603: New
as x
(to explicitly bind a variable) andis x
(to do an identity check) pattern-matching constructs. - #434 (thanks @ArneBachmann!): Conditional assignment using pattern-matching; infix patterns have been improved to support
a `pred` or b = <expr>
syntax that binds toa
ifpred
andb
otherwise. - #670:
jax
support infmap
and multidimensional array literal/concatenation. - #617, #647: New comma operator function
(,)
for joining arguments into a tuple and new(raise)
operator function for raising an exception. - #625: Matrix multiplication operator
@
now supported on all Python versions. - #649 (thanks @hlizard!): Zero-argument
super()
now supported on all Python versions. - #664: Coconut will now automatically backport
typing
imports intotyping_extensions
imports when necessary. - #490 (thanks @pavelbraginskiy!):
:=
assignment expressions can now be chained without parentheses. - #640: f-strings now supported in pattern-matching string patterns.
New compiler features:
- #632: Substantially improved syntax error messages with much finer-grained identification of the error location.
- #636: Coconut can now be used as a
xontrib
in thexonsh
shell to enable Coconut syntax. - #658: Coconut now supports
papermill
. - #575: Better interpreter auto-completing.
- #629 (thanks @tbsexton!): New
coconut[kernel]
installation option for installing only lightweight Jupyter kernel dependencies compared to the full support offered bycoconut[jupyter]
. - #646: Better
coconut.convenience
API.
Bugfixes:
- #638 (thanks @ivanmkc!):
coconut --watch
for watching files for changes to be recompiled fixed. - #657 (thanks @tbsexton!): Jupyter kernel compatibility with new Jupyter/IPython versions fixed.
- #616: Iterator slicing no longer exhausts the entire iterator except when strictly necessary.
- #631: Fixed pattern-matching on
numpy
arrays by registering them as Sequences. - #656 (thanks @hlizard!): Compilation on Windows should no longer convert names of compiled files to lowercase.
- #627 (thanks @fakuivan!): Support for starred function arguments before positional function arguments.
- #653 (thanks @fakuivan!): Fixed dotted names in class/data patterns.
- #655 (thanks @hlizard!): Certain complex f-strings no longer trigger internal Coconut exceptions.
- #667 (thanks @tom-a-horrocks!): Rare interpreter crashes fixed.