refactor: change docs to use k&r indentation
This commit is contained in:
parent
fbc8124646
commit
0343af8375
@ -111,8 +111,7 @@ person?.name
|
||||
person?.name ?? "Jane"
|
||||
person?.greet?.()
|
||||
|
||||
if person?
|
||||
{
|
||||
if person? {
|
||||
person.name
|
||||
}
|
||||
`} />
|
||||
|
@ -8,10 +8,8 @@ import Code from "@/components/Code.astro"
|
||||
|
||||
|
||||
<Code thpcode={`
|
||||
class Logger
|
||||
{
|
||||
pub fun log(String msg)
|
||||
{
|
||||
class Logger {
|
||||
pub fun log(String msg) {
|
||||
print(msg)
|
||||
}
|
||||
}
|
||||
@ -20,20 +18,16 @@ class Logger
|
||||
setLogger(Logger())
|
||||
|
||||
// Using an anonymous class
|
||||
setLogger(class
|
||||
{
|
||||
pub fun log(String msg)
|
||||
{
|
||||
setLogger(class {
|
||||
pub fun log(String msg) {
|
||||
print(msg)
|
||||
}
|
||||
})
|
||||
`} />
|
||||
|
||||
<Code thpcode={`
|
||||
setLogger(class(Int param1) -> SomeClass(param1), SomeInterface
|
||||
{
|
||||
pub fun method()
|
||||
{
|
||||
setLogger(class(Int param1) -> SomeClass(param1), SomeInterface {
|
||||
pub fun method() {
|
||||
// code
|
||||
}
|
||||
})
|
||||
|
@ -55,13 +55,11 @@ The `init` block allow us to run code during the
|
||||
construction of the instance:
|
||||
|
||||
<Code thpcode={`
|
||||
class Dog(String name)
|
||||
{
|
||||
class Dog(String name) {
|
||||
pub String name = name
|
||||
Int name_len = name.length
|
||||
|
||||
init
|
||||
{
|
||||
init {
|
||||
print("Dog created: {name}")
|
||||
}
|
||||
}
|
||||
@ -141,10 +139,8 @@ Proposal 1:
|
||||
|
||||
<Code thpcode={`
|
||||
class PrayingMantis(String name)
|
||||
throws Error
|
||||
{
|
||||
init -> Error!
|
||||
{
|
||||
throws Error {
|
||||
init -> Error! {
|
||||
// Initialization code that may fail
|
||||
}
|
||||
}
|
||||
@ -156,10 +152,8 @@ throws Error
|
||||
The destructor in THP is the same as PHP.
|
||||
|
||||
<Code thpcode={`
|
||||
class Owl()
|
||||
{
|
||||
pub fun __destruct()
|
||||
{
|
||||
class Owl() {
|
||||
pub fun __destruct() {
|
||||
// Cleanup
|
||||
print("Destroying Owl...")
|
||||
}
|
||||
|
@ -40,8 +40,7 @@ Properties are declared with `var`/`val` inside a block.
|
||||
They **must** explicitly declare their datatype.
|
||||
|
||||
<Code thpcode={`
|
||||
class Person()
|
||||
{
|
||||
class Person() {
|
||||
// This is an error. Properties must declare their datatype,
|
||||
// even if the compiler can infer it.
|
||||
val name = "Jane Doe"
|
||||
@ -57,8 +56,7 @@ class Person()
|
||||
**Properties are private by default**, but can be made public with `pub`.
|
||||
|
||||
<Code thpcode={`
|
||||
class Person()
|
||||
{
|
||||
class Person() {
|
||||
// Properties are private by default
|
||||
val String name = "John Doe"
|
||||
|
||||
@ -88,10 +86,8 @@ in the Constructor page.
|
||||
Methods are declared with `fun`, as regular functions.
|
||||
|
||||
<Code thpcode={`
|
||||
class Person()
|
||||
{
|
||||
fun greet()
|
||||
{
|
||||
class Person() {
|
||||
fun greet() {
|
||||
print("Hello")
|
||||
}
|
||||
}
|
||||
@ -100,17 +96,14 @@ class Person()
|
||||
**Methods are private by default**, and are made public with `pub`.
|
||||
|
||||
<Code thpcode={`
|
||||
class Person()
|
||||
{
|
||||
class Person() {
|
||||
// This method is private
|
||||
fun private_greet()
|
||||
{
|
||||
fun private_greet() {
|
||||
print("Hello from private method")
|
||||
}
|
||||
|
||||
// Use \`pub\` to make a method public
|
||||
pub fun greet()
|
||||
{
|
||||
pub fun greet() {
|
||||
print("Hello from greet")
|
||||
}
|
||||
}
|
||||
@ -125,8 +118,7 @@ in THP a method cannot have the same name as a property and viceversa.
|
||||
Doing so is a compile error.
|
||||
|
||||
<Code thpcode={`
|
||||
class Person()
|
||||
{
|
||||
class Person() {
|
||||
String name = "Rose"
|
||||
|
||||
// This is a compile error
|
||||
@ -143,17 +135,14 @@ THP uses the dollar sign `$` as this inside classes.
|
||||
It is **required** when using a class property/method.
|
||||
|
||||
<Code thpcode={`
|
||||
class Person()
|
||||
{
|
||||
class Person() {
|
||||
val String name = "Jane Doe"
|
||||
|
||||
pub fun get_name() -> String
|
||||
{
|
||||
pub fun get_name() -> String {
|
||||
return $name
|
||||
}
|
||||
|
||||
pub fun greet()
|
||||
{
|
||||
pub fun greet() {
|
||||
val person_name = $get_name()
|
||||
print("Hello, I'm {person_name}")
|
||||
}
|
||||
@ -166,10 +155,8 @@ class Person()
|
||||
By default methods cannot mutate the state of the object.
|
||||
|
||||
<Code thpcode={`
|
||||
class Animal(var String name)
|
||||
{
|
||||
pub fun set_name(String new_name)
|
||||
{
|
||||
class Animal(var String name) {
|
||||
pub fun set_name(String new_name) {
|
||||
$name = new_name // Error: Cannot mutate $name
|
||||
}
|
||||
}
|
||||
@ -179,10 +166,8 @@ To do so the method must be annotated. The caller must also
|
||||
declare a mutable variable.
|
||||
|
||||
<Code thpcode={`
|
||||
class Animal(var String name)
|
||||
{
|
||||
pub mut fun set_name(String new_name)
|
||||
{
|
||||
class Animal(var String name) {
|
||||
pub mut fun set_name(String new_name) {
|
||||
$name = new_name // Ok
|
||||
}
|
||||
}
|
||||
|
@ -14,16 +14,13 @@ import Code from "@/components/Code.astro"
|
||||
|
||||
|
||||
<Code thpcode={`
|
||||
if condition
|
||||
{
|
||||
if condition {
|
||||
// code
|
||||
}
|
||||
else if condition2
|
||||
{
|
||||
else if condition2 {
|
||||
// more code
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
// even more code
|
||||
}
|
||||
|
||||
@ -38,8 +35,7 @@ val result = if condition { value1 } else { value2 }
|
||||
TBD
|
||||
|
||||
<Code thpcode={`
|
||||
if variable is Datatype
|
||||
{
|
||||
if variable is Datatype {
|
||||
// code using variable
|
||||
}
|
||||
`} />
|
||||
@ -52,13 +48,11 @@ TBD
|
||||
<Code thpcode={`
|
||||
val user_id = POST::get("user_id")
|
||||
|
||||
if Some(user_id) = user_id
|
||||
{
|
||||
if Some(user_id) = user_id {
|
||||
print("user_id exists: {user_id}")
|
||||
}
|
||||
|
||||
if Some(user_id) = user_id && user_id > 0
|
||||
{
|
||||
if Some(user_id) = user_id && user_id > 0 {
|
||||
print("user_id is greater than 0: {user_id}")
|
||||
}
|
||||
`} />
|
||||
|
@ -17,8 +17,7 @@ Braces are required.
|
||||
<Code thpcode={`
|
||||
val numbers = [0, 1, 2, 3]
|
||||
|
||||
for number in numbers
|
||||
{
|
||||
for number in numbers {
|
||||
print(number)
|
||||
}
|
||||
`} />
|
||||
@ -30,8 +29,7 @@ val dict = .{
|
||||
cherries: 3,
|
||||
}
|
||||
|
||||
for value in dict
|
||||
{
|
||||
for value in dict {
|
||||
print("{value}")
|
||||
}
|
||||
`} />
|
||||
@ -42,8 +40,7 @@ for value in dict
|
||||
<Code thpcode={`
|
||||
val numbers = [0, 1, 2, 3]
|
||||
|
||||
for index, number in numbers
|
||||
{
|
||||
for index, number in numbers {
|
||||
print("{index} : {number}")
|
||||
}
|
||||
`} />
|
||||
@ -55,8 +52,7 @@ val dict = .{
|
||||
cherries: 3,
|
||||
}
|
||||
|
||||
for key, value in dict
|
||||
{
|
||||
for key, value in dict {
|
||||
print("{key} => {value}")
|
||||
}
|
||||
`} />
|
||||
@ -68,8 +64,7 @@ for key, value in dict
|
||||
val colors = ["red", "green", "blue"]
|
||||
var index = 0
|
||||
|
||||
while index < colors.size()
|
||||
{
|
||||
while index < colors.size() {
|
||||
print("{colors[index]}")
|
||||
index += 1
|
||||
}
|
||||
@ -83,10 +78,8 @@ TBD
|
||||
You can give labels to loops, allowing you to `break` and `continue` in nested loops.
|
||||
|
||||
<Code thpcode={`
|
||||
:top for i in values_1
|
||||
{
|
||||
for j in values_2
|
||||
{
|
||||
:top for i in values_1 {
|
||||
for j in values_2 {
|
||||
// ...
|
||||
break :top
|
||||
}
|
||||
|
@ -19,33 +19,27 @@ case Some(id) { print("user_id exists: {id}") }
|
||||
case None { print("user_id doesn't exist") }
|
||||
|
||||
match user_id
|
||||
case Some(id)
|
||||
{
|
||||
case Some(id) {
|
||||
print("user_id exists: {id}")
|
||||
}
|
||||
case None
|
||||
{
|
||||
case None {
|
||||
print("user_id doesn't exist")
|
||||
}
|
||||
|
||||
|
||||
match user_id
|
||||
case Some(id) if id > 0
|
||||
{
|
||||
case Some(id) if id > 0 {
|
||||
print("user_id exists: {id}")
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
print("user_id has other values ")
|
||||
}
|
||||
|
||||
match customer_id
|
||||
case 1, 2, 3
|
||||
{
|
||||
case 1, 2, 3 {
|
||||
print("special discount for our first 3 customers!")
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
print("hello dear")
|
||||
}
|
||||
`} />
|
||||
|
@ -14,12 +14,10 @@ const thpcode = `union Animal {
|
||||
val my_pet = try Pets::get_first()
|
||||
|
||||
match my_pet
|
||||
case Dog(name)
|
||||
{
|
||||
case Dog(name) {
|
||||
print("{name} has 1 live ")
|
||||
}
|
||||
case Cat(name, lives)
|
||||
{
|
||||
case Cat(name, lives) {
|
||||
print("{name} has {lives}")
|
||||
}`;
|
||||
|
||||
@ -177,12 +175,10 @@ const [thp_html] = await native_highlighter(
|
||||
thpcode={`
|
||||
match test_file
|
||||
case DirEntry::File(filename)
|
||||
if !filename.starts_with(".")
|
||||
{
|
||||
if !filename.starts_with(".") {
|
||||
print(filename)
|
||||
}
|
||||
case _
|
||||
{
|
||||
case _ {
|
||||
print("A valid file was not found")
|
||||
}
|
||||
`}
|
||||
@ -197,8 +193,7 @@ const [thp_html] = await native_highlighter(
|
||||
thpcode={`
|
||||
String? username = Post::get("username")
|
||||
|
||||
if username?
|
||||
{
|
||||
if username? {
|
||||
// username is a \`String\` here
|
||||
print("Hello, {username}")
|
||||
}
|
||||
@ -215,8 +210,7 @@ const [thp_html] = await native_highlighter(
|
||||
thpcode={`
|
||||
val user_id = POST::get("id")
|
||||
val user = try User::find(user_id)
|
||||
catch DBException e
|
||||
{
|
||||
catch DBException e {
|
||||
log_error(e.message)
|
||||
return page(.{}, 404)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user