Skip to content

Commit

Permalink
chore(docs): updating docs
Browse files Browse the repository at this point in the history
  • Loading branch information
razshare committed Mar 1, 2024
1 parent 03ddab2 commit 7c2ca80
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 24 deletions.
28 changes: 8 additions & 20 deletions docs/28.goffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,37 +44,31 @@ Now use `goffi()` to interop with your _Go_ program from _Php_.
```php
<?php
// src/main.php
use CatPaw\Core\Unsafe;
use function CatPaw\Core\goffi;
use function CatPaw\Core\anyError;

interface Contract {
/**
* Double a value.
* @param int $value
* @return Unsafe<int>
*/
function DoubleIt(int $value):Unsafe;
function DoubleIt(int $value):int;
}

function main(){
function main():Unsafe{
return anyError(function(){
$lib = goffi(Contract::class, './libgoffi.so')->try($error)
or yield $error;

$doubled = $lib->DoubleIt(3)->try($error)
or yield $error;
$doubled = $lib->DoubleIt(3);

echo "doubled: $doubled\n";
});
}
```

> [!NOTE]
> - Pay attention to the return type of `DoubleIt` in the Php interface, it's `Unsafe`.\
> That is because all Go calls are treated as unsafe calls.\
> When calling `goffi()`, your interface is being parsed through reflection.\
> If any of your interface methods doesn't return an `Unsafe`, the `goffi()` call will fail.
> - Go native errors may also be managed through this mechanism in the future.
> If any of your interface methods doesn't specify a return type, the `goffi()` call will fail.
Run the program.

Expand Down Expand Up @@ -108,26 +102,20 @@ Call _Greeting_ from php like so

```php
<?php
use CatPaw\Core\Unsafe;
use function CatPaw\Core\anyError;
use function CatPaw\Core\goffi;
use CatPaw\Core\Unsafe;

interface Contract {
/**
* Double a value.
* @param int $value
* @return Unsafe<void>
*/
function Greeting(string $name):Unsafe;
function Greeting(string $name):void;
}

function main():Unsafe {
return anyError(function() {
$goffi = goffi(Contract::class, './libgoffi.so')->try($error)
or yield $error;

$goffi->Greeting('world')->try($error)
or yield $error;
$goffi->Greeting('world');
});
}
```
Expand Down
5 changes: 1 addition & 4 deletions src/scripts/Core/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,7 @@ function asFileName(string ...$path):string {
* return error($error);
* }
*
* $doubled = $lib->DoubleIt(3)->try($error);
* if($error){
* return error($error);
* }
* $doubled = $lib->DoubleIt(3);
* echo "doubled: $doubled\n";
* }
* ```
Expand Down

0 comments on commit 7c2ca80

Please sign in to comment.