auto ref deduction and common type deduction inconsistency

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Tue Aug 19 16:56:02 PDT 2014


On Tue, 19 Aug 2014 22:28:25 +0000
Peter Alexander via Digitalmars-d <digitalmars-d at puremagic.com> wrote:

> The  rule in the spec is "The lexically first ReturnStatement 
> determines the ref-ness of [an auto ref] function"
> 
> Why is this? I think it would be more consistent and convenient 
> to be: "An auto ref function returns by ref if all return paths 
> return an lvalue, else it returns by value".
first: compilation speed. compiler can stop looking at function just
after the first 'return'.

second: it's easier to human to determine the actual return type this
way. imagine that you want to return refs, and somewhere deep in your
code you accidentaly returns some literal. it can take ages to figure
out what happens.

just add something like "if (0) return 42;" to foo(). compiler will
eliminate dead code, but will use 'return 42' to determine function
return type.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20140820/ef3eb4c4/attachment.sig>


More information about the Digitalmars-d mailing list