refactor: change docs to use k&r indentation

This commit is contained in:
Araozu 2024-11-21 22:01:10 -05:00
parent fbc8124646
commit 0343af8375
8 changed files with 54 additions and 107 deletions

View File

@ -111,8 +111,7 @@ person?.name
person?.name ?? "Jane" person?.name ?? "Jane"
person?.greet?.() person?.greet?.()
if person? if person? {
{
person.name person.name
} }
`} /> `} />

View File

@ -8,10 +8,8 @@ import Code from "@/components/Code.astro"
<Code thpcode={` <Code thpcode={`
class Logger class Logger {
{ pub fun log(String msg) {
pub fun log(String msg)
{
print(msg) print(msg)
} }
} }
@ -20,20 +18,16 @@ class Logger
setLogger(Logger()) setLogger(Logger())
// Using an anonymous class // Using an anonymous class
setLogger(class setLogger(class {
{ pub fun log(String msg) {
pub fun log(String msg)
{
print(msg) print(msg)
} }
}) })
`} /> `} />
<Code thpcode={` <Code thpcode={`
setLogger(class(Int param1) -> SomeClass(param1), SomeInterface setLogger(class(Int param1) -> SomeClass(param1), SomeInterface {
{ pub fun method() {
pub fun method()
{
// code // code
} }
}) })

View File

@ -55,13 +55,11 @@ The `init` block allow us to run code during the
construction of the instance: construction of the instance:
<Code thpcode={` <Code thpcode={`
class Dog(String name) class Dog(String name) {
{
pub String name = name pub String name = name
Int name_len = name.length Int name_len = name.length
init init {
{
print("Dog created: {name}") print("Dog created: {name}")
} }
} }
@ -141,10 +139,8 @@ Proposal 1:
<Code thpcode={` <Code thpcode={`
class PrayingMantis(String name) class PrayingMantis(String name)
throws Error throws Error {
{ init -> Error! {
init -> Error!
{
// Initialization code that may fail // Initialization code that may fail
} }
} }
@ -156,10 +152,8 @@ throws Error
The destructor in THP is the same as PHP. The destructor in THP is the same as PHP.
<Code thpcode={` <Code thpcode={`
class Owl() class Owl() {
{ pub fun __destruct() {
pub fun __destruct()
{
// Cleanup // Cleanup
print("Destroying Owl...") print("Destroying Owl...")
} }

View File

@ -40,8 +40,7 @@ Properties are declared with `var`/`val` inside a block.
They **must** explicitly declare their datatype. They **must** explicitly declare their datatype.
<Code thpcode={` <Code thpcode={`
class Person() class Person() {
{
// This is an error. Properties must declare their datatype, // This is an error. Properties must declare their datatype,
// even if the compiler can infer it. // even if the compiler can infer it.
val name = "Jane Doe" val name = "Jane Doe"
@ -57,8 +56,7 @@ class Person()
**Properties are private by default**, but can be made public with `pub`. **Properties are private by default**, but can be made public with `pub`.
<Code thpcode={` <Code thpcode={`
class Person() class Person() {
{
// Properties are private by default // Properties are private by default
val String name = "John Doe" val String name = "John Doe"
@ -88,10 +86,8 @@ in the Constructor page.
Methods are declared with `fun`, as regular functions. Methods are declared with `fun`, as regular functions.
<Code thpcode={` <Code thpcode={`
class Person() class Person() {
{ fun greet() {
fun greet()
{
print("Hello") print("Hello")
} }
} }
@ -100,17 +96,14 @@ class Person()
**Methods are private by default**, and are made public with `pub`. **Methods are private by default**, and are made public with `pub`.
<Code thpcode={` <Code thpcode={`
class Person() class Person() {
{
// This method is private // This method is private
fun private_greet() fun private_greet() {
{
print("Hello from private method") print("Hello from private method")
} }
// Use \`pub\` to make a method public // Use \`pub\` to make a method public
pub fun greet() pub fun greet() {
{
print("Hello from 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. Doing so is a compile error.
<Code thpcode={` <Code thpcode={`
class Person() class Person() {
{
String name = "Rose" String name = "Rose"
// This is a compile error // 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. It is **required** when using a class property/method.
<Code thpcode={` <Code thpcode={`
class Person() class Person() {
{
val String name = "Jane Doe" val String name = "Jane Doe"
pub fun get_name() -> String pub fun get_name() -> String {
{
return $name return $name
} }
pub fun greet() pub fun greet() {
{
val person_name = $get_name() val person_name = $get_name()
print("Hello, I'm {person_name}") print("Hello, I'm {person_name}")
} }
@ -166,10 +155,8 @@ class Person()
By default methods cannot mutate the state of the object. By default methods cannot mutate the state of the object.
<Code thpcode={` <Code thpcode={`
class Animal(var String name) class Animal(var String name) {
{ pub fun set_name(String new_name) {
pub fun set_name(String new_name)
{
$name = new_name // Error: Cannot mutate $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. declare a mutable variable.
<Code thpcode={` <Code thpcode={`
class Animal(var String name) class Animal(var String name) {
{ pub mut fun set_name(String new_name) {
pub mut fun set_name(String new_name)
{
$name = new_name // Ok $name = new_name // Ok
} }
} }

View File

@ -14,16 +14,13 @@ import Code from "@/components/Code.astro"
<Code thpcode={` <Code thpcode={`
if condition if condition {
{
// code // code
} }
else if condition2 else if condition2 {
{
// more code // more code
} }
else else {
{
// even more code // even more code
} }
@ -38,8 +35,7 @@ val result = if condition { value1 } else { value2 }
TBD TBD
<Code thpcode={` <Code thpcode={`
if variable is Datatype if variable is Datatype {
{
// code using variable // code using variable
} }
`} /> `} />
@ -52,13 +48,11 @@ TBD
<Code thpcode={` <Code thpcode={`
val user_id = POST::get("user_id") 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}") 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}") print("user_id is greater than 0: {user_id}")
} }
`} /> `} />

View File

@ -17,8 +17,7 @@ Braces are required.
<Code thpcode={` <Code thpcode={`
val numbers = [0, 1, 2, 3] val numbers = [0, 1, 2, 3]
for number in numbers for number in numbers {
{
print(number) print(number)
} }
`} /> `} />
@ -30,8 +29,7 @@ val dict = .{
cherries: 3, cherries: 3,
} }
for value in dict for value in dict {
{
print("{value}") print("{value}")
} }
`} /> `} />
@ -42,8 +40,7 @@ for value in dict
<Code thpcode={` <Code thpcode={`
val numbers = [0, 1, 2, 3] val numbers = [0, 1, 2, 3]
for index, number in numbers for index, number in numbers {
{
print("{index} : {number}") print("{index} : {number}")
} }
`} /> `} />
@ -55,8 +52,7 @@ val dict = .{
cherries: 3, cherries: 3,
} }
for key, value in dict for key, value in dict {
{
print("{key} => {value}") print("{key} => {value}")
} }
`} /> `} />
@ -68,8 +64,7 @@ for key, value in dict
val colors = ["red", "green", "blue"] val colors = ["red", "green", "blue"]
var index = 0 var index = 0
while index < colors.size() while index < colors.size() {
{
print("{colors[index]}") print("{colors[index]}")
index += 1 index += 1
} }
@ -83,10 +78,8 @@ TBD
You can give labels to loops, allowing you to `break` and `continue` in nested loops. You can give labels to loops, allowing you to `break` and `continue` in nested loops.
<Code thpcode={` <Code thpcode={`
:top for i in values_1 :top for i in values_1 {
{ for j in values_2 {
for j in values_2
{
// ... // ...
break :top break :top
} }

View File

@ -19,33 +19,27 @@ case Some(id) { print("user_id exists: {id}") }
case None { print("user_id doesn't exist") } case None { print("user_id doesn't exist") }
match user_id match user_id
case Some(id) case Some(id) {
{
print("user_id exists: {id}") print("user_id exists: {id}")
} }
case None case None {
{
print("user_id doesn't exist") print("user_id doesn't exist")
} }
match user_id match user_id
case Some(id) if id > 0 case Some(id) if id > 0 {
{
print("user_id exists: {id}") print("user_id exists: {id}")
} }
else else {
{
print("user_id has other values ") print("user_id has other values ")
} }
match customer_id match customer_id
case 1, 2, 3 case 1, 2, 3 {
{
print("special discount for our first 3 customers!") print("special discount for our first 3 customers!")
} }
else else {
{
print("hello dear") print("hello dear")
} }
`} /> `} />

View File

@ -14,12 +14,10 @@ const thpcode = `union Animal {
val my_pet = try Pets::get_first() val my_pet = try Pets::get_first()
match my_pet match my_pet
case Dog(name) case Dog(name) {
{
print("{name} has 1 live ") print("{name} has 1 live ")
} }
case Cat(name, lives) case Cat(name, lives) {
{
print("{name} has {lives}") print("{name} has {lives}")
}`; }`;
@ -177,12 +175,10 @@ const [thp_html] = await native_highlighter(
thpcode={` thpcode={`
match test_file match test_file
case DirEntry::File(filename) case DirEntry::File(filename)
if !filename.starts_with(".") if !filename.starts_with(".") {
{
print(filename) print(filename)
} }
case _ case _ {
{
print("A valid file was not found") print("A valid file was not found")
} }
`} `}
@ -196,9 +192,8 @@ const [thp_html] = await native_highlighter(
title="Null safety" title="Null safety"
thpcode={` thpcode={`
String? username = Post::get("username") String? username = Post::get("username")
if username? if username? {
{
// username is a \`String\` here // username is a \`String\` here
print("Hello, {username}") print("Hello, {username}")
} }
@ -215,8 +210,7 @@ const [thp_html] = await native_highlighter(
thpcode={` thpcode={`
val user_id = POST::get("id") val user_id = POST::get("id")
val user = try User::find(user_id) val user = try User::find(user_id)
catch DBException e catch DBException e {
{
log_error(e.message) log_error(e.message)
return page(.{}, 404) return page(.{}, 404)
} }