Re: Why we cannot use string in mixins?

2016-02-29 Thread Jesse Phillips via Digitalmars-d-learn

On Sunday, 28 February 2016 at 03:08:14 UTC, mahdi wrote:

Thanks.

So the author was plain wrong about using enums instead of 
strings. The misconception is due to assuming we can use 
`string` variables at compile time but we cannot (as they are 
run-time data).


Not exactly either.

#define str "My string for use"

In C people know the processor which creates a "manifest const." 
In D, it was chosen to reuse enum as the keyword to declare 
manifest const.


enum string str = "My string for use";

But in D, we don't required declaring the type everywhere so you 
can just specify a storage class:


const str = "My string for use";

Combine the things together and you create a compile time string 
without specifying that the type is string.


enum str = "My string for use";



Re: Why we cannot use string in mixins?

2016-02-27 Thread sigod via Digitalmars-d-learn

On Saturday, 27 February 2016 at 23:43:07 UTC, cym13 wrote:
Could you please provide a link to said comment? Maybe some 
context would help bring some sanity over this statement.


Topic: 
https://www.reddit.com/r/programming/comments/30sqtd/why_didnt_the_d_language_become_mainstream_as/


Said comment: 
https://www.reddit.com/r/programming/comments/30sqtd/why_didnt_the_d_language_become_mainstream_as/cpvwdkb




Re: Why we cannot use string in mixins?

2016-02-27 Thread mahdi via Digitalmars-d-learn

Thanks.

So the author was plain wrong about using enums instead of 
strings. The misconception is due to assuming we can use `string` 
variables at compile time but we cannot (as they are run-time 
data).


Re: Why we cannot use string in mixins?

2016-02-27 Thread Chris Wright via Digitalmars-d-learn
On Sat, 27 Feb 2016 23:29:49 +, mahdi wrote:

> I read this criticism about D on Reddit and it claims that you cannot
> use strings in mixins. Can you please elaborate about this and the
> reason behind it?
> 
> QUOTE:
> Look at strings: they are defined as immutable(char []).

That would be an immutable array pointing to immutable characters. That 
is, you have a (length, pointer) tuple; the tuple and its members can't 
be modified.

In other words, this isn't valid:

  immutable(char[]) s = "hello";
  s = "world";

Because you'd be overwriting the pointer and length properties of `s`.

If `string` were an alias to `immutable(char[])`, that would be pretty 
inconvenient.

Instead, strings are defined as immutable(char)[]. That's a (length, 
pointer) tuple; the length and the pointer are mutable. However, the data 
that the pointer points to cannot be modified.

> "immutable" as in "you could put it in ROM",

immutable means you could mprotect(2) that data as readonly and it 
wouldn't change how the program runs. You can come up with mutable data 
at runtime and then make an immutable copy of it. Arrays, for instance, 
have a special property, idup, that yields an immutable copy. You can 
create a class which can be constructed as an immutable instance.

enum, on the other hand, means that the data (and anything it points to) 
must be known at compile time. It's all in the binary's data segment.

> ... CTFE can't see strings correctly (can't be determined at compile
> time).

CTFE is performed at compile time. The person was complaining that all 
strings must be determined at compile time. This objection doesn't even 
make sense given their misconceptions about the meaning of 'immutable'.

> So rather than fix the compiler
> so that strings can be used in mixins in the way that people expect it
> to work even if that means making them something other than
> immutable(char []), they decided to start using enums where strings
> actually go. Seriously: take the same code and replace 'string "foo"'
> with 'enum "foo"' and it starts to work!

Neither of those examples are valid D syntax.

The person might have observed something like: you can declare a variable 
as a string initialized with a compile-time constant. Then you try to use 
it as a template parameter or a CTFE function parameter. It fails, 
complaining that you tried to use a variable that's not a compile-time 
constant. You replace `string` with `enum` and it works.

In other words, you replace a mutable pointer and length with a compile-
time constant and suddenly you can use it as a compile-time constant. I'm 
sure this is shocking.


Re: Why we cannot use string in mixins?

2016-02-27 Thread Rikki Cattermole via Digitalmars-d-learn

On 28/02/16 12:43 PM, cym13 wrote:

On Saturday, 27 February 2016 at 23:29:49 UTC, mahdi wrote:

