Kipper icon indicating copy to clipboard operation
Kipper copied to clipboard

[Feature] Implement `match` operator for matching any type to a type

Open Luna-Klatzer opened this issue 7 months ago • 0 comments

Is there an existing proposal for this?

  • [X] I have searched the existing issues

This feature does not exist in the latest version

  • [X] I am using the latest version

Proposal

Implement a match operator which matches any type against an interface. This is a simple wrapper of all the type functionality present in Kipper, which allows the user to match anything against a Kipper type.

This means the following:

  • match can compare primitives to a specific type:
    1 match num; // -> true
    "2" match str; // -> true
    true match null; // -> false
    
  • match can compare objects to interface types:
    interface X {
      x: num
    }
    
    { x: 1 } matches X; // -> true
    { y: "2" } matches X; // -> false
    
  • match can compare arrays to Array and functions to Func:
    [1, 2, 3] match Array<num>; // -> true
    
    (): void -> {} match Func<void> // -> true
    (): num -> 1 match Func<void> // -> false
    

As such, match is also not identical to typeof or instanceof. typeof returns obj for every object in the language and as such can not be used to compare interface types, while instanceof is limited to a prototype being present i.e. a class instance being compared to another class.

Exact behaviour / changes you want

  • [x] #495
  • [ ] #659
  • [ ] Implement a built-in match operator which matches any given expression to a given type specifier.
  • [ ] Implement internal functionality for the match operator.

Luna-Klatzer avatar Jul 18 '24 09:07 Luna-Klatzer