Re: getopt: How does arraySep work?

2020-07-16 Thread Jon Degenhardt via Digitalmars-d-learn
On Thursday, 16 July 2020 at 17:40:25 UTC, Steven Schveighoffer 
wrote:

On 7/16/20 1:13 PM, Andre Pany wrote:
On Thursday, 16 July 2020 at 05:03:36 UTC, Jon Degenhardt 
wrote:

On Wednesday, 15 July 2020 at 07:12:35 UTC, Andre Pany wrote:

[...]


An enhancement is likely to hit some corner-cases involving 
list termination requiring choices that are not fully 
generic. Any time a legal list value looks like a legal 
option. Perhaps the most important case is single digit 
numeric options like '-1', '-2'. These are legal short form 
options, and there are programs that use them. They are also 
somewhat common numeric values to include in command lines 
inputs.


[...]


My naive implementation would be that any dash would stop the 
list of multiple values. If you want to have a value 
containing a space or a dash, you enclose it with double 
quotes in the terminal.


Enclose with double quotes in the terminal does nothing:

myapp --modelicalibs "file-a.mo" "file-b.mo"

will give you EXACTLY the same string[] args as:

myapp --modelicalibs file-a.mo file-b.mo

I think Jon's point is that it's difficult to distinguish where 
an array list ends if you get the parameters as separate items.


Like:

myapp --numbers 1 2 3 -5 -6

Is that numbers=> [1, 2, 3, -5, -6]

or is it numbers=> [1, 2, 3], 5 => true, 6 => true

This is probably why the code doesn't support that.

-Steve


Yes, this what I was getting. Thanks for the clarification.

Also, it's not always immediately obvious what part of the 
argument splitting is being done by the shell, and what is being 
done by the program/getopt. Taking inspiration from the recent 
one-liners, here's way to see how the program gets the args from 
the shell for different command lines:


$ echo 'import std.stdio; void main(string[] args) { args[1 .. 
$].writeln; }' | dmd -run - --numbers 1,2,3,-5,-6

["--numbers", "1,2,3,-5,-6"]

$ echo 'import std.stdio; void main(string[] args) { args[1 .. 
$].writeln; }' | dmd -run - --numbers 1 2 3 -5 -6

["--numbers", "1", "2", "3", "-5", "-6"]

$ echo 'import std.stdio; void main(string[] args) { args[1 .. 
$].writeln; }' | dmd -run - --numbers "1" "2" "3" "-5" "-6"

["--numbers", "1", "2", "3", "-5", "-6"]

$ echo 'import std.stdio; void main(string[] args) { args[1 .. 
$].writeln; }' | dmd -run - --numbers '1 2 3 -5 -6'

["--numbers", "1 2 3 -5 -6"]

The first case is what getopt supports now - All the values in a 
single string with a separator that getopt splits on. The 2nd and 
3rd are identical from the program's perspective (Steve's point), 
but they've already been split, so getopt would need a different 
approach. And requires dealing with ambiguity. The fourth form 
eliminates the ambiguity, but puts the burden on the user to use 
quotes.


Re: getopt: How does arraySep work?

2020-07-16 Thread Steven Schveighoffer via Digitalmars-d-learn

On 7/16/20 1:13 PM, Andre Pany wrote:

On Thursday, 16 July 2020 at 05:03:36 UTC, Jon Degenhardt wrote:

On Wednesday, 15 July 2020 at 07:12:35 UTC, Andre Pany wrote:

[...]


An enhancement is likely to hit some corner-cases involving list 
termination requiring choices that are not fully generic. Any time a 
legal list value looks like a legal option. Perhaps the most important 
case is single digit numeric options like '-1', '-2'. These are legal 
short form options, and there are programs that use them. They are 
also somewhat common numeric values to include in command lines inputs.


[...]


My naive implementation would be that any dash would stop the list of 
multiple values. If you want to have a value containing a space or a 
dash, you enclose it with double quotes in the terminal.


Enclose with double quotes in the terminal does nothing:

myapp --modelicalibs "file-a.mo" "file-b.mo"

will give you EXACTLY the same string[] args as:

myapp --modelicalibs file-a.mo file-b.mo

