Documenting how to obtain static methods for a type (#6938)

Fixes #6748 by documenting how to obtain all static methods available on a `Type`.
This commit is contained in:
Jaroslav Tulach 2023-06-06 10:06:10 +02:00 committed by GitHub
parent 27feaf6bc5
commit f1bdcbb534
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -36,7 +36,26 @@ type Type
## ADVANCED ## ADVANCED
Returns a vector of `Meta.Constructor` for this type Returns a vector of method names that can be invoked
on instances of this type.
? Static Methods
To obtain list of _static methods_ on a given type
use `Meta.type_of`.
> Example
All instance methods to invoke on `Integer` as
`(v:Integer) v.method_name...`:
Meta.meta Integer . methods
> Example
All static methods to invoke on `Integer` as
`Integer.method_name...`:
Meta.meta (Meta.type_of Integer) . methods
methods : Vector methods : Vector
methods self = methods self =
Vector.from_polyglot_array (get_type_methods self.value) Vector.from_polyglot_array (get_type_methods self.value)

View File

@ -30,9 +30,13 @@ type My_Type
@b (self -> self.foo) @b (self -> self.foo)
other_method self a = a other_method self a = a
create foo bar = My_Type.Value foo bar 3
@self ("se" + "lf") @self ("se" + "lf")
My_Type.my_method self = self.foo + self.bar + self.baz My_Type.my_method self = self.foo + self.bar + self.baz
My_Type.factory = My_Type.create 1 2
@a (test_method 3 4) @a (test_method 3 4)
@b (Test_Type.Value 49) @b (Test_Type.Value 49)
@c (Error.throw "Error Value") @c (Error.throw "Error Value")
@ -247,9 +251,16 @@ spec =
methods.sort . should_equal ['bar', 'baz', 'first_method', 'foo', 'my_method', 'other_method', 'second_method'] methods.sort . should_equal ['bar', 'baz', 'first_method', 'foo', 'my_method', 'other_method', 'second_method']
Test.specify "static methods of MyType" <|
methods = Meta.meta (Meta.type_of My_Type) . methods
methods.sort . should_equal ['Value', 'create', 'factory', 'first_method', 'my_method', 'other_method', 'second_method']
Test.specify "methods of Integer" <| Test.specify "methods of Integer" <|
Meta.meta Integer . methods . sort . should_equal ['round', 'truncate'] Meta.meta Integer . methods . sort . should_equal ['round', 'truncate']
Test.specify "static methods of Integer" <|
Meta.meta (Meta.type_of Integer) . methods . sort . should_equal ['parse', 'parse_builtin', 'round', 'truncate']
Test.specify "should correctly handle Java values" <| Test.specify "should correctly handle Java values" <|
java_meta = Meta.meta Random.new java_meta = Meta.meta Random.new
java_meta . should_be_a Meta.Polyglot java_meta . should_be_a Meta.Polyglot