www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Trivial (but not bikeshed please) question of

reply Russel Winder via Digitalmars-d <digitalmars-d puremagic.com> writes:
I need to know the community view on D idiom regarding UFCS in one
particular case (mostly because I am doing a presentation and need to
know which one to put on the slides).

Given:

	import std.concurrency: Tid, send

and some code that spawns, then in a spawned task should I write:

	parent.send(result)

or:

	send(parent, result)

as being idiomatic D code?

This has all the appearance of a potential troll, but that is not my
intention, I really do need to know which the community feels is
idiomatic D.


--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder ekiga.n=
et
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
Oct 28 2014
next sibling parent "Gary Willoughby" <dev nomad.so> writes:
On Tuesday, 28 October 2014 at 08:15:58 UTC, Russel Winder via 
Digitalmars-d wrote:
 I need to know the community view on D idiom regarding UFCS in 
 one
 particular case (mostly because I am doing a presentation and 
 need to
 know which one to put on the slides).
If you are asking in the general term, i always favour the UFCS style wherever possible. I personally think it reads better and is easier to refactor later if needed. I also believe it's idiomatic which is strongly influenced by std.algorithm and std.range's composable functions.
Oct 28 2014
prev sibling next sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 10/28/2014 1:15 AM, Russel Winder via Digitalmars-d wrote:
 This has all the appearance of a potential troll, but that is not my
 intention, I really do need to know which the community feels is
 idiomatic D.
It's a good question. I prefer: parent.send(result) when doing a "pipeline" style operation, like: parent.send(result).doSomethingElse().finishUp(); and when using 'send' to extend a struct/class interface, and parent is a pointer to the struct/class. Otherwise, I use the traditional function style.
Oct 28 2014
prev sibling next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Tuesday, October 28, 2014 08:15:44 Russel Winder via 
Digitalmars-d wrote:
 I need to know the community view on D idiom regarding UFCS in 
 one
 particular case (mostly because I am doing a presentation and 
 need to
 know which one to put on the slides).

 Given:

   import std.concurrency: Tid, send

 and some code that spawns, then in a spawned task should I 
 write:

   parent.send(result)

 or:

   send(parent, result)

 as being idiomatic D code?

 This has all the appearance of a potential troll, but that is 
 not my
 intention, I really do need to know which the community feels is
 idiomatic D.