I think Jon's point is that it's difficult to distinguish where an array 
list ends if you get the parameters as separate items.


Like:

myapp --numbers 1 2 3 -5 -6

Is that numbers=> [1, 2, 3, -5, -6]

or is it numbers=> [1, 2, 3], 5 => true, 6 => true

This is probably why the code doesn't support that.

-Steve


Re: getopt: How does arraySep work?

2020-07-16 Thread Andre Pany via Digitalmars-d-learn

On Thursday, 16 July 2020 at 05:03:36 UTC, Jon Degenhardt wrote:

On Wednesday, 15 July 2020 at 07:12:35 UTC, Andre Pany wrote:

[...]


An enhancement is likely to hit some corner-cases involving 
list termination requiring choices that are not fully generic. 
Any time a legal list value looks like a legal option. Perhaps 
the most important case is single digit numeric options like 
'-1', '-2'. These are legal short form options, and there are 
programs that use them. They are also somewhat common numeric 
values to include in command lines inputs.


[...]


My naive implementation would be that any dash would stop the 
list of multiple values. If you want to have a value containing a 
space or a dash, you enclose it with double quotes in the 
terminal.



myapp --modelicalibs "fila-a.mo" "file-b.mo" --log-level info


But you are right there a corner cases to be considered.

Kind regards
Andre


Re: getopt: How does arraySep work?

2020-07-15 Thread Jon Degenhardt via Digitalmars-d-learn

On Wednesday, 15 July 2020 at 07:12:35 UTC, Andre Pany wrote:

On Tuesday, 14 July 2020 at 15:48:59 UTC, Andre Pany wrote:
On Tuesday, 14 July 2020 at 14:33:47 UTC, Steven Schveighoffer 
wrote:

On 7/14/20 10:22 AM, Steven Schveighoffer wrote:
The documentation needs updating, it should say "parameters 
are added sequentially" or something like that, instead of 
"separation by whitespace".


https://github.com/dlang/phobos/pull/7557

-Steve


Thanks for the answer and the pr. Unfortunately my goal here 
is to simulate a partner tool written  in C/C++ which supports 
this behavior. I will also create an enhancement issue for 
supporting this behavior.


Kind regards
Anste


Enhancement issue:
https://issues.dlang.org/show_bug.cgi?id=21045

Kind regards
André


An enhancement is likely to hit some corner-cases involving list 
termination requiring choices that are not fully generic. Any 
time a legal list value looks like a legal option. Perhaps the 
most important case is single digit numeric options like '-1', 
'-2'. These are legal short form options, and there are programs 
that use them. They are also somewhat common numeric values to 
include in command lines inputs.


I ran into a couple cases like this with a getopt cover I wrote. 
The cover supports runtime processing of command arguments in the 
order entered on the command line rather than the compile-time 
getopt() call order. Since it was only for my stuff, not Phobos, 
it was an easy choice: Disallow single digit short options. But a 
Phobos enhancement might make other choices.


IIRC, a characteristic of the current getopt implementation is 
that it does not have run-time knowledge of all the valid 
options, so the set of ambiguous entries is larger than just the 
limited set of options specified in the program. Essentially, 
anything that looks syntactically like an option.


Doesn't mean an enhancement can't be built, just that there might 
some constraints to be aware of.


--Jon




Re: getopt: How does arraySep work?

2020-07-15 Thread Andre Pany via Digitalmars-d-learn

On Tuesday, 14 July 2020 at 15:48:59 UTC, Andre Pany wrote:
On Tuesday, 14 July 2020 at 14:33:47 UTC, Steven Schveighoffer 
wrote:

On 7/14/20 10:22 AM, Steven Schveighoffer wrote:
The documentation needs updating, it should say "parameters 
are added sequentially" or something like that, instead of 
"separation by whitespace".


https://github.com/dlang/phobos/pull/7557

-Steve


Thanks for the answer and the pr. Unfortunately my goal here is 
to simulate a partner tool written  in C/C++ which supports 
this behavior. I will also create an enhancement issue for 
supporting this behavior.


Kind regards
Anste


Enhancement issue:
https://issues.dlang.org/show_bug.cgi?id=21045

Kind regards
André


Re: getopt: How does arraySep work?

