www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - -> and :: operators

reply Walter Bright <newshound2 digitalmars.com> writes:
These, of course, are C++ operators that are replace with the . operator 
in D. But when I translate C++ code to D, sometimes these operators get 
left behind, and sometimes I simply reflexively type them into D code.

The error message coming out of dmd could be better. I suggest 
recognizing -> and :: in the lexer, and saying something like:

    "The '->' operator is not in D, did you mean '.'?"

Anyone want to do a PR for this? (Should be pretty straightforward.)
Oct 08 2015
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 10/08/2015 08:41 PM, Walter Bright wrote:
 These, of course, are C++ operators that are replace with the . operator
 in D. But when I translate C++ code to D, sometimes these operators get
 left behind, and sometimes I simply reflexively type them into D code.

 The error message coming out of dmd could be better. I suggest
 recognizing -> and :: in the lexer, and saying something like:

     "The '->' operator is not in D, did you mean '.'?"

 Anyone want to do a PR for this? (Should be pretty straightforward.)
Semi-relatedly, a colleague who has heard many D sales pitches from me over the years is recently "looking at Go" and liking it very much. He came to me today telling me about this awesome Go feature where you just type a dot after a pointer and the language is so great that it works! You don't need to type (*p).member. Isn't Go awesome! I responded "yep, it's a great feature and those gostards will never admit that they took that feature from D." (There is probably earlier precedence but it felt great to say it to my friend. :) ) Ali
Oct 08 2015
next sibling parent reply Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= writes:
On Friday, 9 October 2015 at 04:15:42 UTC, Ali Çehreli wrote:
 Go feature where you just type a dot after a pointer and the 
 language is so great that it works! You don't need to type 
 (*p).member. Isn't Go awesome!

 I responded "yep, it's a great feature and those gostards will 
 never admit that they took that feature from D." (There is 
 probably earlier precedence but it felt great to say it to my 
 friend. :) )
Just about all higher languages does this, since the reference type does not have members. Simula too. But this unfortunately breaks down when you add smart-pointers, which makes this approach unsound since pointer-type members collide with object members. So C++ actually got this right by requiring explicit dereferencing.
Oct 09 2015
next sibling parent Atila Neves <atila.neves gmail.com> writes:
On Friday, 9 October 2015 at 12:19:55 UTC, Ola Fosheim Grøstad 
wrote:
 On Friday, 9 October 2015 at 04:15:42 UTC, Ali Çehreli wrote:
 Go feature where you just type a dot after a pointer and the 
 language is so great that it works! You don't need to type 
 (*p).member. Isn't Go awesome!

 I responded "yep, it's a great feature and those gostards will 
 never admit that they took that feature from D." (There is 
 probably earlier precedence but it felt great to say it to my 
 friend. :) )
Just about all higher languages does this, since the reference type does not have members. Simula too. But this unfortunately breaks down when you add smart-pointers, which makes this approach unsound since pointer-type members collide with object members. So C++ actually got this right by requiring explicit dereferencing.
The only case in which the C++ way is right is when there are two member functions of the same name in the pointed-to object and the smart pointer itself since you can disambiguate `ptr->get()` from `ptr.get()`. Atila
Oct 09 2015
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 10/09/2015 05:19 AM, Ola Fosheim Grøstad wrote:
 On Friday, 9 October 2015 at 04:15:42 UTC, Ali Çehreli wrote:
 Go feature where you just type a dot after a pointer and the language
 is so great that it works! You don't need to type (*p).member. Isn't
 Go awesome!

 I responded "yep, it's a great feature and those gostards will never
 admit that they took that feature from D." (There is probably earlier
 precedence but it felt great to say it to my friend. :) )
