|
Here an example code: // NOTE: $this file is provided as a single file as an example only.
// Classes should be publicly accessible and not private, but in $this example it's not necessary.
// It is possible add composition of classes go-style with compose.
namespace Domain
use \StdPS\String
use \StdPS\Array
use \StdPS\Time
use \StdPS\Scheduler
class Contact
var $id: string
var $name: string
var $email: string
construct($id: string, $name: string, $email: string): void
$this.id = $id
$this.name = $name
$this.email = $email
end
pub func updateEmail($newEmail: string): void
$this.validateEmail($newEmail)
$this.email = $newEmail
end
toString(): string
return String.Join(["Contact(id:", $this.id, ", name:", $this.name, ", email:", $this.email, ")"], " ")
end
func validateEmail($newEmail: string): void
if (!String.Email.Validate($newEmail))
raise(InvalidArgumentError, "Invalid email address: " + $newEmail)
end
end
end
interface ContactRepository
pub func save($contact: Contact): void
pub func findById($id: string): Contact|void
pub func findAll(): [Contact]
end
class InMemoryContactRepository: ContactRepository
mut $contacts: [string:Contact]
construct(): void
$this.contacts = []
end
pub func save($contact: Contact): void
$this.contacts[$contact.id] = $contact
end
pub func findById($id: string): Contact|void
if (isset($this.contacts[$id]))
return($this.contacts[$id])
end
return(null)
end
pub func findAll(): [Contact]
return($this.contacts)
end
end
pub class ContactService
var $repository: ContactRepository
construct($repo: ContactRepository): void
$this.repository = $repo
end
pub func registerContact($id: string, $name: string, $email: string): void
var $contact = Contact($id, $name, $email)
$this.repository.save($contact)
end
pub func getContactInfo($id: string): string|void
var $contact = $this.repository.findById($id)
if ($contact)
return($contact.toString())
end
return(null)
end
pub func listAllContacts(): [Contact]
return($this.repository.findAll())
end
end
mut $repo = InMemoryContactRepository()
var $service = ContactService($repo)
$service.registerContact($id="1", $name="Alice", $email="alice@example.com")
$service.registerContact($id="2", $name="Bob", $email="bob@example.com")
echo(String.Join(["Contact Info:", $service.getContactInfo("1")], "\n"))
echo("\nAll Contacts:\n")
var $all = $service.listAllContacts()
foreach($all, $contact)
echo($contact.toString(), "\n")
end
var $numbers = [10,20,30,40,50]
var $subNumbers = $numbers[1:4] // Expecting [20,30,40]
echo("Sliced Numbers: " + String\Join($subNumbers, ", "))
func simulateTask($id: int): void
Time\Sleep(1)
echo("Task " + $id + " completed")
end
php(simulateTask(1))
php(simulateTask(2))
echo("Launched tasks concurrently.")
Scheduler.JoinAll()
|
Having mutable and constants is great, but I'm not fan of apocopes like pub, mut and func. English is full of small words, there is really no need to go out of it. Think let, tie, tye, bag, box...
You can also make string and array as expression in which you can call functions through dot notation. Or even like ruby have percent notation à la Perl/Ruby for array/string creation.