2020-07-14 Thread Andre Pany via Digitalmars-d-learn
On Tuesday, 14 July 2020 at 14:33:47 UTC, Steven Schveighoffer 
wrote:

On 7/14/20 10:22 AM, Steven Schveighoffer wrote:
The documentation needs updating, it should say "parameters 
are added sequentially" or something like that, instead of 
"separation by whitespace".


https://github.com/dlang/phobos/pull/7557

-Steve


Thanks for the answer and the pr. Unfortunately my goal here is 
to simulate a partner tool written  in C/C++ which supports this 
behavior. I will also create an enhancement issue for supporting 
this behavior.


Kind regards
Anste


Re: getopt: How does arraySep work?

2020-07-14 Thread Steven Schveighoffer via Digitalmars-d-learn

On 7/14/20 10:22 AM, Steven Schveighoffer wrote:
The documentation needs updating, it should say "parameters are added 
sequentially" or something like that, instead of "separation by 
whitespace".


https://github.com/dlang/phobos/pull/7557

-Steve


Re: getopt: How does arraySep work?

2020-07-14 Thread Steven Schveighoffer via Digitalmars-d-learn

On 7/14/20 10:05 AM, Steven Schveighoffer wrote:

Hm... that looks like it IS actually expecting to do what Andre wants. 
It's adding each successive parameter.


If that doesn't work, then there's something wrong with the logic that 
decides whether a parameter is part of the previous argument or not.


Please file a bug.


Belay that, the behavior is as designed, I think the issue is the 
documentation.


If arraySep is "", then it's not "separation by whitespace", but rather 
you must repeat the parameter and each one is appended to the array:


dmd -run sample.d --modelicalibs a --modelicalibs b

If you want to specify all the parameters in one, you have to provide an 
arraySep.


The documentation needs updating, it should say "parameters are added 
sequentially" or something like that, instead of "separation by whitespace".


-Steve


Re: getopt: How does arraySep work?

2020-07-14 Thread Steven Schveighoffer via Digitalmars-d-learn

On 7/14/20 9:51 AM, Anonymouse wrote:

On Tuesday, 14 July 2020 at 11:12:06 UTC, Andre Pany wrote:

[...]


Steven Schveighoffer already answered while I was composing this, so 
discarding top half.


As far as I can tell the default arraySep of "" splitting the argument 
by whitespace is simply not the case.



https://github.com/dlang/phobos/blob/master/std/getopt.d#L923


     // ...
     else static if (isArray!(typeof(*receiver)))
     {
     // array receiver
     import std.range : ElementEncodingType;
     alias E = ElementEncodingType!(typeof(*receiver));

     if (arraySep == "")
     {
     *receiver ~= to!E(val);
     }
     else
     {
     foreach (elem; val.splitter(arraySep).map!(a => to!E(a))())
     *receiver ~= elem;
     }
     }

So you will probably want an arraySep of " " if you want --modelicalibs 
"a b".


Hm... that looks like it IS actually expecting to do what Andre wants. 
It's adding each successive parameter.


If that doesn't work, then there's something wrong with the logic that 
decides whether a parameter is part of the previous argument or not.


Please file a bug.

-Steve


Re: getopt: How does arraySep work?

2020-07-14 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 14 July 2020 at 13:40:44 UTC, Steven Schveighoffer 
wrote:


The whitespace separator doesn't get to your program. args is:

["sample", "--modelicalibs", "a", "b"]

There is no separator in the parameter to --modelicalibs, it's 
just "a".


What you need to do is:

dmd -run sample.d --modilicalibs "a b"

-Steve


I thought this was the solution too, but when I actually tried 
it, I got `modelicaLibs == ["a b"]` and the assertion still 
failed.


Re: getopt: How does arraySep work?

2020-07-14 Thread Anonymouse via Digitalmars-d-learn

On Tuesday, 14 July 2020 at 11:12:06 UTC, Andre Pany wrote:

[...]


Steven Schveighoffer already answered while I was composing this, 
so discarding top half.


As far as I can tell the default arraySep of "" splitting the 
argument by whitespace is simply not the case.



https://github.com/dlang/phobos/blob/master/std/getopt.d#L923


