Draft for a proposal for a Pure Function API

Based on the adoption of one of the two following possible future proposal (depending of the community interest for my previous ideas) :

  • proposal_explicit_reference_syntax
  • proposal_explicit_ownership_syntax

This API provides a way to check if a function is pure and thus allow us to fearless compose them or paralellize them in thread pools. This also assure a way to make future high level API that can use secure and context free function that can run beside main script.

This API is based on at least one of the following proposals :

API

interface Function {
    isPure(func: Function): boolean
}

isPure return true if the passed function only use arguments that respect (or use 0 arguments):

  • "@arg" (read only reference)
  • "copy arg" (deep copied argument)
  • "move arg" (scope moved argument)

And all function used in current function body return true for Function.isPure()

Examples

No arguments

function doNothing() {
    return
}

console.assert(Function.isPure(doNothing) === true)

Read-only reference arguments

function compute(@matrixA, @matrixB) {
    const result = Matrix.add(@matrixA, @matrixB)
    console.log(result)
    return result
}

console.assert(Function.isPure(compute) === false)

Explicit onwnership described arguments

async function extractUser(copy username) {
    const datas = await fetch('api/username')
    return await datas.json()?.user
}

console.assert(Function.isPure(extractUser) === false)

const datas = await fetch('api/username')
async function extractUserPure(move datas) {
    return await datas.json()?.user
}

console.assert(Function.isPure(extractUserPure) === true)

Arguments mix

function foo(@arg0, copy arg1, move arg2) {
    return result
}

console.assert(Function.isPure(foo) === true)

function buzz(@arg0, copy arg1, move arg2, arg3) { //arg3 is no referenced of ownerhip described argument
    return result
}

console.assert(Function.isPure(foo) === false)

FAQs

  • Throw an expection can be a criteria of non-pure function in order to assure the constant execution flow.

Proposal: https://github.com/JOTSR/proposal_pure_function_api`