I don't see how it matters much. Both are perfectly valid. Plenty of folks like UFCS, but it doesn't get used everywhere. The primary, technical benefit of UFCS (aside from pure personal preference) is that in generic code, it allows for member functions to override the more generic free functions and specialize their behavior. Code that wants that should use UFCS. Code that doesn't want that, should avoid UFCS. Also, code that needs to disambiguate between free functions (due to conflicting imports) needs to either avoid UFCS or use aliases to get around the conflict. For all other code, it's a matter of personal preference. I'd say that the only thing that makes UFCS more D-like beyond that is simply the fact that most other languages can't do it (especially D's primary competitors), but that isn't really a technical benefit so much as something that's simply different about D. - Jonathan M Davis
Oct 28 2014
prev sibling parent reply "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Tuesday, 28 October 2014 at 08:15:58 UTC, Russel Winder via 
Digitalmars-d wrote:
 	parent.send(result)

 or:

 	send(parent, result)

 as being idiomatic D code?
I cannot speak for idioms, but this is a good example of how UFCS fails to capture the semantics of dot notation. "X.action(Y)" will in most OO languages mean do "action" to object "X", but "parent.send(results)" means the opposite?! That's not good. "send(parent,result)" is therefore better.
Oct 28 2014
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Ola Fosheim Grøstad:

 "X.action(Y)" will in most OO languages mean do "action" to 
 object "X", but "parent.send(results)" means the opposite?!
Why the opposite? Bye, bearophile
Oct 28 2014
parent "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Tuesday, 28 October 2014 at 13:59:10 UTC, bearophile wrote:
 Ola Fosheim Grøstad:

 "X.action(Y)" will in most OO languages mean do "action" to 
 object "X", but "parent.send(results)" means the opposite?!
Poorly formulated… :P
 Why the opposite?
You are instructing the object to send the input somewhere: "postoffice.send(letter,person)" "object.send(message,recipient)" "system.send(X,Y)" "send(X,Y)"
Oct 28 2014
prev sibling next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 10/28/14 9:41 AM, "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= 
<ola.fosheim.grostad+dlang gmail.com>" wrote:
 On Tuesday, 28 October 2014 at 08:15:58 UTC, Russel Winder via
 Digitalmars-d wrote:
     parent.send(result)

 or:

     send(parent, result)

 as being idiomatic D code?
I cannot speak for idioms, but this is a good example of how UFCS fails to capture the semantics of dot notation. "X.action(Y)" will in most OO languages mean do "action" to object "X", but "parent.send(results)" means the opposite?! That's not good. "send(parent,result)" is therefore better.
I think it means, send result to parent. Isn't this what you said? On the larger question, I think whatever seems most natural should be used. UFCS can make things read very confusing. But I don't think this is an example of that. -Steve
Oct 28 2014
parent reply "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Tuesday, 28 October 2014 at 14:04:35 UTC, Steven Schveighoffer 
wrote:
 I think it means, send result to parent. Isn't this what you 
 said?
I had to run out the door and hit enter too early… :P
 On the larger question, I think whatever seems most natural 
 should be used. UFCS can make things read very confusing. But I 
 don't think this is an example of that.
I think "send" is really only the right term to use for a mediating third object: network-connection, mail-agent etc… E.g.: postoffice.send(letter,person) system.send(message,receiver) vs flyingcarpet.send_to(location) person.receive_letter(mailbox) mailbox.receive_letters_from(postman)
Oct 28 2014
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 10/28/14 10:56 AM, "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= 
<ola.fosheim.grostad+dlang gmail.com>" wrote:
 On Tuesday, 28 October 2014 at 14:04:35 UTC, Steven Schveighoffer wrote:
 I think it means, send result to parent. Isn't this what you said?
I had to run out the door and hit enter too early… :P
 On the larger question, I think whatever seems most natural should be
 used. UFCS can make things read very confusing. But I don't think this
 is an example of that.
I think "send" is really only the right term to use for a mediating third object: network-connection, mail-agent etc… E.g.: postoffice.send(letter,person) system.send(message,receiver) vs flyingcarpet.send_to(location) person.receive_letter(mailbox) mailbox.receive_letters_from(postman)
I don't think recipient.send(mail) is that unintuitive. It's how I would visualize it from a contact application for instance. -Steve
Oct 28 2014
next sibling parent reply "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Tuesday, 28 October 2014 at 16:02:03 UTC, Steven Schveighoffer 
wrote:
 I don't think recipient.send(mail) is that unintuitive. It's 
 how I would visualize it from a contact application for 
 instance.
sender.send(mail) ? Consistency about direction is important when you choose names and syntax. I think in general that message passing "obj.method()" should be used when the function is either mutating the object or is an activity that the object has a sense of ownership on. UFCS breaks this distinction, e.g.: arr.sort() // inline mutating sort vs. sort(arr) // functional pure sort Chaining would be better done using a pipelining syntax: stringarr -> sort -> reverse -> join!", " -> file.write; gen_pairs() -> (k,v) => (k,v,v*v) -> takestriplet -> auto result;
Oct 28 2014
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 10/28/14 1:09 PM, "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= 
<ola.fosheim.grostad+dlang gmail.com>" wrote:
 On Tuesday, 28 October 2014 at 16:02:03 UTC, Steven Schveighoffer wrote:
 I don't think recipient.send(mail) is that unintuitive. It's how I
 would visualize it from a contact application for instance.
sender.send(mail) ? Consistency about direction is important when you choose names and syntax. I think in general that message passing "obj.method()" should be used when the function is either mutating the object or is an activity that the object has a sense of ownership on.
But parent is not the actual object, it's a *mailbox* of that object, or a reference. In essence, you are saying "use this recipient record to send a message to it's target"
 UFCS breaks this distinction, e.g.:

 arr.sort()  // inline mutating sort

 vs.

 sort(arr)  // functional pure sort
If you are saying we should expect sort(arr) to return a *copy* of the array that is sorted, I don't think that's a fair assessment of D user expectations. D is not a functional language. Even D pure function can mutate data. -Steve
Oct 28 2014
next sibling parent reply Russel Winder via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Tue, 2014-10-28 at 13:44 -0400, Steven Schveighoffer via
Digitalmars-d wrote:
[=E2=80=A6]
 If you are saying we should expect sort(arr) to return a *copy* of the=
=20
 array that is sorted, I don't think that's a fair assessment of D user=
=20
 expectations. D is not a functional language. Even D pure function can=
=20
 mutate data.
Python's take on this works quite well: x.sort() is a mutating sort delivering nothing, whilst: sorted(x) created a new sorted item based on the data of x. In both case you can give optional arguments to specify the sort key on the data in the container and the direction of the sort. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Oct 28 2014
parent "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Tuesday, 28 October 2014 at 17:57:26 UTC, Russel Winder via 
Digitalmars-d wrote:
 Python's take on this works quite well:

 	x.sort()

 is a mutating sort delivering nothing, whilst:

 	sorted(x)
Yes, I also find the grammatical distinction interesting. One possible mapping: - imperative for mutation: do "sort" ! - adjectives for value semantics: it returns a "sorted" version. - present participle for generators: it adds "sorting" property to input.
Oct 28 2014
prev sibling parent reply "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Tuesday, 28 October 2014 at 17:44:36 UTC, Steven Schveighoffer 
wrote:
 But parent is not the actual object, it's a *mailbox* of that 
 object, or a reference. In essence, you are saying "use this 
 recipient record to send a message to it's target"
I think in general you should strive to achieve this syntax: "subject.method(object);" Who are you asking to do the "send"? Not "parent", so parent is not subject for "send", it is subject for "receive": "Subject, do method using object!".
 If you are saying we should expect sort(arr) to return a *copy* 
 of the array that is sorted, I don't think that's a fair 
 assessment of D user expectations.
I prefer to use import systems where I retain the module name for non-generic "free" functions. So then then module will be the subject of the action, or I add a prefix with underscore to indicate an affiliated subject: os.syscall() // os is the subject gc_free() // gc subsystem is the subject I very seldom have short free functions that aren't strictly pure.
Oct 28 2014
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 10/28/14 3:28 PM, "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= 
<ola.fosheim.grostad+dlang gmail.com>" wrote:
 On Tuesday, 28 October 2014 at 17:44:36 UTC, Steven Schveighoffer wrote:
 But parent is not the actual object, it's a *mailbox* of that object,
 or a reference. In essence, you are saying "use this recipient record
 to send a message to it's target"
I think in general you should strive to achieve this syntax: "subject.method(object);" Who are you asking to do the "send"? Not "parent", so parent is not subject for "send", it is subject for "receive": "Subject, do method using object!".
I look at it as: "pipe which I have named 'parent' to help me remember who is at the other end, please send this data to your other end" In any case, I get your point, and I think we can probably move on from this. Subjectivity is never resolvable :) -Steve
Oct 28 2014
prev sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 10/28/14 9:02 AM, Steven Schveighoffer wrote:
 I don't think recipient.send(mail) is that unintuitive. It's how I would
 visualize it from a contact application for instance.
