Add docs on constructors
This commit is contained in:
parent
396a5b811a
commit
ee0be7635b
@ -86,7 +86,11 @@ export async function native_highlighter(code: string): Promise<string> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function translate_token_type(tt: TokenType, value: string): string {
|
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) {
|
switch (tt) {
|
||||||
case "Datatype":
|
case "Datatype":
|
||||||
|
@ -5,3 +5,5 @@ title: Abstract
|
|||||||
import Code from "../../../components/Code.astro"
|
import Code from "../../../components/Code.astro"
|
||||||
|
|
||||||
# Abstract
|
# Abstract
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,60 +7,92 @@ import Code from "../../../components/Code.astro"
|
|||||||
# Constructor/Destructor
|
# Constructor/Destructor
|
||||||
|
|
||||||
|
|
||||||
(for now) THP's constructors are inspired by Kotlin.
|
## Constructor
|
||||||
|
|
||||||
PHP only allows a single constructor, and so does THP.
|
The constructor syntax in THP is inspired by Kotlin.
|
||||||
The basic constructor has the syntax of function parameters.
|
|
||||||
|
Constructors are declared like function definitions:
|
||||||
|
|
||||||
<Code thpcode={`
|
<Code thpcode={`
|
||||||
// |this is the constructor |
|
// |this is the constructor |
|
||||||
class Animal(String fullname, Int age)
|
class Animal(String fullname, Int age)
|
||||||
|
|
||||||
val a1 = Animal("Nal", 4)
|
// Creating an instance
|
||||||
|
val cat = Animal("Michi", 3)
|
||||||
`} />
|
`} />
|
||||||
|
|
||||||
The class properties can be declared in the constructor,
|
A constructor declared that way is public.
|
||||||
using the keywords `pub`, `var`, `val`:
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
|
||||||
<Code thpcode={`
|
<Code thpcode={`
|
||||||
class Animal(
|
// Cow has a protected constructor
|
||||||
// Since we are using val/var, these are promoted to class properties
|
class Cow
|
||||||
val String fullname,
|
protected constructor(String color)
|
||||||
var Int age,
|
|
||||||
)
|
// Bat has a private constructor
|
||||||
|
class Bat
|
||||||
|
private constructor(Int height)
|
||||||
|
`} />
|
||||||
|
|
||||||
|
|
||||||
|
### Init block
|
||||||
|
|
||||||
|
The `init` block allow us to run code during the
|
||||||
|
construction of the instance:
|
||||||
|
|
||||||
|
<Code thpcode={`
|
||||||
|
class Dog(String name)
|
||||||
{
|
{
|
||||||
pub fun say()
|
pub String name = name
|
||||||
|
Int name_len = name.length
|
||||||
|
|
||||||
|
init
|
||||||
{
|
{
|
||||||
// Here we are using the properties declared in the constructor
|
print("Dog created: {name}")
|
||||||
print("My name is {$fullname} and i'm {$age} years old")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val a1 = Animal("Nal", 4)
|
|
||||||
a1.say() //: My name is Nal and i'm 4 years old
|
|
||||||
`} />
|
`} />
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### Constructor promotion
|
||||||
|
|
||||||
|
Constructor parameters can serve as class properties.
|
||||||
|
This is done by adding a modifier and `var`/`val`.
|
||||||
|
|
||||||
|
<Code thpcode={`
|
||||||
|
class Parrot(
|
||||||
|
// A public property
|
||||||
|
pub val String name,
|
||||||
|
// A protected property
|
||||||
|
protected var Int age,
|
||||||
|
// A private property
|
||||||
|
var String last_name,
|
||||||
|
)
|
||||||
|
`} />
|
||||||
|
|
||||||
|
|
||||||
By using this syntax you are declaring properties and assigning them
|
By using this syntax you are declaring properties and assigning them
|
||||||
at the same time.
|
at the same time.
|
||||||
|
|
||||||
The contructor parameters can also have default values.
|
The contructor parameters can also have default values.
|
||||||
|
|
||||||
|
|
||||||
### Constructor visibility
|
|
||||||
|
|
||||||
The constructor is public by default. It can be made private/protected
|
|
||||||
like this:
|
|
||||||
|
|
||||||
<Code thpcode={`
|
|
||||||
class Animal
|
|
||||||
private constructor(
|
|
||||||
val String fullname,
|
|
||||||
var Int age,
|
|
||||||
)
|
|
||||||
{...}
|
|
||||||
`} />
|
|
||||||
|
|
||||||
|
|
||||||
### Derived properties
|
### Derived properties
|
||||||
|
|
||||||
You can declare properties whose values depend on values
|
You can declare properties whose values depend on values
|
||||||
@ -73,7 +105,7 @@ class Animal(
|
|||||||
{
|
{
|
||||||
// A property whose value depends on \`fullname\`
|
// A property whose value depends on \`fullname\`
|
||||||
// This is executed after the contructor
|
// This is executed after the contructor
|
||||||
pub val Int name_length = $fullname.length
|
pub val Int name_length = fullname.length
|
||||||
}
|
}
|
||||||
|
|
||||||
val a2 = Animal("Doa")
|
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
|
TBD
|
||||||
use a `init` block.
|
|
||||||
|
Proposal 1:
|
||||||
|
|
||||||
<Code thpcode={`
|
<Code thpcode={`
|
||||||
class Animal(
|
class PrayingMantis(String name) -> self!Error
|
||||||
val String fullname,
|
|
||||||
)
|
|
||||||
{
|
|
||||||
init
|
|
||||||
{
|
|
||||||
print("{$fullname} in contruction...")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val a3 = Animal("Lola") //: Lola in construction
|
|
||||||
`} />
|
`} />
|
||||||
|
|
||||||
|
Proposal 2:
|
||||||
|
|
||||||
|
<Code thpcode={`
|
||||||
|
class PrayingMantis(String name)
|
||||||
|
throws Error
|
||||||
|
`} />
|
||||||
|
|
||||||
|
Proposal 3:
|
||||||
|
|
||||||
|
<Code thpcode={`
|
||||||
|
class PrayingMantis(String name)
|
||||||
|
{
|
||||||
|
init -> self!Error
|
||||||
|
{
|
||||||
|
// Something that may fail
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`} />
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Destructor
|
||||||
|
|
||||||
|
The destructor in THP is the same as PHP.
|
||||||
|
|
||||||
|
<Code thpcode={`
|
||||||
|
class Owl
|
||||||
|
{
|
||||||
|
pub fun __destruct()
|
||||||
|
{
|
||||||
|
// Cleanup
|
||||||
|
print("Destroying Owl...")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`} />
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user