Just about all higher languages does this, since the reference type does not have members. Simula too. But this unfortunately breaks down when you add smart-pointers, which makes this approach unsound since pointer-type members collide with object members.
Yeah... Type properties is another example for D: .sizeof and .alignof are not propagated to the pointee and .stringof is not propagated to the type of the pointee. Similarly, for class references, one must remember to use __traits(classInstanceSize) and std.traits.classInstanceAlignment. (Yes, I notice the inconsistency. ;) ) Ali
Oct 09 2015
prev sibling parent reply Freddy <Hexagonalstar64 gmail.com> writes:
On Friday, 9 October 2015 at 04:15:42 UTC, Ali Çehreli wrote:
 Semi-relatedly, a colleague who has heard many D sales pitches 
 from me over the years is recently "looking at Go" and liking 
 it very much. He came to me today telling me about this awesome 
 Go feature where you just type a dot after a pointer and the 
 language is so great that it works! You don't need to type 
 (*p).member. Isn't Go awesome!

 I responded "yep, it's a great feature and those gostards will 
 never admit that they took that feature from D." (There is 
 probably earlier precedence but it felt great to say it to my 
 friend. :) )

 Ali
Stole from D? You mean java right?
Oct 09 2015
next sibling parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 09-Oct-2015 21:44, Freddy wrote:
 On Friday, 9 October 2015 at 04:15:42 UTC, Ali Çehreli wrote:
 Semi-relatedly, a colleague who has heard many D sales pitches from me
 over the years is recently "looking at Go" and liking it very much. He
 came to me today telling me about this awesome Go feature where you
 just type a dot after a pointer and the language is so great that it
 works! You don't need to type (*p).member. Isn't Go awesome!

 I responded "yep, it's a great feature and those gostards will never
 admit that they took that feature from D." (There is probably earlier
 precedence but it felt great to say it to my friend. :) )

 Ali
Stole from D? You mean java right?
There is no value type objects in Java so no. More likely C#. -- Dmitry Olshansky
Oct 09 2015
next sibling parent reply Warwick <warwick warwick.com> writes:
On Friday, 9 October 2015 at 19:48:39 UTC, Dmitry Olshansky wrote:
 On 09-Oct-2015 21:44, Freddy wrote:
 Stole from D? You mean java right?
There is no value type objects in Java so no. More likely C#.
Delphi / Object Pascal had it in the mid 90s IIRC. Long before C#, and possibly before Java was released.
Oct 10 2015
parent Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= writes:
On Saturday, 10 October 2015 at 22:54:15 UTC, Warwick wrote:
 On Friday, 9 October 2015 at 19:48:39 UTC, Dmitry Olshansky 
 wrote:
 On 09-Oct-2015 21:44, Freddy wrote:
 Stole from D? You mean java right?
There is no value type objects in Java so no. More likely C#.
Delphi / Object Pascal had it in the mid 90s IIRC. Long before C#, and possibly before Java was released.
Simula is the origin, it came about in the 60s.
Oct 10 2015
prev sibling next sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 10/9/2015 12:48 PM, Dmitry Olshansky wrote:
 On 09-Oct-2015 21:44, Freddy wrote:
 Stole from D? You mean java right?
There is no value type objects in Java so no. More likely C#.
Since C# was an internal Microsoft project at the time this was developed for D, no.
Oct 10 2015
prev sibling parent reply Idan Arye <GenericNPC gmail.com> writes:
On Friday, 9 October 2015 at 19:48:39 UTC, Dmitry Olshansky wrote:
 On 09-Oct-2015 21:44, Freddy wrote:
 On Friday, 9 October 2015 at 04:15:42 UTC, Ali Çehreli wrote:
 Semi-relatedly, a colleague who has heard many D sales 
 pitches from me
 over the years is recently "looking at Go" and liking it very 
 much. He
 came to me today telling me about this awesome Go feature 
 where you
 just type a dot after a pointer and the language is so great 
 that it
 works! You don't need to type (*p).member. Isn't Go awesome!

 I responded "yep, it's a great feature and those gostards 
 will never
 admit that they took that feature from D." (There is probably 
 earlier
 precedence but it felt great to say it to my friend. :) )

 Ali
