Hi all,
we are working on extension to make pattern match works on modules, feedback is welcome
Examples:
let {length, cons : con /* rename*/ } = module (List)
let test i =
let {length, v } = module (List)
length i + v
It works similar to JS as below
let {length, cons} = require('list')
Use cases:
- Reexports
Imagine we reexport functions from module A, we can do it like this
let {fn1,fn2,fn3} = module(A)
- Imports
Imagine we want to import functions from module A*, we can do it like this
%%private(
let {fn1,fn2,fn3} = module (A1)
let {fn4,fn5,fn6} = module (A2)
)
Note the private
syntax is a bit ugly, but we are moving in the good direction
- Local import to replace the usage of local open
Previously user writes:
let calcuate = (x,var) => {
open List
length x + var
}
This is bad for static analysis, since it is unclear var
is from the argument or shadowed by module List
,
Now you can write:
let calculate = (x,var) => {
let { length } = List
length x + var // var is clearly from the argument now
}
Backwards compatibility:
- No new syntax is introduced, the old type error is now a valid pattern, so it is backwards compatible
Drawbacks:
- It is unclear how to import operators, this could be delayed