From f1bdcbb53452142ee67d1653610e8dfbcb2f862b Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Tue, 6 Jun 2023 10:06:10 +0200 Subject: [PATCH] Documenting how to obtain static methods for a type (#6938) Fixes #6748 by documenting how to obtain all static methods available on a `Type`. --- .../lib/Standard/Base/0.0.0-dev/src/Meta.enso | 21 ++++++++++++++++++- test/Tests/src/Semantic/Meta_Spec.enso | 11 ++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Meta.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Meta.enso index ccb64e2ac45..3f224cedc8d 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Meta.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Meta.enso @@ -36,7 +36,26 @@ type Type ## 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 self = Vector.from_polyglot_array (get_type_methods self.value) diff --git a/test/Tests/src/Semantic/Meta_Spec.enso b/test/Tests/src/Semantic/Meta_Spec.enso index c4f6eb61211..9ce2e1f17fe 100644 --- a/test/Tests/src/Semantic/Meta_Spec.enso +++ b/test/Tests/src/Semantic/Meta_Spec.enso @@ -30,9 +30,13 @@ type My_Type @b (self -> self.foo) other_method self a = a + create foo bar = My_Type.Value foo bar 3 + @self ("se" + "lf") My_Type.my_method self = self.foo + self.bar + self.baz +My_Type.factory = My_Type.create 1 2 + @a (test_method 3 4) @b (Test_Type.Value 49) @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'] + 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" <| 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" <| java_meta = Meta.meta Random.new java_meta . should_be_a Meta.Polyglot