Totally. It's actually how OOP started - calling a method was sending a message to an object etc. -- Andrei
Oct 28 2014
next sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 10/28/2014 7:00 PM, Andrei Alexandrescu wrote:
 Totally.
Bitchin' fer shur
Oct 28 2014
prev sibling parent reply "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Wednesday, 29 October 2014 at 02:00:20 UTC, Andrei 
Alexandrescu wrote:
 On 10/28/14 9:02 AM, Steven Schveighoffer wrote:
 I don't think recipient.send(mail) is that unintuitive. It's 
 how I would
 visualize it from a contact application for instance.
Totally. It's actually how OOP started - calling a method was sending a message to an object etc. -- Andrei
Incorrect, the message passing terminology comes from Smalltalk, but this applies there too. OOP started with Simula67 which focused on Object Oriented Modeling for simulation. You have a world of objects, representing aspects of their real world counterparts (abstract or concrete). These objects have methods which represent actions/events of the object. Coherent and sensible naming is considered an important aspect of modeling. D has a lot to learn from other areas of CS.
Oct 29 2014
parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 10/29/2014 12:46 AM, "Ola Fosheim Grøstad" 
<ola.fosheim.grostad+dlang gmail.com>" wrote:
 Coherent and sensible naming is considered an important aspect of modeling. D
 has a lot to learn from other areas of CS.