// ...
else static if (isArray!(typeof(*receiver)))
{
// array receiver
import std.range : ElementEncodingType;
alias E = ElementEncodingType!(typeof(*receiver));

if (arraySep == "")
{
*receiver ~= to!E(val);
}
else
{
foreach (elem; val.splitter(arraySep).map!(a => 
to!E(a))())

*receiver ~= elem;
}
}

So you will probably want an arraySep of " " if you want 
--modelicalibs "a b".


Re: getopt: How does arraySep work?

2020-07-14 Thread Steven Schveighoffer via Digitalmars-d-learn

On 7/14/20 7:12 AM, Andre Pany wrote:

Hi,

by reading the documentation of std.getopt I would assume, this is a 
valid call


dmd -run sample.d --modelicalibs a b

``` d
import std;

void main(string[] args)
{
     string[] modelicaLibs;
     getopt(args, "modelicalibs", );
     assert(modelicaLibs == ["a", "b"]);
}
```

but it fails, as array modelicaLIbs only contains ["a"].

The std.getopt : arraySep documentation hints that it should work:
The string used to separate the elements of an array or associative 
array (default is "" which means the elements are separated by 
whitespace).


Is my understanding wrong, or is this a bug?


The whitespace separator doesn't get to your program. args is:

["sample", "--modelicalibs", "a", "b"]

There is no separator in the parameter to --modelicalibs, it's just "a".

What you need to do is:

dmd -run sample.d --modilicalibs "a b"

-Steve


getopt: How does arraySep work?

2020-07-14 Thread Andre Pany via Digitalmars-d-learn

Hi,

by reading the documentation of std.getopt I would assume, this 
is a valid call


dmd -run sample.d --modelicalibs a b

``` d
import std;

void main(string[] args)
{
string[] modelicaLibs;
getopt(args, "modelicalibs", );
assert(modelicaLibs == ["a", "b"]);
}
```

but it fails, as array modelicaLIbs only contains ["a"].

The std.getopt : arraySep documentation hints that it should work:
The string used to separate the elements of an array or 
associative array (default is "" which means the elements are 
separated by whitespace).


Is my understanding wrong, or is this a bug?

Kind regards
André





How does this work?

2014-11-23 Thread Freddy via Digitalmars-d-learn

I know what this does, but can someone explain how it works?

static if((typeof((inout int=0){

})));



Re: How does this work?

2014-11-23 Thread ketmar via Digitalmars-d-learn
On Sun, 23 Nov 2014 22:51:48 +
Freddy via Digitalmars-d-learn digitalmars-d-learn@puremagic.com
wrote:

 I know what this does, but can someone explain how it works?
 
 static if((typeof((inout int=0){
 
 })));
 
it was here somewhere. this is, as you can see, a lambda. `typeof()`
can be used even for invalid code and will return special `error` type
(don't try to catch it, just trust me ;-). in `static if` this `error`
type means `false`. labmda that can not be compiled is obvious invalid,
uncompilable code, so it has a type of `error`.

and `inout` is a hack for some kind of functions/templates.

to make a long story short: don't try to remember it all, you'll forget
it next day. just take it as it is and be happy. ;-)


signature.asc
Description: PGP signature


Re: How does GC.addRange work?

2014-09-24 Thread kiran kumari via Digitalmars-d-learn

On Saturday, 20 September 2014 at 20:14:36 UTC, Gary Willoughby
wrote:
How does GC.addRange work? i.e. what is it doing? I'm assuming 
reading the docs that it adds a range for the GC to scan but 
what actually happens? Does the GC look into this range and 
check for the existence of pointers it's currently managing?


For example, if i nulled a pointer in the range i added would 
that trigger the GC to collect that resource on the next sweep? 
(assuming it was the last reference.)


see more example
http://techgurulab.com/course/java-quiz-online/


Re: How does GC.addRange work?

2014-09-22 Thread Steven Schveighoffer via Digitalmars-d-learn

On 9/21/14 3:00 PM, Gary Willoughby wrote:

On Saturday, 20 September 2014 at 23:08:08 UTC, ketmar via
Digitalmars-d-learn wrote:

On Sat, 20 Sep 2014 22:21:13 +
Gary Willoughby via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com wrote:


So zeroing values will inform the GC the reference has gone?

yes.


