Better HN
topnewbestaskshowjobs
Log inSign up

anvius

2 karma · joined Apr 12, 2016 (10y ago) · 3 submissions

Submissions

comment 1y ago · link · parent

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()

2.

Ask HN: A PHP Superset Language

1 points · by anvius · 1y ago · 3 comments

comment 8y ago · link · parent

Nooooo! It could be pineapple!!! :)