Derive variable name of return value from type
Only make an exception if the return type matches the invocant to
avoid confusing code samples like
my $string = $string->trim;
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/888627fd
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/888627fd
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/888627fd
Branch: refs/heads/master
Commit: 888627fdab9f19e364a024b0085d121b3b18cac7
Parents: 55a44b1
Author: Nick Wellnhofer <[email protected]>
Authored: Sat Feb 27 15:15:55 2016 +0100
Committer: Nick Wellnhofer <[email protected]>
Committed: Sat Feb 27 15:15:55 2016 +0100
----------------------------------------------------------------------
compiler/src/CFCPerlPod.c | 23 ++++++++++++++---------
1 file changed, 14 insertions(+), 9 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/888627fd/compiler/src/CFCPerlPod.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCPerlPod.c b/compiler/src/CFCPerlPod.c
index 19a35b4..855b698 100644
--- a/compiler/src/CFCPerlPod.c
+++ b/compiler/src/CFCPerlPod.c
@@ -392,18 +392,24 @@ CFCPerlPod_gen_subroutine_pod(CFCCallable *func,
static char*
S_gen_code_sample(CFCCallable *func, const char *alias, CFCClass *klass,
int is_constructor) {
- char *prologue = CFCUtil_sprintf("");
+ char *prologue = CFCUtil_sprintf("");
+ char *class_var_name = S_camel_to_lower(CFCClass_get_struct_sym(klass));
CFCType *ret_type = CFCCallable_get_return_type(func);
if (!CFCType_is_void(ret_type)) {
- if (is_constructor) {
- char *ret_name = S_perl_var_name(ret_type, is_constructor);
- prologue = CFCUtil_cat(prologue, "my $", ret_name, " = ", NULL);
- FREEMEM(ret_name);
+ char *ret_name = S_perl_var_name(ret_type, is_constructor);
+
+ if (!is_constructor && strcmp(ret_name, class_var_name) == 0) {
+ // Return type equals `klass`. Use a generic variable name
+ // to avoid confusing code samples like
+ // `my $string = $string->trim`.
+ prologue = CFCUtil_cat(prologue, "my $result = ", NULL);
}
else {
- prologue = CFCUtil_cat(prologue, "my $retval = ", NULL);
+ prologue = CFCUtil_cat(prologue, "my $", ret_name, " = ", NULL);
}
+
+ FREEMEM(ret_name);
}
if (is_constructor) {
@@ -411,9 +417,7 @@ S_gen_code_sample(CFCCallable *func, const char *alias,
CFCClass *klass,
prologue = CFCUtil_cat(prologue, invocant, NULL);
}
else {
- char *lower = S_camel_to_lower(CFCClass_get_struct_sym(klass));
- prologue = CFCUtil_cat(prologue, "$", lower, NULL);
- FREEMEM(lower);
+ prologue = CFCUtil_cat(prologue, "$", class_var_name, NULL);
}
prologue = CFCUtil_cat(prologue, "->", alias, NULL);
@@ -433,6 +437,7 @@ S_gen_code_sample(CFCCallable *func, const char *alias,
CFCClass *klass,
sample = S_gen_positional_sample(prologue, param_list, start);
}
+ FREEMEM(class_var_name);
FREEMEM(prologue);
return sample;
}