Kipper icon indicating copy to clipboard operation
Kipper copied to clipboard

[Feature] Implement high-level `typeof` operator

Open lorenzholzbauer 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

Add a typeof operator that returns the runtime object instead of a string like in JS.

For example:

  • Null:
    var x: null = null;
    
    var y: type = typeof x; // -> null
    
  • Primitives:
    var x: str = "x";
    
    var y: type = typeof x; // -> str
    
  • Objects:
    interface X {
      prop: num
    }
    
    var x: X = {
      prop: 1
    };
    
    var y: type = typeof x; // -> obj  
    
  • Arrays:
    var x: Array<num> = [1, 2, 3]
    
    var y: type = typeof x; // -> Array  
    
  • Functions:
    var x: Func<num> = (): num -> {
      return 1;
    };
    
    var y: type = typeof x; // -> Func<num>
    
  • Class:
    class Duck {
      ...
    }
    
    var x = new Duck();
    var y: type = typeof x; // -> Duck
    

Exact behaviour / changes you want

  • [x] #659
  • [x] Add typeof operator which is a built-in keyword that is separate from any references and such.
  • [x] Add internal code structure required to evaluate the type of a variable at runtime.
  • [x] Add type information to functions so they can also be used with typeof.
  • [x] Add internal function typeof implementing the functionality for typeof.

lorenzholzbauer avatar Jul 16 '24 08:07 lorenzholzbauer