Syntax Proposal: Replace thin with {}
Currently, a non-capturing function pointer type is written as def(Int) thin -> Int. We propose replacing the thin keyword with an empty capture list {} (e.g. def(Int) {} -> Int).
f: def(): Generic function type (allows both closures and function pointers).f: def() {}: Explicitly disallows captures (strictly a function pointer).
This change:
- Matches the syntax of a closure definition that captures nothing (analogous to a function pointer, as non-capturing closures implicitly coerce into them).
- Removes the need for a separate
thinkeyword.
def main():
def add_1(n: Int) {} -> Int:
return n + 1
print(take_fn_ptr(add_1))
# current
def take_fn_ptr(f: def(Int) thin -> Int) -> Int:
return f(42)
# proposed
def take_fn_ptr(f: def(Int) {} -> Int) -> Int:
return f(42)
def complex_example():
# current
def return_fn_ptr(f: def(Int) thin -> Int) {} -> def(Int) thin -> Int:
return f
# proposed
def return_fn_ptr(f: def(Int) {} -> Int) {} -> def(Int) {} -> Int:
return f
Open Questions:
- Is there a practical use case for explicitly requiring a closure and rejecting a thin function pointer (e.g. using a syntax like
f: def() {_}orf: def() {...})? - Currently, it is bit confusing that
def(Int) thin -> Intacts as a direct function type, whiledef(Int) -> Intacts as a meta function type requiring explicit parameterization (orSome[]). Instead of this split, coulddef(Int) -> Intauto-parameterize on the closure type?
# current: thin function type
def take_fn(f: def(Int) thin -> Int) -> Int:
return f(42)
# current: generic - explicit parameterization required
def take_fn[F: def(Int) -> Int](f: F) -> Int:
return f(42)
# current: Some[] alternative
def take_fn(f: Some[def(Int) -> Int]) -> Int:
return f(42)
# proposed: explicitly strict function pointer
def take_fn(f: def(Int) {} -> Int) -> Int:
return f(42)
# proposed: explicit parameterizes on the closure type
def take_fn(f: def(Int) {_} -> Int) -> Int:
return f(42)
# proposed: auto-parameterizes on the closure type
def take_fn(f: def(Int) -> Int) -> Int:
return f(42)