auto*

Patrick Schluter via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 6 22:28:38 PDT 2017


On Thursday, 6 July 2017 at 23:50:24 UTC, bauss wrote:
> On Thursday, 6 July 2017 at 23:12:03 UTC, H. S. Teoh wrote:
>> On Thu, Jul 06, 2017 at 10:31:10PM +0000, Meta via 
>> Digitalmars-d wrote:
>>> On Thursday, 6 July 2017 at 21:58:45 UTC, H. S. Teoh wrote:
>>> > On Thu, Jul 06, 2017 at 09:42:22PM +0000, Meta via 
>>> > Digitalmars-d wrote:
>>> > > On Thursday, 6 July 2017 at 18:10:57 UTC, FoxyBrown wrote:
>>> > > > Create an auto pointer, handy in some cases and fits in 
>>> > > > the language as a natural generalization.
>>> > > 
>>> > > It's been suggested before (as well as more powerful 
>>> > > generalization for slices and associative arrays), but 
>>> > > Andrei vetoed it so it probably won't be added even if 
>>> > > somebody created a formal proposal for it.
>>> > 
>>> > I'm curious, what exactly was proposed?  Because I have a 
>>> > hard time understanding what's intended from the OP's 
>>> > description.
>>> > 
>>> > 
>>> > T
>>> 
>>> Partial type inference. `auto*` declares a point to a type 
>>> that is inferred from inspecting the RHS's type. The previous 
>>> proposal was for doing `auto[] a = [0, 1, 2]` (a's type is 
>>> inferred as int[]).
>>
>> But doesn't `auto a = [0, 1, 2];` *already* infer typeof(a) as 
>> int[]? What does `auto[]` add over what `auto` already does?
>>
>>
>> T
>
> It does, but I think it's more a thing of knowing what exactly 
> the auto is.
>
> Let's say you have.
>
> auto a = foo();
>
> You have no idea what auto actually is in that case, but
>
> auto* a = foo();
>
> You know auto is a pointer of whatever foo returns.

If you want to document the type returned, then use an explicit 
type. Type deduction is imho a thing to avoid redundancy in an 
expression, not to transform D into python (and to make templates 
possible). If the type of an expression becomes difficult to 
deduce, it is a good idea imo to explicitely write out the type. 
This adds a simple constraint, i.e. adds a way for the compiler 
to exploit the type system to make the program more secure.


More information about the Digitalmars-d mailing list