Thanks, i just wanted to make it clear in my mind.


Just to be crystal clear, zeroing values in that range will make the GC 
able to collect the memory that those values previously pointed at. 
However, you have to remove the range in order for the GC to ignore that 
data. In other words, if you zero that memory, the GC will continue to 
scan those zeros until you GC.removeRange it.


-Steve


Re: How does GC.addRange work?

2014-09-21 Thread Gary Willoughby via Digitalmars-d-learn
On Saturday, 20 September 2014 at 23:08:08 UTC, ketmar via 
Digitalmars-d-learn wrote:

On Sat, 20 Sep 2014 22:21:13 +
Gary Willoughby via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com wrote:


So zeroing values will inform the GC the reference has gone?

yes.


Thanks, i just wanted to make it clear in my mind.


How does GC.addRange work?

2014-09-20 Thread Gary Willoughby via Digitalmars-d-learn
How does GC.addRange work? i.e. what is it doing? I'm assuming 
reading the docs that it adds a range for the GC to scan but what 
actually happens? Does the GC look into this range and check for 
the existence of pointers it's currently managing?


For example, if i nulled a pointer in the range i added would 
that trigger the GC to collect that resource on the next sweep? 
(assuming it was the last reference.)


Re: How does GC.addRange work?

2014-09-20 Thread ketmar via Digitalmars-d-learn
On Sat, 20 Sep 2014 20:14:35 +
Gary Willoughby via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com wrote:

 How does GC.addRange work? i.e. what is it doing? I'm assuming 
 reading the docs that it adds a range for the GC to scan but what 
 actually happens? Does the GC look into this range and check for 
 the existence of pointers it's currently managing?
yes. this adds GC root. but normal GC root is just a single pointer,
and range root as a memory region that will be scanned for pointers
(i.e. something like array of pointers).

note that scan is conservative, so if you happen to have some integer
value that can be interpreted as pointer to GC-managed memory, it will
be considered as pointer.


signature.asc
Description: PGP signature


Re: How does GC.addRange work?

2014-09-20 Thread ketmar via Digitalmars-d-learn
On Sat, 20 Sep 2014 22:21:13 +
Gary Willoughby via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com wrote:

 So zeroing values will inform the GC the reference has gone?
yes.


signature.asc
Description: PGP signature


How does noexcept work?

2013-11-19 Thread Jeroen Bollen

If I have a function:
@safe pure void functionName() {
return;
}
Where do I put the noexcept?


Re: How does noexcept work?

2013-11-19 Thread Namespace

On Tuesday, 19 November 2013 at 18:01:19 UTC, Jeroen Bollen wrote:

If I have a function:
@safe pure void functionName() {
return;
}
Where do I put the noexcept?


Did you mean nothrow?
You can put it unfortunately on both sides.
left:

@safe pure nothrow void functionName() {
return;
}

right:
@safe pure void functionName() nothrow {
return;
}


Re: How does noexcept work?

2013-11-19 Thread Jeroen Bollen

On Tuesday, 19 November 2013 at 18:09:29 UTC, Namespace wrote:
On Tuesday, 19 November 2013 at 18:01:19 UTC, Jeroen Bollen 
wrote:

If I have a function:
@safe pure void functionName() {
return;
}
Where do I put the noexcept?


Did you mean nothrow?
You can put it unfortunately on both sides.
left:

@safe pure nothrow void functionName() {
return;
}

right:
@safe pure void functionName() nothrow {
return;
}


Thanks, don't mind me being stupid. I was getting confused with 
C++. *facepalm*


How does immutable work.

2012-08-09 Thread egslava

Hello! Sorry for my English.

I read manual about immutable and const keyword:
http://dlang.org/const3.html

And tried to build my program:
http://dpaste.dzfl.pl/f803ae94

If I will change immutable to const output will not changed.
But why? Why output is look like this?

I would understand the output like this:
10
10
7FFF7E68AEB0
7FFF7E68AEB0

In this case, I would think - we just ignoring all assign 
operators.


I would understand the output like this:
10
20
7FFF7E68AEB0
F

In this case, I would think - we just one more time located 
memory and copied value of variable.


But why the output does look like this? How does this 
construction work? Why memory addresses are same, assign is 
working, but value of immutable variable doesn't change?