D doesn't tell you what to name things, and neither does any other language.
Oct 29 2014
parent reply "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Wednesday, 29 October 2014 at 08:38:02 UTC, Walter Bright 
wrote:
 D doesn't tell you what to name things, and neither does any 
 other language.
Languages provide a semantic view of how to model. A library (like phobos) provides prebuilt components that implies a way to model. Universities should teach how to model in a consistent fashion so that the semantics of the model is easy to grasp for an outsider. Languages and their libraries should support this modeling effort and make it easier. It is important that a lot of thought has been put into libraries and consistent naming and semantics, because they affect the overall model. Things like UFCS and certain aspect of phobos is making the model less clear. D needs to stop expanding and tighten what you have (or even remove features).
Oct 29 2014
parent reply Russel Winder via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Wed, 2014-10-29 at 08:50 +0000, via Digitalmars-d wrote:
[=E2=80=A6]
 Universities should teach how to model in a consistent fashion so=20
 that the semantics of the model is easy to grasp for an outsider.=20
Universities can never teach anything. Universities are places that should support and guide student learning. The distinction here is actually very subtle, often misunderstood, and yet crucial to real success.=20
 Languages and their libraries should support this modeling effort=20
 and make it easier.
=20
 It is important that a lot of thought has been put into libraries=20
 and consistent naming and semantics, because they affect the=20
 overall model.
The naming strategy in the standard library of a language acts as an exemplar of how a language should be used. STL and C++, Phobos and D. Thus the naming and coding conventions in Phobos specify what the D community should emulate. Even when they are wrong. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Oct 29 2014
parent reply "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Wednesday, 29 October 2014 at 15:09:27 UTC, Russel Winder via 
Digitalmars-d wrote:
 Universities can never teach anything. Universities are places 
 that should support and guide student learning.
Teachers teach the moment they open their mouths… (even if it is unintended and the wrong things are being deduced by students).
 The naming strategy in the standard library of a language acts 
 as an exemplar of how a language should be used. STL and C++, 
 Phobos and D.
Which is why I currently do not use STL or Phobos…
 Thus the naming and coding conventions in Phobos specify what 
 the D  community should emulate. Even when they are wrong.
Hardly.
Oct 29 2014
parent reply Russel Winder via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Wed, 2014-10-29 at 15:46 +0000, via Digitalmars-d wrote:
[=E2=80=A6]
 Hardly.
I must clearly improve on my attempts at satire.=20 --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Oct 29 2014
parent "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Wednesday, 29 October 2014 at 16:08:06 UTC, Russel Winder via 
Digitalmars-d wrote:
 On Wed, 2014-10-29 at 15:46 +0000, via Digitalmars-d wrote:
 […]
 Hardly.
I must clearly improve on my attempts at satire.
Oh… :^) *blush* Well, I actually kinda agree with your satire for languages which For a system level language the libraries and runtime ought to be optional, the language/compiler and how you can use it to support your own project is the only thing that truly matters.
Oct 29 2014
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 10/28/2014 06:41 AM, "Ola Fosheim Grøstad" 
<ola.fosheim.grostad+dlang gmail.com>" wrote:
 On Tuesday, 28 October 2014 at 08:15:58 UTC, Russel Winder via
 Digitalmars-d wrote:
     parent.send(result)

 or:

     send(parent, result)

 as being idiomatic D code?
I cannot speak for idioms, but this is a good example of how UFCS fails to capture the semantics of dot notation. "X.action(Y)" will in most OO languages mean do "action" to object "X", but "parent.send(results)" means the opposite?! That's not good. "send(parent,result)" is therefore better.
Agreed. Going off-topic a little, I don't like putting .writeln at the end for a similar reason: It makes sense only when it is writeln (not writefln) and there is only one item to write: 42.writeln("hello"); // Not good "The value: %s".writefln(42); // Not good 42.writeln; // Acceptable but not for Ali Although acceptable, I don't use the last form because I don't like the other forms. Ali
Oct 28 2014