doc: update property/atom samples

This commit is contained in:
2025-04-10 13:04:43 +01:00
parent 4dec09352d
commit 8dd67501bd

View File

@@ -65,17 +65,17 @@ class Person
- setAge:age inUnit:units
match units in
#years => self::age = age,
#months => self::age = age / 12,
#days => self::age = age / 365,
$years => self::age = age,
$months => self::age = age / 12,
$days => self::age = age / 365,
_ => self::age = 0
end!
- getAgeInUnit:units
^match units in
#years => self::age,
#months => self::age / 12,
#days => self::age / 365,
$years => self::age,
$months => self::age / 12,
$days => self::age / 365,
_ => 0
end!
@@ -103,12 +103,12 @@ class Person
is converted to the lambda
[ :x | self::val = x ]
*/
$ exampleProperty | get => self::val, set => self::val = value.
-> exampleProperty | get => self::val, set => self::val = value.
/* Without the lambda synthesis, the property would look like this:
Note that this is the only time it is legal to access private fields
via `self` from a lambda. */
$ exampleProperty |
-> exampleProperty |
get => [ ^self::val ],
set => [ :x | self::val = x ].
@@ -118,7 +118,7 @@ class Person
If either the `set` or `get` elements are not provided, the property
becomes read-only or write-only respectively */
$ exampleProperty2 | get => 42.
-> exampleProperty2 | get => 42.
/* A property can also be configured to act just like a regular variable,
by setting the getter and setters to default implementations.
@@ -130,15 +130,15 @@ class Person
Similarly, the default setter will set the value of a private member
variable named by prepending two underscores to the property name
(__exampleProperty3 in this case) to the value provided to the setter. */
$ exampleProperty3 (get, set)
-> exampleProperty3 (get, set)
/* Just like in the earlier examples, either `get` or `set` can be omitted
to create a write-only or read-only property respectively */
$ exampleProperty4 (get)
-> exampleProperty4 (get)
end
p1 = Person new(name:'John Doe' age:34).
p1 setAge:100 inUnit:#months.
p1 setAge:100 inUnit:$months.
/**
@@ -304,7 +304,7 @@ a = (32, 64).
try
v = Int parse:'342'
catch (#err:number_format, err) in
catch ($err:number_format, err) in
cout put:'Cannot parse integer string ({err})'
catch (_, err) in
cout put:'Unknown error occurred ({err})'
@@ -313,7 +313,7 @@ end
/* equivalent 'pure' syntax */
[ v = Int parse:'342' ]
on:#err:number_format do:[ :err :data |
on:$err:number_format do:[ :err :data |
cout put:'Cannot parse integer string ({err})'
];
onError:[ :err :data |
@@ -326,31 +326,6 @@ v = 5
squared
squared.
/**
below are some examples of throwing errors. when an error is thrown,
the runtimes unwinds the stack looking for the nearest error handler.
if no error handlers are found, the exception information is written
to cerr and exection stops.
the exact behaviour of a throw statement depends on what is thrown:
1) a string is thrown: a generic Error object is created.
Sending -msg to the Error returns the thrown string.
Sending -data to the Error returns null.
2) an object that implements the Error protocol is thrown: the
thrown object becomes the error object that is passed to the
error handler (or that is written to cerr when execution stops).
3) an object that doesn't implement the Error protocol is thrown:
a generic Error object is created.
sending -msg to the Error returns null.
sending -data to the Error returns the thrown object.
**/
/* example 1) */
throw 'an error occurred'.
/* example 2) */
throw { i => 32, s => 'error' }.
/******************************************************************************/
x = 32.
@@ -361,7 +336,7 @@ x = 32.
/* and the equivalent 'fancy' syntax */
cout put:'Hello, world!' if x > 10.
cout put:'Hello, world!' unless x <= 10.
-- cout put:'Hello, world!' unless x <= 10.
/**
NOTE that keywords (if, end, try, and so on) can still be used as message
@@ -470,7 +445,7 @@ pkg2 = pkg1 map:[ :x | ^x * 2 ].
**/
age = Person new(name:'John Doe' age:34);
setAge:144 inUnit:#months;
setAge:144 inUnit:$months;
ageInMonths.
-- age now has the value 144
@@ -478,7 +453,7 @@ age = Person new(name:'John Doe' age:34);
age = do
x = Person new(name:'John Doe' age:34).
x setAge:144 inUnit:#months.
x setAge:144 inUnit:$months.
x ageInMonths
end
@@ -501,7 +476,7 @@ end
**/
p1 = Person new(name:'John Doe' age:34);
setAge:100 inUnit:#months;
setAge:100 inUnit:$months;
yourself::
/* p1 now contains the Person object */
@@ -509,7 +484,7 @@ p1 = Person new(name:'John Doe' age:34);
p1 = do
x = Person new(name:'John Doe' age:34).
x setAge:100 inUnit:#months.
x setAge:100 inUnit:$months.
x
end.