I would even think, that in compile-time compiler changes
writeln(x) to writeln(10), but why we can dereference x?


Re: How does immutable work.

2012-08-09 Thread Simen Kjaeraas

On Thu, 09 Aug 2012 19:25:47 +0200, egslava egsl...@gmail.com wrote:


Hello! Sorry for my English.

I read manual about immutable and const keyword:
http://dlang.org/const3.html

And tried to build my program:
http://dpaste.dzfl.pl/f803ae94

If I will change immutable to const output will not changed.
But why? Why output is look like this?

I would understand the output like this:
10
10
7FFF7E68AEB0
7FFF7E68AEB0

In this case, I would think - we just ignoring all assign operators.

I would understand the output like this:
10
20
7FFF7E68AEB0
F

In this case, I would think - we just one more time located memory and  
copied value of variable.


But why the output does look like this? How does this construction work?  
Why memory addresses are same, assign is working, but value of immutable  
variable doesn't change?


I would even think, that in compile-time compiler changes
writeln(x) to writeln(10), but why we can dereference x?


The compiler has optimized the call to writeln(x), by assuming x will never
change (which is reasonable). Now, since x is a variable, not a manifest
constant (which you would get by writing enum x = 10;, for instance), it
has a memory location, so you can ask for its address.

The reason you get the same value for both y and x, is that they point to
the same location, and the first writeln call simply ignores the value
stored there.

Finally, the reason this does not behave like you'd expect is because
casts are a deliberate hole in the type system, and casting away  
immutability
leads to undefined behavior (in this case, changing the value of the  
immutable

variable does not change the value where that variable is used).

--
Simen


Re: How does immutable work.

2012-08-09 Thread Ali Çehreli

On 08/09/2012 10:25 AM, egslava wrote:
 Hello! Sorry for my English.

Thank you for using English.

 I read manual about immutable and const keyword:
 http://dlang.org/const3.html

 And tried to build my program:
 http://dpaste.dzfl.pl/f803ae94

Here it is:

import std.stdio;

void main(string[] args)
{
immutable int x = 10;

int *y = cast(int*) x;
*y += 10;

writeln(x);
writeln(*y);

writeln(x);
writeln(y);

}

You are subverting the type system by treating an immutable variable as 
mutable.


 If I will change immutable to const output will not changed.

immutable and const are the same thing on a variable. I have written my 
understanding about this topic here:


  http://ddili.org/ders/d.en/const_and_immutable.html

I have unpublished amendments to that page. I am going to add this section:

=
Should a parameter be const or immutable?

The two sections above may give the impression that const should be 
preferred over immutable because it is more flexible.


This is not always true because const erases the information about 
whether the original variable is mutable or immutable. This information 
is hidden even from the compiler.


A consequence of this fact is that const parameters cannot be passed as 
arguments to functions that take immutable parameters. The following 
code produces a compilation error related to this fact:


void main()
{
/* The original variable is immutable */
immutable int[] slice = [ 10, 20, 30, 40 ];
foo(slice);
}

/* A function that takes its parameter as const, in order to
 * be more useful. */
void foo(const int[] slice)
{
bar(slice);// ← compilation ERROR
}

/* A function that takes its parameter as immutable for a
 * plausible reason. */
void bar(immutable int[] slice)
{
/* ... */
}

The code cannot be compiled as it is not known whether the original 
variable that foo()'s const parameter references is immutable or not.


Note: It is clear in the code above that the original variable in main() 
is immutable. However, the compiler compiles functions individually 
without regard to all of the places that function is called from. 
According to the compiler, the slice parameter of foo() may refer to a 
mutable variable, as well as an immutable one.


A solution would be to call bar() with an immutable copy of the parameter:

void foo(const int[] slice)
{
bar(slice.idup);
}

Although that is a sensible solution, it does have the cost of copying, 
which would be wasteful in the case where the original variable has been 
immutable to begin with.


After this analysis, it should be clear that taking parameters always as 
const does not seem to be the best approach in every situation. After 
all, if foo()'s parameter has been defined as immutable, there would not 
be any need to copy it before calling bar():


void foo(immutable int[] slice) // This time immutable
{
bar(slice);// Copying is not needed anymore
}

