diff --git a/src/lexer/highlighter.ts b/src/lexer/highlighter.ts index 5071b22..064a2d2 100644 --- a/src/lexer/highlighter.ts +++ b/src/lexer/highlighter.ts @@ -86,7 +86,11 @@ export async function native_highlighter(code: string): Promise { } function translate_token_type(tt: TokenType, value: string): string { - const keywords = ["throws", "extends", "constructor", "case", "static", "const", "enum", "union", "loop", "use", "break", "catch", "continue", "as", "do", "else", "finally", "for", "fun", "if", "in", "fn", "nil", "return", "throw", "try", "while", "type", "match", "with", "of", "abstract", "class", "interface", "private", "pub", "override", "open", "init", "val", "var", "mut", "clone"]; + const keywords = ["throws", "extends", "constructor", "case", "static", "const", + "enum", "union", "loop", "use", "break", "catch", "continue", "as", "do", + "else", "finally", "for", "fun", "if", "in", "fn", "nil", "return", "throw", + "try", "while", "type", "match", "with", "of", "abstract", "class", "interface", + "private", "protected", "pub", "override", "open", "init", "val", "var", "mut", "clone"]; switch (tt) { case "Datatype": diff --git a/src/pages/learn/classes/abstract.mdx b/src/pages/learn/classes/abstract.mdx index 71104ae..a467228 100644 --- a/src/pages/learn/classes/abstract.mdx +++ b/src/pages/learn/classes/abstract.mdx @@ -5,3 +5,5 @@ title: Abstract import Code from "../../../components/Code.astro" # Abstract + + diff --git a/src/pages/learn/classes/constructor.mdx b/src/pages/learn/classes/constructor.mdx index 810f553..b727b2a 100644 --- a/src/pages/learn/classes/constructor.mdx +++ b/src/pages/learn/classes/constructor.mdx @@ -7,60 +7,92 @@ import Code from "../../../components/Code.astro" # Constructor/Destructor -(for now) THP's constructors are inspired by Kotlin. +## Constructor -PHP only allows a single constructor, and so does THP. -The basic constructor has the syntax of function parameters. +The constructor syntax in THP is inspired by Kotlin. + +Constructors are declared like function definitions: -The class properties can be declared in the constructor, -using the keywords `pub`, `var`, `val`: +A constructor declared that way is public. + +Note that the parameters in the constructor (`fullname`, +`age` above) are not properties, and cannot be used +inside the class methods, only in the +[`init` block](#init-block) and properties declaration. + +To declare properties in the constructor see +[Constructor promotion](#constructor-promotion). + +### Constructor visibility + +If you want to declare a constructor as protected +or private you need to add the `constructor` +keyword, after the visibility modifier: + + + +### Init block + +The `init` block allow us to run code during the +construction of the instance: + + + + + +### Constructor promotion + +Constructor parameters can serve as class properties. +This is done by adding a modifier and `var`/`val`. + + + + By using this syntax you are declaring properties and assigning them at the same time. The contructor parameters can also have default values. -### Constructor visibility - -The constructor is public by default. It can be made private/protected -like this: - - - - ### Derived properties You can declare properties whose values depend on values @@ -73,7 +105,7 @@ class Animal( { // A property whose value depends on \`fullname\` // This is executed after the contructor - pub val Int name_length = $fullname.length + pub val Int name_length = fullname.length } val a2 = Animal("Doa") @@ -81,22 +113,51 @@ print(a2.name_length) //: 3 `} /> -### Init block +### Constructor that may fail -If you need to additional logic in the constructor you can -use a `init` block. +TBD + +Proposal 1: self!Error `} /> +Proposal 2: + + + +Proposal 3: + + self!Error + { + // Something that may fail + } +} +`} /> + + + +## Destructor + +The destructor in THP is the same as PHP. + + + + +