digitalmars.D.learn - Needed return type in static method? bug or feature?
- Antonio Corbi (23/23) Mar 08 2016 Hi all!
- Adam D. Ruppe (7/8) Mar 08 2016 It is allowed because the "auto" keyword doesn't actually
- Antonio Corbi (13/21) Mar 08 2016 Thank's Adam!.
- Jonathan M Davis via Digitalmars-d-learn (8/32) Mar 08 2016 The return type is optional so long as one of the keywords that indicate...
Hi all! The following code compiles and works, but the static methods do not have a return type. It also compiles and works if the appropiate (or auto) return type is added to them. -----------------8><---------------------------- import std.stdio; class B { int foo () { return 1; } static sbar () { return "hi!"; } static ibar () { return 0; } } void main () { auto b = new B; writeln (B.sbar); writeln (B.ibar); } -----------------8><---------------------------- Is it a feature or a bug? I've seen it being used in https://github.com/gecko0307/dlib/blob/master/dlib/math/matrix.d Thank's for your help!
Mar 08 2016
On Tuesday, 8 March 2016 at 13:40:06 UTC, Antonio Corbi wrote:Is it a feature or a bug?It is allowed because the "auto" keyword doesn't actually required for auto functions (or variables), what you need is any one of the storage classes. Those include static, auto, const, immutable, even pure. If any of them are present, the compiler knows you are writing a function or declaring a variable and will infer the type.
Mar 08 2016
On Tuesday, 8 March 2016 at 14:13:17 UTC, Adam D. Ruppe wrote:On Tuesday, 8 March 2016 at 13:40:06 UTC, Antonio Corbi wrote:Thank's Adam!. I had figured out something like this but I couldn't find anything in the docs (http://dlang.org/spec/attribute.html#static), moreover, the example there: ----------8><--------------------- class Foo { static int bar() { return 6; } ... ----------8><--------------------- does mention the return type, that's what confused me.Is it a feature or a bug?It is allowed because the "auto" keyword doesn't actually required for auto functions (or variables), what you need is any one of the storage classes. Those include static, auto, const, immutable, even pure. If any of them are present, the compiler knows you are writing a function or declaring a variable and will infer the type.
Mar 08 2016
On Tuesday, March 08, 2016 14:56:06 Antonio Corbi via Digitalmars-d-learn wrote:On Tuesday, 8 March 2016 at 14:13:17 UTC, Adam D. Ruppe wrote:The return type is optional so long as one of the keywords that indicates that it's a variable or a function is there, so you can choose to put it or not. In most cases, I think that folks put the return type on functions or use auto, but it's up to you. Where it usually comes up is enums and variable declarations. - Jonathan M DavisOn Tuesday, 8 March 2016 at 13:40:06 UTC, Antonio Corbi wrote:Thank's Adam!. I had figured out something like this but I couldn't find anything in the docs (http://dlang.org/spec/attribute.html#static), moreover, the example there: ----------8><--------------------- class Foo { static int bar() { return 6; } ... ----------8><--------------------- does mention the return type, that's what confused me.Is it a feature or a bug?It is allowed because the "auto" keyword doesn't actually required for auto functions (or variables), what you need is any one of the storage classes. Those include static, auto, const, immutable, even pure. If any of them are present, the compiler knows you are writing a function or declaring a variable and will infer the type.
Mar 08 2016