Stole from D? You mean java right?
There is no value type objects in Java so no. More likely C#.
Nope - C# uses -> to access member of a struct referenced by a pointer. See https://msdn.microsoft.com/en-us/library/50sbeks5.aspx The difference between reference types and pointers is that with reference types, THERE ARE NO value varaiables. So it's safe to use . instead of -> for accessing member through a reference because there is no value type, because there is no such a thing as accessing a member of a reference type without dereferencing it. So it's safe to do so on classes in C#, but not on structs. This is the innovation in D(regarding this issue) - that on struct types, the same operator is used for BOTH the value type and the pointer to it.
Oct 11 2015
next sibling parent reply Warwick <warwick warwick.com> writes:
On Sunday, 11 October 2015 at 09:43:04 UTC, Idan Arye wrote:
 On Friday, 9 October 2015 at 19:48:39 UTC, Dmitry Olshansky 
 wrote:

 This is the innovation in D(regarding this issue) - that on 
 struct types, the same operator is used for BOTH the value type 
 and the pointer to it.
At the risk of sounding like a broken record the Delphi variant of Object Pascal started doing that some time around Delphi 4 or Delphi 5 IIRC. (So mid to late 90s). IE. You accessed members with the dot operator whether it was an object (like D Delphi's objects are heap based / reference semantics), a struct, or a pointer to a struct.
Oct 11 2015
parent reply Idan Arye <GenericNPC gmail.com> writes:
On Sunday, 11 October 2015 at 13:05:41 UTC, Warwick wrote:
 On Sunday, 11 October 2015 at 09:43:04 UTC, Idan Arye wrote:
 On Friday, 9 October 2015 at 19:48:39 UTC, Dmitry Olshansky 
 wrote:

 This is the innovation in D(regarding this issue) - that on 
 struct types, the same operator is used for BOTH the value 
 type and the pointer to it.
At the risk of sounding like a broken record the Delphi variant of Object Pascal started doing that some time around Delphi 4 or Delphi 5 IIRC. (So mid to late 90s). IE. You accessed members with the dot operator whether it was an object (like D Delphi's objects are heap based / reference semantics), a struct, or a pointer to a struct.
You should have elaborated then. The other guys talked about the invention of reference types, so I assumed you did as well.
Oct 11 2015
parent Warwick <warwick warwick.com> writes:
On Sunday, 11 October 2015 at 13:50:18 UTC, Idan Arye wrote:
 On Sunday, 11 October 2015 at 13:05:41 UTC, Warwick wrote:
 On Sunday, 11 October 2015 at 09:43:04 UTC, Idan Arye wrote:
 On Friday, 9 October 2015 at 19:48:39 UTC, Dmitry Olshansky 
 wrote:
At the risk of sounding like a broken record the Delphi variant of Object Pascal started doing that some time around Delphi 4 or Delphi 5 IIRC. (So mid to late 90s). IE. You accessed members with the dot operator whether it was an object (like D Delphi's objects are heap based / reference semantics), a struct, or a pointer to a struct.
You should have elaborated then. The other guys talked about the invention of reference types, so I assumed you did as well.
Sorry I've been on pills to prevent premature elaboration. :-)
Oct 11 2015
prev sibling parent Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= writes:
On Sunday, 11 October 2015 at 09:43:04 UTC, Idan Arye wrote:
 This is the innovation in D(regarding this issue) - that on 
 struct types, the same operator is used for BOTH the value type 
 and the pointer to it.
BETA is the successor to Simula and it does not distinguish between value or reference and only use dot reference. What they do instead of having a dereference operator is to have a "reference operator" and also a "pattern operator" for type variables. So it works something like this: Call a and send the output as the input to b and then store the result: (1,(2,3)) -> a -> b -> (c,(d,e)) Assign reference a to b (instanced class/function) a[] -> b[] Replace class-type variable a with class-type b (not instanced, which is equivalent to a class-type/function pointer) a## -> b## It makes a lot of sense for an OO language since we usually use the referenced content more often than we want to change the pointer.
Oct 11 2015
prev sibling parent reply Jeremy DeHaan <dehaan.jeremiah gmail.com> writes:
On Friday, 9 October 2015 at 18:44:50 UTC, Freddy wrote:
 On Friday, 9 October 2015 at 04:15:42 UTC, Ali Çehreli wrote:
 Semi-relatedly, a colleague who has heard many D sales pitches 
 from me over the years is recently "looking at Go" and liking 
 it very much. He came to me today telling me about this 
 awesome Go feature where you just type a dot after a pointer 
 and the language is so great that it works! You don't need to 
 type (*p).member. Isn't Go awesome!

 I responded "yep, it's a great feature and those gostards will 
 never admit that they took that feature from D." (There is 
 probably earlier precedence but it felt great to say it to my 
 friend. :) )

 Ali
Stole from D? You mean java right?
Java doesn't have pointers.
Oct 09 2015
parent reply deadalnix <deadalnix gmail.com> writes:
On Friday, 9 October 2015 at 19:54:19 UTC, Jeremy DeHaan wrote:
 On Friday, 9 October 2015 at 18:44:50 UTC, Freddy wrote:
 On Friday, 9 October 2015 at 04:15:42 UTC, Ali Çehreli wrote:
 Semi-relatedly, a colleague who has heard many D sales 
 pitches from me over the years is recently "looking at Go" 
 and liking it very much. He came to me today telling me about 
 this awesome Go feature where you just type a dot after a 
 pointer and the language is so great that it works! You don't 
 need to type (*p).member. Isn't Go awesome!

 I responded "yep, it's a great feature and those gostards 
 will never admit that they took that feature from D." (There 
 is probably earlier precedence but it felt great to say it to 
 my friend. :) )

 Ali
