Skip to content

v0.10.0

Choose a tag to compare

@github-actions github-actions released this 18 Oct 00:07
· 60 commits to main since this release
e5a01a2

Pyfun 0.10.0

async { } now behaves like F#'s async: nothing runs until it is awaited, and every await is a
fresh run. Native builds are checked against plain runs on every change, and the compiler answers
more Python habits with the Pyfun spelling. One change breaks Python code that runs a Pyfun async
value directly, so this is a minor release.

Breaking

  • An Async value is cold. Building one runs nothing, and every await is a fresh run, as in
    F#'s async:

    let roll =
      async {
        print "rolling"
        return 6
      }
    
    let twice =
      async {
        let! a = roll
        let! b = roll
        return a + b
      }
    

    twice prints rolling two times and returns 12. Before, the second let! raised
    RuntimeError: cannot reuse already awaited coroutine, and Async.parallel [roll, roll] ran
    roll once.

    Nothing starts before it is awaited, and that changes one pattern. An extern over a Python call
    that starts work as soon as it is called (a call returning an asyncio Task, or a browser fetch
    promise under Pyodide) used to overlap when written like this:

    let a = fetch url1
    let b = fetch url2
    let! x = a
    let! y = b
    

    Now b starts only after a finishes, and awaiting one value twice makes the request twice.
    Nothing fails, it is just slower. To overlap requests, hand them to Async.parallel together:
    let! pages = Async.parallel [fetch url1, fetch url2].

    Python code handed a Pyfun Async now receives an
    awaitable rather than a coroutine: it can await it any number of times, or call it for a fresh
    coroutine as it would an async def. To migrate, write asyncio.run(job()) where Python ran a
    Pyfun value with asyncio.run(job) (Python 3.14 accepts either; 3.12 and 3.13 need the call).
    Async callbacks handed to Python, such as an asyncio.start_server handler, are adapted so
    Python receives real coroutines. (#177)

Fixed

  • == in a native build. A sum type the program compares with < gave false for
    Some 1 == Some 1 once mypyc compiled it, and <=/>= stopped the build. (#172)
  • A native build of an async self tail call failed to compile on Linux and crashed on Windows
    (a mypyc bug, python/mypy#22221). (#174, and #177 removes the shape altogether)
  • A multi-line string in a file saved on Windows carried a carriage return before every
    newline. A line break in a string is now \n whatever the file's line endings, as in Python.
    (#182)
  • A file named like 01-intro.pyfun builds natively. (#172)
  • pip install mypy setuptools is the install line for native builds: a fresh Python 3.12+
    venv has no setuptools, which mypyc needs. (#172)

Improved

  • Errors that answer the Python habit: def area w h = … says to write let area w h = …;
    an unbound len, str, sum and eleven other builtins name their Pyfun spelling; && and ||
    say and and or; and [1, 2] + [3] points at List.concat xs ys. (#164, #168, #175, #176)
  • A callback that misses an extern's closed arrow points at ->{e}. Handing an async { }
    handler to extern serve: (Reader -> Writer -> Async unit) -> … used to fail with "effect mismatch:
    cannot unify …" under the whole call. It now says what the handler performs, what the extern's
    arrow allows, and to write the arrow as ->{e}, rather than pasting in the handler's own
    effects, which would accept only handlers doing exactly that. (#181, found by a real
    program, #180)
  • Native builds are checked on every PR against plain runs of every lesson program and
    example. (#172)

Thanks

To @PandaHUN777 for the &&/|| and list-concatenation hints (#164, #168).

Upgrading

pip install --upgrade pyfun-lang. Python code that runs a Pyfun async value with
asyncio.run(job) should call it, asyncio.run(job()).