Skip to content

Commit 7760a15

Browse files
committed
added support for higher order dispatch in multiple dispatch
1 parent 1317270 commit 7760a15

9 files changed

Lines changed: 902 additions & 248 deletions

File tree

SYNTAX.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,64 @@ When multiple implementations match, the most specific one wins:
462462
1. **HTTP Status Literal** (e.g., `404`) - specificity: 100
463463
2. **Object Type / Entity** (e.g., `UserObject`) - specificity: 90
464464
3. **Primitive Type** (e.g., `i32`) - specificity: 80
465-
4. **HTTP Status Range** (e.g., `2xx`) - specificity: 50
466-
5. **Any** (no type annotation) - specificity: 0
465+
4. **Function Type** (e.g., `func(i32) -> i32`) - specificity: 60 + parameter/return specificity
466+
5. **HTTP Status Range** (e.g., `2xx`) - specificity: 50
467+
6. **Any** (no type annotation) - specificity: 0
468+
469+
### Higher-Order Dispatch
470+
471+
W++ supports **higher-order dispatch** — functions can dispatch on the signatures of function-typed parameters using the `func(...)` type annotation syntax:
472+
473+
```wpp
474+
// Dispatch on function parameter types
475+
funcy apply(fn: func(i32) -> i32, data: i32) -> i32 {
476+
print("Applying integer function")
477+
return fn(data)
478+
}
479+
480+
funcy apply(fn: func(string) -> string, data: string) -> string {
481+
print("Applying string function")
482+
return fn(data)
483+
}
484+
485+
// Helper functions
486+
func double(x: i32) -> i32 {
487+
return x * 2
488+
}
489+
490+
func uppercase(s: string) -> string {
491+
return s // placeholder
492+
}
493+
494+
// Calls dispatch to first overload based on function signature
495+
let result1 = apply(double, 5) // prints "Applying integer function", returns 10
496+
497+
// Calls dispatch to second overload
498+
let result2 = apply(uppercase, "hello") // prints "Applying string function"
499+
```
500+
501+
#### Function Type Syntax
502+
503+
Function types use the syntax: `func(ParamType1, ParamType2, ...) -> ReturnType`
504+
505+
- **Parameters**: Comma-separated list of parameter types
506+
- **Return Type**: Optional, specified with `-> Type` (defaults to `i32` if omitted)
507+
- **Empty Parameters**: Use `func() -> RetType` for functions with no parameters
508+
509+
Examples:
510+
```wpp
511+
func(i32) -> i32 // Single parameter, returns i32
512+
func(i32, string) -> bool // Two parameters, returns bool
513+
func() -> i32 // No parameters, returns i32
514+
func(i32, i32) // Two parameters, returns i32 (default)
515+
```
516+
517+
#### Function Type Specificity
518+
519+
Function types have a base specificity of 60, plus additional specificity based on their parameter and return types. This means:
520+
- A function type is more specific than HTTP status ranges
521+
- A function type is less specific than primitive types when used alone
522+
- The total specificity increases with more specific parameter and return types
467523

468524
---
469525

0 commit comments

Comments
 (0)