From 396a5b811a33fcfdd7314346f17d1f689aba171b Mon Sep 17 00:00:00 2001 From: Araozu Date: Sat, 20 Jul 2024 22:44:42 -0500 Subject: [PATCH] add doc pages --- src/pages/learn/classes/abstract.mdx | 7 + src/pages/learn/classes/constructor.mdx | 102 ++++++++++ src/pages/learn/classes/definition.mdx | 197 ++++++-------------- src/pages/learn/classes/inheritance.mdx | 26 +++ src/pages/learn/classes/readonly.mdx | 7 + src/pages/learn/classes/visibility.mdx | 7 + src/pages/learn/index.mdx | 5 + src/pages/learn/templating/control-flow.mdx | 65 +++++++ 8 files changed, 275 insertions(+), 141 deletions(-) create mode 100644 src/pages/learn/classes/abstract.mdx create mode 100644 src/pages/learn/classes/constructor.mdx create mode 100644 src/pages/learn/classes/inheritance.mdx create mode 100644 src/pages/learn/classes/readonly.mdx create mode 100644 src/pages/learn/classes/visibility.mdx diff --git a/src/pages/learn/classes/abstract.mdx b/src/pages/learn/classes/abstract.mdx new file mode 100644 index 0000000..71104ae --- /dev/null +++ b/src/pages/learn/classes/abstract.mdx @@ -0,0 +1,7 @@ +--- +layout: ../../../layouts/DocsLayout.astro +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 new file mode 100644 index 0000000..810f553 --- /dev/null +++ b/src/pages/learn/classes/constructor.mdx @@ -0,0 +1,102 @@ +--- +layout: ../../../layouts/DocsLayout.astro +title: Constructor/Destructor +--- +import Code from "../../../components/Code.astro" + +# Constructor/Destructor + + +(for now) THP's constructors are inspired by Kotlin. + +PHP only allows a single constructor, and so does THP. +The basic constructor has the syntax of function parameters. + + + +The class properties can be declared in the constructor, +using the keywords `pub`, `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 +on the constructor. + + + + +### Init block + +If you need to additional logic in the constructor you can +use a `init` block. + + + diff --git a/src/pages/learn/classes/definition.mdx b/src/pages/learn/classes/definition.mdx index 23afc7d..6c3cf0b 100644 --- a/src/pages/learn/classes/definition.mdx +++ b/src/pages/learn/classes/definition.mdx @@ -1,6 +1,6 @@ --- layout: ../../../layouts/DocsLayout.astro -title: Classes +title: Basics --- import Code from "../../../components/Code.astro" @@ -8,30 +8,34 @@ import Code from "../../../components/Code.astro" Syntax and semantics heavily inspired by Kotlin. +Classes in THP are significantly different than PHP. -## Create a new instance of a class +## Definition -To create an instance of a class call it as if it were a function, +A class is defined as follows: + + + +The name of the class **MUST** begin with an uppercase letter. + + + +## Instanciation + +To create an instance of a class call it as if it was a function, without `new`. -## Simple class - -Classes are declared with the `class` keyword. - - ## Properties -Properties are declared with `var`/`val`. -They **must** declare their datatype. +Properties are declared with `var`/`val` inside a block. +They **must** explicitly declare their datatype. -Properties are private by default, -but can be made public with `pub`. +**Properties are private by default**, but can be made public with `pub`. -More information about how properties interact with the constructor -is found in the contructor section. +Unlike PHP, to access properties and methods use dot notation `.` +instead of an arrow `->`. + +Static properties are explained in the Static page. + +Readonly properties are explained in the Readonly page. + +The interaction between properties and the constructor is explained +in the Constructor page. + ## Methods @@ -77,7 +95,7 @@ class Person } `} /> -Methods are private by default, and are made public with `pub`. +**Methods are private by default**, and are made public with `pub`. +[Unlike PHP](https://www.php.net/manual/en/language.oop5.basic.php#language.oop5.basic.properties-methods), +in THP a method cannot have the same name as a property and viceversa. +Doing so is a compile error. + + String { + "Rose" + } +} +`} /> + + ## This -THP uses the dollar sign `$` as this. It is **required** when -using a class property/method. +THP uses the dollar sign `$` as this inside classes. +It is **required** when using a class property/method. -## Static members - -Static members are detailed in their own page. - - -## Constructor - -(for now) THP's constructors are inspired by Kotlin. - -PHP only allows a single constructor, and so does THP. -The basic constructor has the syntax of function parameters. - - - -The class properties can be declared in the constructor, -using the keywords `pub`, `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 -on the constructor. - - - - -### Init block - -If you need to additional logic in the constructor you can -use a `init` block. - - - - -## Inheritance - - - ## Mutable methods diff --git a/src/pages/learn/classes/inheritance.mdx b/src/pages/learn/classes/inheritance.mdx new file mode 100644 index 0000000..2ccd484 --- /dev/null +++ b/src/pages/learn/classes/inheritance.mdx @@ -0,0 +1,26 @@ +--- +layout: ../../../layouts/DocsLayout.astro +title: Inheritance +--- +import Code from "../../../components/Code.astro" + +# Inheritance + + + + diff --git a/src/pages/learn/classes/readonly.mdx b/src/pages/learn/classes/readonly.mdx new file mode 100644 index 0000000..35c0536 --- /dev/null +++ b/src/pages/learn/classes/readonly.mdx @@ -0,0 +1,7 @@ +--- +layout: ../../../layouts/DocsLayout.astro +title: Readonly +--- +import Code from "../../../components/Code.astro" + +# Readonly diff --git a/src/pages/learn/classes/visibility.mdx b/src/pages/learn/classes/visibility.mdx new file mode 100644 index 0000000..8c5bce1 --- /dev/null +++ b/src/pages/learn/classes/visibility.mdx @@ -0,0 +1,7 @@ +--- +layout: ../../../layouts/DocsLayout.astro +title: Visibility +--- +import Code from "../../../components/Code.astro" + +# Visibility diff --git a/src/pages/learn/index.mdx b/src/pages/learn/index.mdx index 0097fcf..24821d4 100644 --- a/src/pages/learn/index.mdx +++ b/src/pages/learn/index.mdx @@ -44,7 +44,12 @@ pagesLayout: title: Classes children: - path: definition + - path: constructor + - path: inheritance - path: static + - path: visibility + - path: readonly + - path: abstract - path: interfaces - path: anonymous - path: magic diff --git a/src/pages/learn/templating/control-flow.mdx b/src/pages/learn/templating/control-flow.mdx index c5303a9..bff0474 100644 --- a/src/pages/learn/templating/control-flow.mdx +++ b/src/pages/learn/templating/control-flow.mdx @@ -7,3 +7,68 @@ import Info from "../../../components/docs/Info.astro" # Control flow +## If expressions + +Use `@if`, `@else if` and `@else` to branch during the template +creation. + + HTML +{ +
+ @if user.verified + { +

Hello {user.name}

+ } + @else + { +

Verify your account to continue

+ } +
+} +`} /> + + +## Loops + +Use `@for`: + + HTML +{ + val users = User::get_all() + +
+ @for user in users + { + + } +
+} +`} /> + + +## Match + +Use `@match` for pattern matching: + + HTML +{ +
+ @match user.type + case ::Admin + { + + } + case ::User + { + + } +
+} +`} /> + + + +