The consequence of that choice is having to make an immutable copy of 
the original variable when calling foo(), if that variable were not 
immutable to begin with:


foo(mutableSlice.idup);

The decision of whether a parameter should be marked as const or 
immutable is not always easy.


Templates can provide some help with this decision. (We will see 
templates in later chapters.) Although I don't expect you to fully 
understand the following function at this point in the book, I will show 
it as a solution to this very problem. The following function template 
foo() can be called both by mutable variables and by immutable 
variables. The parameter would be copied only if the original variable 
has been mutable; no copying would take place if it has been immutable:


import std.conv;
// ...

/* Because it is a template, foo() can be called by mutable
 * and immutable variables. */
void foo(T)(T[] slice)
{
/* 'to()' does not make a copy if the original variable is
 * already immutable. */
bar(to!(immutable T[])(slice));
}
=

 But why? Why output is look like this?

Here is the output:

10
20
7FFF62F454F0
7FFF62F454F0

 I would understand the output like this:
 10
 10
 7FFF7E68AEB0
 7FFF7E68AEB0

For that to make sense, the assignment through y should have been 
ignored. Would that make sense? What should the compiler favor?


 In this case, I would think - we just ignoring all assign operators.

I don't think the assignments are being ignored. Since you told the 
compiler that x is immutable, it can use its value instead of reading 
from memory every time. Reading from the memory would be unnecessary, 
right? It is immutable after all.


 I would understand the output like this:
 10
 20
 7FFF7E68AEB0
 F

That would not make sense. That doesn't look like a valid address to me.

 In this case, I would think - we just one more time located memory and
 copied value of variable.

 But why the output does look like this? How does this construction work

Re: How does immutable work.

2012-08-09 Thread David Nadlinger

On Thursday, 9 August 2012 at 17:25:48 UTC, egslava wrote:

If I will change immutable to const output will not changed.
But why? Why output is look like this?


Just to repeat the gist of it: immutable is a guarantee that the 
value stored in the variable will never change, and the compiler 
is free to assume that when optimizing code. By using a cast and 
then writing to the location, you have subverted the type system, 
so all bets are off – in fact, your program is experiencing 
undefined behavior.


David


Re: How does immutable work.

2012-08-09 Thread Chris Cain
In addition to what everyone else said, I think it's important to 
also say that, in general, you should _not_ cast away immutable 
or const like you did. As far as I'm concerned, it's a 
programming error. It's possible that future compilers might  
have immutable data stored in Read-Only Memory and forcing a 
change like this could potentially crash your program.


How does this work ?

2012-04-19 Thread Somedude
I'm going through a number of bug reports, trying to reproduce the
problems and see what can be closed easily (i.e non reproduced, correct
behaviour, etc), and I just came accross
http://d.puremagic.com/issues/show_bug.cgi?id=7326 titled
write interprets enum with byte backing type as a character

Here is the case description:

import std.stdio;

enum X : byte
{
Foobar = 65,
}

void main()
{
X x;
writeln(x);  // writes 'A'
writeln(cast(byte)x);  // writes 65
}



That's it.
When I run this on Win32, I get:
Foo
65

Can anyone explain me if it is the correct behaviour, and if yes, why ?
Thx.



Re: How does this work ?

2012-04-19 Thread Andrej Mitrovic
On 4/19/12, Somedude lovelyd...@mailmetrash.com wrote:
 Can anyone explain me if it is the correct behaviour, and if yes, why ?

It's fixed now and we can close this down. I think it was related to
formatting issues, that's all.


Re: how does range.put work

2009-08-08 Thread Jos van Uden

Oliver wrote:

The source code for the standard library comes with the compiler.
If you look in std\array.d, you find this around line 279 (reflowed for
readability):

void put(T, E)(ref T[] a, E e) {
assert(a.length);
a[0] = e; a = a[1 .. $];
}


Would anybody care to explain what this is used for? I find
the example in array.d rather unhelpful.

Example:

void main()
{
int[] a = [ 1, 2, 3 ];
int[] b = a;
a.put(5);
assert(a == [ 2, 3 ]);
assert(b == [ 5, 2, 3 ]);
}