I read this criticism about D on Reddit and it claims that you cannot
use strings in mixins. Can you please elaborate about this and the
reason behind it?

QUOTE:
Look at strings: they are defined as immutable(char []). "immutable"
as in "you could put it in ROM", ... CTFE can't see strings correctly
(can't be determined at compile time). So rather than fix the compiler
so that strings can be used in mixins in the way that people expect it
to work even if that means making them something other than
immutable(char []), they decided to start using enums where strings
actually go. Seriously: take the same code and replace 'string "foo"'
with 'enum "foo"' and it starts to work!
END QUOTE


This sounds like (please, pardon my language) a shitty load of bullshit
from an incompetent that didn't know what he's talking about (that's
excusable) and didn't try to understand it better before going on a
pointless rant (that's not).

Could you please provide a link to said comment? Maybe some context
would help bring some sanity over this statement.


I looked it up via Google. You basically said it right.

The user has since been deleted. Every point they made had not only been 
debunked but shown to be as useful as this one, the first one.


Re: Why we cannot use string in mixins?

2016-02-27 Thread ag0aep6g via Digitalmars-d-learn

On 28.02.2016 00:29, mahdi wrote:

I read this criticism about D on Reddit and it claims that you cannot
use strings in mixins. Can you please elaborate about this and the
reason behind it?

QUOTE:
Look at strings: they are defined as immutable(char []).


immutable(char)[] actually


"immutable" as
in "you could put it in ROM", ... CTFE can't see strings correctly
(can't be determined at compile time). So rather than fix the compiler
so that strings can be used in mixins in the way that people expect it
to work even if that means making them something other than
immutable(char []), they decided to start using enums where strings
actually go. Seriously: take the same code and replace 'string "foo"'
with 'enum "foo"' and it starts to work!
END QUOTE


I can only guess that something like this is the perceived problem:


string hello(string who) {return "hello " ~ who;}
void main()
{
string w = "world";
static string hw = hello(w); /* Error: variable w cannot be read at 
compile time */

}


The poster seems to be puzzled as to why the compiler refuses to see 
that `w` is a constant. It's even marked "immutable"!


As noted above, `w` is not immutable, though. Only the elements are. 
Also, `immutable` does not mean that the value is a compile time 
constant. Immutable values can be constructed at run time, and then they 
can't be used at compile time, obviously. `enum` values, on the other 
hand, are compile time constants.


Re: Why we cannot use string in mixins?

2016-02-27 Thread cym13 via Digitalmars-d-learn

On Saturday, 27 February 2016 at 23:29:49 UTC, mahdi wrote:
I read this criticism about D on Reddit and it claims that you 
cannot use strings in mixins. Can you please elaborate about 
this and the reason behind it?


QUOTE:
Look at strings: they are defined as immutable(char []). 
"immutable" as in "you could put it in ROM", ... CTFE can't see 
strings correctly (can't be determined at compile time). So 
rather than fix the compiler so that strings can be used in 
mixins in the way that people expect it to work even if that 
means making them something other than immutable(char []), they 
decided to start using enums where strings actually go. 
Seriously: take the same code and replace 'string "foo"' with 
'enum "foo"' and it starts to work!

END QUOTE


This sounds like (please, pardon my language) a shitty load of 
bullshit from an incompetent that didn't know what he's talking 
about (that's excusable) and didn't try to understand it better 
before going on a pointless rant (that's not).


Could you please provide a link to said comment? Maybe some 
context would help bring some sanity over this statement.


Why we cannot use string in mixins?

2016-02-27 Thread mahdi via Digitalmars-d-learn
I read this criticism about D on Reddit and it claims that you 
cannot use strings in mixins. Can you please elaborate about this 
and the reason behind it?


QUOTE:
Look at strings: they are defined as immutable(char []). 
"immutable" as in "you could put it in ROM", ... CTFE can't see 
strings correctly (can't be determined at compile time). So 
rather than fix the compiler so that strings can be used in 
mixins in the way that people expect it to work even if that 
means making them something other than immutable(char []), they 
decided to start using enums where strings actually go. 
Seriously: take the same code and replace 'string "foo"' with 
'enum "foo"' and it starts to work!

END QUOTE