Stole from D? You mean java right?
Java doesn't have pointers.
-_- Yeah reference are definitively not pointer. No they aren't. No I told you they aren't. Nope. Not even a little ! No way ! That's not true ! I told you that's not true. Nope !
Oct 09 2015
parent reply Jeremy DeHaan <dehaan.jeremiah gmail.com> writes:
On Friday, 9 October 2015 at 21:21:10 UTC, deadalnix wrote:
 On Friday, 9 October 2015 at 19:54:19 UTC, Jeremy DeHaan wrote:
 On Friday, 9 October 2015 at 18:44:50 UTC, Freddy wrote:
 On Friday, 9 October 2015 at 04:15:42 UTC, Ali Çehreli wrote:
 [...]
Stole from D? You mean java right?
Java doesn't have pointers.
-_- Yeah reference are definitively not pointer. No they aren't. No I told you they aren't. Nope. Not even a little ! No way ! That's not true ! I told you that's not true. Nope !
I guess you could argue it that way, but I don't see references and pointers as the same. Similar, yes, but not the same.
Oct 09 2015
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, 9 October 2015 at 23:57:28 UTC, Jeremy DeHaan wrote:
 On Friday, 9 October 2015 at 21:21:10 UTC, deadalnix wrote:
 On Friday, 9 October 2015 at 19:54:19 UTC, Jeremy DeHaan wrote:
 On Friday, 9 October 2015 at 18:44:50 UTC, Freddy wrote:
 On Friday, 9 October 2015 at 04:15:42 UTC, Ali Çehreli wrote:
 [...]