You're putting an element in a, but then the first element is moved out 
of a and the new one shows up in b? Weird. I guess I don't understand 
what a range is.


Jos




Re: how does range.put work

2009-08-08 Thread Daniel Keep


Jos van Uden wrote:
 Oliver wrote:
 The source code for the standard library comes with the compiler.
 If you look in std\array.d, you find this around line 279 (reflowed for
 readability):
 void put(T, E)(ref T[] a, E e) {
 assert(a.length);
 a[0] = e; a = a[1 .. $];
 }
 
 Would anybody care to explain what this is used for? I find
 the example in array.d rather unhelpful.
 
 Example:
 
 void main()
 {
 int[] a = [ 1, 2, 3 ];
 int[] b = a;
 a.put(5);
 assert(a == [ 2, 3 ]);
 assert(b == [ 5, 2, 3 ]);
 }
 
 You're putting an element in a, but then the first element is moved out
 of a and the new one shows up in b? Weird. I guess I don't understand
 what a range is.
 
 Jos

No; read the code.  Before the put, a and b are pointing to the same
span of memory.  a.put(5) puts the value 5 into the front (first
element) of the array, then advances the array.

However, put can't see b, so it doesn't get updated along with a.  The
end result is that b = [5,2,3] and a = b[1..3] = [2,3].

Why do it like this?  Here's an example:

void putNumbers(Range)(Range r)
{
int i = 0;
while( !r.empty )
{
r.put(i);
++i;
}
}

void main()
{
int[10] ten_numbers;
putNumbers(ten_numbers);
assert( ten_numbers = [0,1,2,3,4,5,6,7,8,9] );
}

Note that putNumbers will work with any type that supports the range
API, not just arrays.


Re: how does range.put work

2009-08-08 Thread Jos van Uden

Daniel Keep wrote:


No; read the code.  Before the put, a and b are pointing to the same
span of memory.  a.put(5) puts the value 5 into the front (first
element) of the array, then advances the array.

However, put can't see b, so it doesn't get updated along with a.  The
end result is that b = [5,2,3] and a = b[1..3] = [2,3].

Why do it like this?  Here's an example:

void putNumbers(Range)(Range r)
{
int i = 0;
while( !r.empty )
{
r.put(i);
++i;
}
}

void main()
{
int[10] ten_numbers;
putNumbers(ten_numbers);
assert( ten_numbers = [0,1,2,3,4,5,6,7,8,9] );
}


I see.

Your example should be in the documentation in my opinion, rather
then the meaningless one that's there now. Something like this
perhaps:

void putNumbers(Range, T)(Range r, T start, T incr) {
T i = start;
while( !r.empty )
{
r.put(i);
i += incr;
}
}

void main() {
   int[10] ten_ints;
   putNumbers!(int[])(ten_ints, 4, 2);
   assert( ten_ints == [4,6,8,10,12,14,16,18,20,22] );
}

Jos




Re: how does range.put work

2009-08-07 Thread O.K.
Good point ! Use the [S/F]o[u]rce !
Thx, Oliver

 O.K. wrote:
  Hello,
  could someone plz clearify what the exact semantics of put
  are ?
  Put works with an appender, but gives me a runtime exception
  when using an array.
 
  Best regards,
  Oliver
 The source code for the standard library comes with the compiler.
 If you look in std\array.d, you find this around line 279 (reflowed for
 readability):
  void put(T, E)(ref T[] a, E e) {
  assert(a.length);
  a[0] = e; a = a[1 .. $];
  }



how does range.put work

2009-08-06 Thread O.K.
Hello,
could someone plz clearify what the exact semantics of put
are ?
Put works with an appender, but gives me a runtime exception
when using an array.

Best regards,
Oliver


Re: how does range.put work

2009-08-06 Thread Daniel Keep


O.K. wrote:
 Hello,
 could someone plz clearify what the exact semantics of put
 are ?
 Put works with an appender, but gives me a runtime exception
 when using an array.
 
 Best regards,
 Oliver

The source code for the standard library comes with the compiler.

If you look in std\array.d, you find this around line 279 (reflowed for
readability):

 void put(T, E)(ref T[] a, E e) {
 assert(a.length);
 a[0] = e; a = a[1 .. $];
 }