Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
November 06, 2017 Bug or "hidden" feature? | ||||
---|---|---|---|---|
| ||||
If a function has an UDA you don't need to give the function a return type. Is that a bug or a hidden feature? Example: ``` import std.stdio; struct A { } @A test() { writeln("Called test()"); } @A test2() { return true; } void main() { test(); writeln(test2()); } ``` |
November 06, 2017 Re: Bug or "hidden" feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bauss | On Monday, 6 November 2017 at 09:44:24 UTC, bauss wrote:
> If a function has an UDA you don't need to give the function a return type.
>
> Is that a bug or a hidden feature?
>
> Example:
>
> ```
> import std.stdio;
>
> struct A { }
>
> @A test()
> {
> writeln("Called test()");
> }
>
> @A test2()
> {
> return true;
> }
>
> void main()
> {
> test();
> writeln(test2());
> }
> ```
Feature as return type is not necessary and considered auto when some qualifiers are added
const foo() {}
@property foo() {}
etc
|
November 06, 2017 Re: Bug or "hidden" feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Temtaime | On Monday, 6 November 2017 at 09:48:39 UTC, Temtaime wrote:
> On Monday, 6 November 2017 at 09:44:24 UTC, bauss wrote:
>> If a function has an UDA you don't need to give the function a return type.
>>
>> Is that a bug or a hidden feature?
>>
>> Example:
>>
>> ```
>> import std.stdio;
>>
>> struct A { }
>>
>> @A test()
>> {
>> writeln("Called test()");
>> }
>>
>> @A test2()
>> {
>> return true;
>> }
>>
>> void main()
>> {
>> test();
>> writeln(test2());
>> }
>> ```
>
> Feature as return type is not necessary and considered auto when some qualifiers are added
>
> const foo() {}
>
> @property foo() {}
>
> etc
Yeah I was aware of it for build-in attributes, but didn't know it applied to user-defined attributes. Always thought you had to be explicit about "auto" there, but I guess not.
|
November 06, 2017 Re: Bug or "hidden" feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bauss | On Monday, November 06, 2017 09:50:47 bauss via Digitalmars-d wrote:
> On Monday, 6 November 2017 at 09:48:39 UTC, Temtaime wrote:
> > On Monday, 6 November 2017 at 09:44:24 UTC, bauss wrote:
> >> If a function has an UDA you don't need to give the function a return type.
> >>
> >> Is that a bug or a hidden feature?
> >>
> >> Example:
> >>
> >> ```
> >> import std.stdio;
> >>
> >> struct A { }
> >>
> >> @A test()
> >> {
> >>
> >> writeln("Called test()");
> >>
> >> }
> >>
> >> @A test2()
> >> {
> >>
> >> return true;
> >>
> >> }
> >>
> >> void main()
> >> {
> >>
> >> test();
> >> writeln(test2());
> >>
> >> }
> >> ```
> >
> > Feature as return type is not necessary and considered auto when some qualifiers are added
> >
> > const foo() {}
> >
> > @property foo() {}
> >
> > etc
>
> Yeah I was aware of it for build-in attributes, but didn't know it applied to user-defined attributes. Always thought you had to be explicit about "auto" there, but I guess not.
The same goes for variables. The type is always infered if it's not explicit. It's just that auto is required when there isn't something else there to indicate that it's a variable or function declaration. e.g.
enum foo = "bar";
or
static baz = 42;
or
immutable hello = "world";
- Jonathan M Davis
|
November 06, 2017 Re: Bug or "hidden" feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bauss | On 06.11.2017 10:50, bauss wrote:
>>>
>>
>> Feature as return type is not necessary and considered auto when some qualifiers are added
>>
>> const foo() {}
>>
>> @property foo() {}
>>
>> etc
>
> Yeah I was aware of it for build-in attributes, but didn't know it applied to user-defined attributes.
This is a common misconception. Auto means 'automatic storage' (a C leftover), not 'automatic type deduction'. 'auto' (or some other storage class/attribute) is only required to make it clear that what follows is a declaration. To enable type deduction, just don't specify the type.
|
Copyright © 1999-2021 by the D Language Foundation