What If a Function Definition Looked Like Its Type?
A Seseragi function can be defined like this: fn add a : Int -> b : Int -> Int = a + b At first, the obvious visual differences are the missing parameter-list parentheses and commas. But after using this syntax, those stopped being the part I liked most. Remove the parameter names: Int -> Int -> Int The function type is still sitting there almost untouched. When I look at the function definition,…
The Seseragi function syntax presents an intriguing concept: defining a function in a way that mirrors its type definition. At first glance, the syntax may seem unconventional due to the absence of parameter-list parentheses and commas. However, after adapting to this syntax, those aspects ceased to be the most appealing feature.
One of the most intriguing aspects of Seseragi's fn syntax is that the function's type is directly visible within the function definition. For example, the function definition `fn add a : Int - b : Int - Int = a + b` not only defines the function but also displays its type, which is `Int - Int - Int`. This feature allows the reader to understand the function's type in a more intuitive manner.
The parameter names are placed on the function's input side of the type, making it easier to visualize the function's type structure. The function type, `Int - Int - Int`, is presented before the parameter names, `a : Int - b : Int - Int`, giving a clearer picture of the function's input and output types.
This design choice emphasizes the relationship between the function definition and its type, making it less like defining two parameters followed by a return type, and more like describing the function type `Int - Int - Int` and then providing names for the inputs, `a` and `b`. The function type is conceptually placed first, and the definition merely adds names for the inputs without altering the overall shape significantly.
Underlying this syntax is ordinary currying. Seseragi functions are curried, meaning that the function type `Int - Int - Int` can be interpreted as `Int - (Int - Int)`. The first input leads to a new function of type `Int - Int`, and applying the second input to this nested function yields the final result, an `Int`. The function application follows the same structure, with each argument advancing through one arrow in the function type.
This continuous function type, `Int - Int - Int`, is not merely a matter of reducing punctuation; it reveals a deeper understanding of the function type. For example, comparing `Int - Int - Int` to the TypeScript syntax `function add(a: number, b: number): number { return a + b }` or Rust's `fn add(a: i32, b: i32) -> i32 { a + b }`, the parentheses around parameter groups in the latter languages are more like a cohesive unit.
However, Seseragi's type `Int - Int - Int` suggests a different, more nuanced relationship between the inputs and outputs.
In Seseragi, parentheses around tuples, such as `(Int, Int) - Int`, convey a distinct meaning compared to the function type `Int - Int - Int`. The parentheses signify a tuple value, `(Int, Int)`, rather than a grouping of independent parameters. This distinction highlights the importance of the current fn syntax in Seseragi, as it brings the shape of the function type into the definition.
While removing parentheses may not be the primary goal, it does result in a more visually appealing and type-accurate representation. For instance, omitting the return type, as in `fn add a : Int - b : Int = a + b`, would leave the visible function type incomplete. The compiler could potentially infer the return type, but explicitly declaring it maintains clarity for both the human reader and the compiler.
In conclusion, Seseragi's fn syntax offers an innovative way of defining functions that align closely with their type definitions. This approach enhances readability and comprehension by making the function type more visible and emphasizing the function's curried nature. Although the removal of parentheses may seem like a mere aesthetic choice, it ultimately contributes to a clearer understanding of the function's type and behavior.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.