13
The Task
I guess everybody loves automatic code generation and saving some time during work. You have to create a lot of classes and members during the day and you don't want to create all those getters
manually.
The task is to write a program or function, that generates getters
for all class members automatically for you.
The Input
In our language objects are very simple. Names of classes and members must start with an chararacter from [a-zA-Z]
and can only contain the characters [a-zA-Z0-9]
. Here's an example:
class Stack {
public overflow;
protected trace;
private errorReport;
}
The Output
This is a valid output based on the given example:
class Stack {
public overflow;
protected trace;
private errorReport;
public function getOverflow() {
return this->overflow;
}
public function getTrace() {
return this->trace;
}
public function getErrorReport() {
return this->errorReport;
}
}
The Getter
The requirements for a getter
method are:
- The function name must start with
get
followed by the member name with an uppercase initial. - The function has no parameters.
- To return a variable use
return this->memberName;
. getters
andsetters
(see The Bonuses) must be grouped and must come after all variable declarations.
Example:
private value1;
private value2;
public function getValue1() { return this->value; }
public function setValue1(value) { this->value = value; }
public function getValue2() { return this->value; }
public function setValue2(value) { this->value = value; }
The Requirements
- Create a program or a function.
- Input can come from STDIN, command line arguments, function arguments, a file etc.
- Any output format is acceptable from a simple
return
-value to a file or writing to STDOUT. - In- and output don't need to be formatted with whitespaces, newlines, tabs etc. This is a valid input:
class A{protected a;}
. - You can assume that the input is valid and your program can handle unexpected input unexpected as well.
The Bonuses
You can get down to 10% of your original byte count by withdrawing 30% for each feature:
A: Your program can address newly added variables and adds missing getters
only (public function getB() { return this->b; }
in this case):
class A {
public a;
public b;
public function getA() { return this->a; }
}
B: Your program also generates setters
:
class A {
public a;
public getA() { return this->a; }
public setA(a) { this->a = a; }
}
C: Your program can handle static members:
class A {
public static c;
public static function getC() { return this->c; }
}
This is code golf – so shortest answer in bytes wins. Standard loopholes are disallowed.
3
This is my first question - a not too hard one. Hope you like it. Thanks to Martin Büttner for helpful tips in the Sandbox.
– insertusernamehere – 2015-11-12T15:07:01.067Will there only be one class per input? – Conor O'Brien – 2015-11-12T15:10:44.137
If the input has no newlines, should the output have any? – kirbyfan64sos – 2015-11-12T15:12:31.213
@CᴏɴᴏʀO'Bʀɪᴇɴ Yes, there will be only one class per input. – insertusernamehere – 2015-11-12T15:13:03.620
@kirbyfan64sos Newlines are optional. The output can be pretty or minified. – insertusernamehere – 2015-11-12T15:13:53.720
2When supporting both bonuses
A
andB
should items that have getters, but no setters, have setters in the output? – FryAmTheEggman – 2015-11-12T19:48:33.5071@FryAmTheEggman That's a very good question. I would say that for bonus B you can assume that the input is complete, so if there's a getter there's also a setter. – insertusernamehere – 2015-11-12T20:35:48.733
Do the setters always have to take a variable of that name? For instance, would it be valid to generate
public function setOverflow(x){this->overflow=x;}
? – kirbyfan64sos – 2015-11-12T21:12:50.590@kirbyfan64sos Yes that would be valid, as it doesn't break functionality. – insertusernamehere – 2015-11-12T21:14:01.990
2In which language are you allowed to refer to
this
in astatic
accessor? – Neil – 2015-11-12T22:38:14.200@Neil Good catch - I thought nobody would notice. I saw my mistake after I posted it and there was already one answer. So I didn't want to change the requirements afterwards. It's a very special language. ;) – insertusernamehere – 2015-11-12T22:43:43.833