Stole from D? You mean java right?
Java doesn't have pointers.
-_- Yeah reference are definitively not pointer. No they aren't. No I told you they aren't. Nope. Not even a little ! No way ! That's not true ! I told you that's not true. Nope !
I guess you could argue it that way, but I don't see references and pointers as the same. Similar, yes, but not the same.
Java references (as well as C# references and D references) are managed pointers. Their only real differences from "normal" pointers are that they're managed by the GC, you can't assign an address to them (except by assigning another reference to them), and that you can't dereference them except when accessing one of their members. There's no question that per the computer science definition of a pointer, they qualify. They just aren't quite what you get in C/C++ or from D's pointers which aren't class references. - Jonathan M Davis
Oct 09 2015
prev sibling next sibling parent jmh530 <john.michael.hall gmail.com> writes:
On Friday, 9 October 2015 at 03:41:42 UTC, Walter Bright wrote:
    "The '->' operator is not in D, did you mean '.'?"
In the binary case, -> and <- are assignment operators in R. But I imagine you don't want to go crazy with these sorts of messages.
Oct 08 2015
prev sibling next sibling parent reply ixid <adamsibson hotmail.com> writes:
On Friday, 9 October 2015 at 03:41:42 UTC, Walter Bright wrote:
 These, of course, are C++ operators that are replace with the . 
 operator in D. But when I translate C++ code to D, sometimes 
 these operators get left behind, and sometimes I simply 
 reflexively type them into D code.

 The error message coming out of dmd could be better. I suggest 
 recognizing -> and :: in the lexer, and saying something like:

    "The '->' operator is not in D, did you mean '.'?"

 Anyone want to do a PR for this? (Should be pretty 
 straightforward.)
Too late to change but wouldn't it be better to have one operator for members and another for UFCS? '->' would be good for UFCS.
Oct 09 2015
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, 9 October 2015 at 10:03:18 UTC, ixid wrote:
 Too late to change but wouldn't it be better to have one 
 operator for members and another for UFCS? '->' would be good 
 for UFCS.
That would defeat the purpose of _Uniform_ Function Call Syntax. The whole point is that it uses the same syntax for member functions and free functions. In many cases, it's just for aesthetic purposes, but with templated code, it can be critical, because it allows you to effectively overload a free function with a member function (e.g. a particular range type could overload find if it had a more efficient implementation for it than the one in std.algorithm). It also makes it so that a type which doesn't have member functions can work in a template that's expecting the type to have specific member functions (e.g. arrays use free functions for the range primitives, but they're normally used as if they were member functions). - Jonathan M Davis
Oct 09 2015
parent reply ixid <adamsibson hotmail.com> writes:
On Friday, 9 October 2015 at 10:15:42 UTC, Jonathan M Davis wrote:
 That would defeat the purpose of _Uniform_ Function Call 
 Syntax...
For some reason I'd thought it didn't work when you mixed member function calls with function calls but it seems to do so smoothly.
Oct 09 2015
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, 9 October 2015 at 10:42:03 UTC, ixid wrote:
 On Friday, 9 October 2015 at 10:15:42 UTC, Jonathan M Davis 
 wrote:
 That would defeat the purpose of _Uniform_ Function Call 
 Syntax...
For some reason I'd thought it didn't work when you mixed member function calls with function calls but it seems to do so smoothly.
It works fine in general, and if there's a conflict, it's the member function that gets called. So, if you want to guarantee that it's a particular free function that gets called, you need to not use UFCS (and possibly provide the full import path when using it). But in most cases, it's desirable for a member function to be able able to override the behavior of a free function. The main problem is when they happen to match but do completely different things. - Jonathan M Davis
Oct 09 2015
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On Friday, 9 October 2015 at 03:41:42 UTC, Walter Bright wrote:
 These, of course, are C++ operators that are replace with the . 
 operator in D. But when I translate C++ code to D, sometimes 
 these operators get left behind, and sometimes I simply 
 reflexively type them into D code.

 The error message coming out of dmd could be better. I suggest 
 recognizing -> and :: in the lexer, and saying something like:

    "The '->' operator is not in D, did you mean '.'?"

 Anyone want to do a PR for this? (Should be pretty 
 straightforward.)
https://issues.dlang.org/show_bug.cgi?id=15186 https://github.com/D-Programming-Language/dmd/pull/5190
Oct 11 2015