April 14, 2013
On Sunday, 14 April 2013 at 06:35:53 UTC, qznc wrote:
> Sat, 13 Apr 2013 17:38:24 -0700: Ali Çehreli wrote
[...]
>> When it is a reference though, not immutable but D's const is similar to
>> C's const. Otherwise, there is the following difference:
>> 
>>      // in C: I have no idea whether c is immutable void foo(const int *
>>      c);
>> 
>>      // in D: I know d is immutable void foo(immutable const ref d);
>> 
>> (Actually, not only I "know", but I "demand" that d is immutable.)
>
> The C variant is an mutable pointer to an immutable int. What is not to
> know about that?

You can pass a pointer to a mutable int to the C version foo.
You can't do that with D's immutable.
April 14, 2013
On 04/13/2013 11:35 PM, qznc wrote:

>> D's immutable is similar to C's const only when we are talking about
>> values:
>>
>>       const int c_i = 42;    // in C immutable d_i = 42;    // in D
>>
>> When it is a reference though, not immutable but D's const is similar to
>> C's const. Otherwise, there is the following difference:
>>
>>       // in C: I have no idea whether c is immutable void foo(const int *
>>       c);
>>
>>       // in D: I know d is immutable void foo(immutable const ref d);
>>
>> (Actually, not only I "know", but I "demand" that d is immutable.)
>
> The C variant is an mutable pointer to an immutable int. What is not to
> know about that?

What foo() does not know is whether the original int is const or not:

    int i = 0;
    foo(&i);

    // Can be mutated by the caller later on
    i = 1;

For that reason, function foo() cannot store the pointer 'c' in confidence that it will not change in the future.

Of course you and the dlang.org link that you have provided indicate that immutable is not the same as const. When you say "You can qualify variables as immutable, which is similiar to C's const and Java's final, but it is transitive", it sounds like the main difference that brings 'immutable' is transitivity but I think the fact that data cannot be mutated is the main difference. That makes it possible for a function to request immutable data, something not possible in C because a const reference parameter is not a requirement but a promise not to mutate.

And of course you never say they are the same; you say "similar". Nothing is wrong with that. :)

Ali

August 28, 2015
On Sunday, 14 April 2013 at 15:27:29 UTC, Ali Çehreli wrote:
> On 04/13/2013 11:35 PM, qznc wrote:
>
> >> [...]
> talking about
> >>       [...]
> // in D
> >> [...]
> is similar to
> >>       [...]
> foo(const int *
> >>       [...]
> const ref d);
> >> [...]
> immutable.)
> >
> > The C variant is an mutable pointer to an immutable int. What
> is not to
> > know about that?
>
> What foo() does not know is whether the original int is const or not:
>
>     int i = 0;
>     foo(&i);
>
>     // Can be mutated by the caller later on
>     i = 1;
>
> For that reason, function foo() cannot store the pointer 'c' in confidence that it will not change in the future.
>
> Of course you and the dlang.org link that you have provided indicate that immutable is not the same as const. When you say "You can qualify variables as immutable, which is similiar to C's const and Java's final, but it is transitive", it sounds like the main difference that brings 'immutable' is transitivity but I think the fact that data cannot be mutated is the main difference. That makes it possible for a function to request immutable data, something not possible in C because a const reference parameter is not a requirement but a promise not to mutate.
>
> And of course you never say they are the same; you say "similar". Nothing is wrong with that. :)
>
> Ali

This is excellent information on functional programming with D. I would love to see a lot more information in this area - perhaps a much longer article covering in more detail - and also covering what is missing - e.g. does D have a for comprehension, Option, Either etc
August 28, 2015
On Fri, 2015-08-28 at 06:45 +0000, Kingsley via Digitalmars-d-announce wrote:
> 
[…]
> This is excellent information on functional programming with D. I would love to see a lot more information in this area - perhaps a much longer article covering in more detail - and also covering what is missing - e.g. does D have a for comprehension, Option, Either etc

"For comprehensions" are (more less) just ways of doing lazy sequence comprehensions in Scala, Clojure, etc. I think I prefer comprehensions in the Miranda, Haskell, Python, etc. style: generator expression are handled with constructs that do not resemble explicit iteration for loops. Less to create confusion.

-- 
Russel.
=============================================================================
Dr Russel Winder     t:+44 20 7585 2200   voip:sip:
russel.winder@ekiga.net
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



1 2
Next ›   Last »