Skip to content

types_as_comments_proposal

Matěj Štágl edited this page Jun 19, 2022 · 5 revisions

Types as comments proposal

Ambiguity

This proposal conflicts with label declarations for goto.

myLabel: 
something = 0

This would be incorrectly considered as something being a type of a variable myLabel.

Hence we propose a change to label declarations. Labels would be declared as:

label myLabel:
something = 0

label keyword has to be prepended before label ident. goto is rarely used in modern high level programming and hence the overall impact of syntax change should be minor.

Specification

type as used in this proposal is defined as:

TypeBegin = ":", TypeExpr
TypeExpr = LiteralExpr, [GenericTypeExpr], ["?" | "!"];
GenericTypeExpr = "<", {TypeExpr, [","]}, ">";

Some examples of types:

: number
: List<number>
: Tuple<number, string>
: string?
: object!
: List<Tuple<table, table>>

? indicates that the value might be nil, while ! indicates that the value won't be nil.

local x : number = 10

type can be appended at the end of assignment LHS.

function foo(paramA : string, paramB : number) : number {

}

baz = () : number => {

} 

types can be appended after parameter declaration and after closing ) in the function declaration.

typedef User {
  Id: number, // property
  GetFriends: function (howMany : number) : List<Friend> 
  Filter: function (callback : function (item : User)) : List<User>
}

typedef is used to declare types. Typedefs are treated as comments by the compiler.

Types can be used in enums, classes and mixins.

class C {
 P : number = 10
}

mixin M {
  N : string {
     return "str"
  }
}

enum E : number { // all members will be of type "number" by default
 Red,
 Green = 5,
 Blue : string = "blue" // but Blue will be string
}
Clone this wiki locally