D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4272 - x.typeof syntax
Summary: x.typeof syntax
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
: 8661 (view as issue list)
Depends on:
Blocks:
 
Reported: 2010-06-04 15:42 UTC by bearophile_hugs
Modified: 2019-10-23 10:52 UTC (History)
7 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-06-04 15:42:55 UTC
A syntax like x.typeof can be considered.

There are situations where you will need to parenthesize anyway, like:

import std.stdio;
void main() {
    int x = 1;
    float y = 1.5;
    writeln(typeid(typeof(x + y)));
}


You will have to write:
(x + y).typeof


But in many situations with this change you will be able to avoid the ().

This syntax is more similar/uniform to the x.sizeof syntax too (that is sizeof(x) in C).
Comment 1 Trass3r 2010-06-13 17:49:40 UTC
Then typeid should probably also be .typeid instead of typeid()
Comment 2 monarchdodra 2012-09-14 11:17:24 UTC
*** Issue 8661 has been marked as a duplicate of this issue. ***
Comment 3 Jonathan M Davis 2012-09-14 14:20:26 UTC
typeof isn't a property or a function, unlike sizeof. It's like an is-expression, and I think that treating it like a property would be a mistake.
Comment 4 Nick Treleaven 2012-10-06 06:22:10 UTC
(In reply to comment #3)
> typeof isn't a property or a function, unlike sizeof. It's like an
> is-expression, and I think that treating it like a property would be a mistake.

I think (x + y).typeof should not be allowed, use the existing syntax instead.

However, x.typeof is a useful shorthand that helps cut down on nested brackets in is expressions and elsewhere. So I would allow both typeof(expression) and identifier.typeof to be used, but *not* expression.typeof.

The type of an instance is a natural property of the instance IMO.
Comment 5 Maxim Fomin 2012-10-06 06:41:10 UTC
(In reply to comment #4)
> (In reply to comment #3)
> > typeof isn't a property or a function, unlike sizeof. It's like an
> > is-expression, and I think that treating it like a property would be a mistake.
> 
> I think (x + y).typeof should not be allowed, use the existing syntax instead.
> 
> However, x.typeof is a useful shorthand that helps cut down on nested brackets
> in is expressions and elsewhere. So I would allow both typeof(expression) and
> identifier.typeof to be used, but *not* expression.typeof.
> 
> The type of an instance is a natural property of the instance IMO.

The problem is that UFCS was made to work with functions and typeof is not a function. Accepting identifier.typeof would result in questions about which identifiers are valid for this and what else works besides typeof with them.
Comment 6 Nick Treleaven 2012-10-06 10:28:49 UTC
(In reply to comment #5)
> The problem is that UFCS was made to work with functions and typeof is not a
> function. Accepting identifier.typeof would result in questions about which
> identifiers are valid for this and what else works besides typeof with them.

This is not to do with UFCS. There are already many built in properties like x.sizeof, x.init:
http://dlang.org/property.html

typeof fits nicely as a built in property, and helps cut down on nested brackets.
Comment 7 Maxim Fomin 2012-10-07 01:08:41 UTC
(In reply to comment #6)
> (In reply to comment #5)
> > The problem is that UFCS was made to work with functions and typeof is not a
> > function. Accepting identifier.typeof would result in questions about which
> > identifiers are valid for this and what else works besides typeof with them.
> 
> This is not to do with UFCS. There are already many built in properties like
> x.sizeof, x.init:
> http://dlang.org/property.html
> 
> typeof fits nicely as a built in property, and helps cut down on nested
> brackets.

Typeof is not a property either. And it differs from all those properties which, given a type or expression, provide fundamental information about their types like size, default value, name, alignment. Typeof works in opposite direction - given some expression it gives its type. 

BTW, identifier is a primary expression (http://dlang.org/expression.html), so, making idenfier.typeof possible and expression.typeof not (as mentioned above), raises some questions. 

However, if typeof is made a property too, it would be logical and consistent.
Comment 8 RazvanN 2019-10-23 10:52:52 UTC
Closing this as there is not sufficient evidence that this will improve anything and special casing for symbols is not the way to go.