Re: Acess variable that was set by thread

2022-08-07 Thread Emanuele Torre via Digitalmars-d-learn

On Monday, 8 August 2022 at 01:36:45 UTC, vc wrote:
Hello, i have the following code, the flora contains a boolean 
zeus
in the DerivedThread the boolean zeus was set to true; but when 
i'm trying to access it

outside the thread in main it returns me false; any thoughts ?

import flora;

class DerivedThread : Thread
{
this()
{
super();
}

private:
void run()
{
// Derived thread running.
zeus = true;

while(true)
{

}

}
}




void main()
{

auto derived = new DerivedThread().start();

writeln(zeus);

}


When `writeln(zeus)` runs, `zeus = true` probably was not 
evaluated yet.


Re: Verbosity in D

2022-08-07 Thread Emanuele Torre via Digitalmars-d-learn

On Monday, 8 August 2022 at 00:15:48 UTC, pascal111 wrote:

On Sunday, 7 August 2022 at 23:44:26 UTC, Emanuele Torre wrote:

On Sunday, 7 August 2022 at 23:31:45 UTC, pascal111 wrote:
On Sunday, 7 August 2022 at 22:16:55 UTC, Emanuele Torre 
wrote:

[...]


It seems complex, I didn't get it yet, I wished I didn't ask 
about it :)


It's really trivial.
`auto [x, y] = getpoint();` is equivalent to
```C++
auto _p = getpoint();
auto x = _p.x /* first element of the struct */;
auto y = _p.y /* second element of the struct */;`
```
Also works with arrays.
```C++
int[] arr = { 10, 12, 14 };
const auto [x, y, z] = arr;
/* x=10 y=12 z=14 */
```
A lot of programming languages have this.


Really, I'm not sure I'm understanding this syntax `auto [x, y] 
= getpoint();`, if you have the name of term of it in D or an 
explaining link.


D does not have it. You have already been told that when they 
have been mentioned.
If you want more information about it in C++, you could read 
https://en.cppreference.com/w/cpp/language/structured_binding or, 
for javascript, 
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment.


That was just meant to be a simple example to show what jfondren 
meant with "destructing bindings".


Re: Verbosity in D

2022-08-07 Thread Emanuele Torre via Digitalmars-d-learn

On Monday, 8 August 2022 at 00:11:33 UTC, pascal111 wrote:

On Sunday, 7 August 2022 at 23:53:36 UTC, Emanuele Torre wrote:

On Sunday, 7 August 2022 at 16:01:08 UTC, pascal111 wrote:
It's clear by working with D that it has the same bad point 
like Pascal language; the "verbosity". Is there any plans in 
future to make some shorthanded techniques that clean 
verbosity from D?


Quote: "In terms of functionality, Pascal is pretty much 
exactly the same as C, except with some sanity-conserving 
restrictions on one hand, and more verbose syntax on the 
other. It was an okay language for the time when it was 
popular, and I would give it kudos just for having the common 
sense to make the assignment operator := instead of =, and 
not allowing it to be chained, but verbosity back then was 
still something to be avoided if possible, so C was naturally 
seen as superior."


https://www.quora.com/Is-there-a-value-learning-Pascal-now-Are-there-actually-any-parts-where-Pascal-is-better-than-C-Is-this-language-worth-investing-time-into-What-would-the-added-value-be-if-I-learn-it


Regaring this, I don't understand what you mean either.
How is D unnecesarily verbose?
Do you have any specific example?


I don't have specific code but it was a general notice. Take 
Python as in example, the same program in Python doesn't cost 
much code as D code, and of course by putting in accounts that 
that I assume that there are some special tasks D can do, while 
Python can't do.


You are just sounding like a troll now...

That makes no sense:

 "I assume that there are some special tasks D can do, while 
Python can't do"

 How?

 "Take Python as in example, the same program in Python doesn't 
cost much code as D code"
 What same program are you talking about? Why did you mention 
python as if you showed me an example I can see?


What am I supposed to say if can't even explain or show an 
example of how D is more verbose?




Re: Supporting Arabic in GUI

2022-08-07 Thread Emanuele Torre via Digitalmars-d-learn

On Sunday, 7 August 2022 at 23:48:22 UTC, pascal111 wrote:
I have no idea about GUI or Rad programming in D; it's not its 
time, but I'm curious to know if D is fine supporting for 
Arabic language in the GUI applications or we will have some 
issues like I met - in my experience - in Free Pascal.


This is a topic where we trying to make a custom message box 
supporting Arabic as what's supposed to be, but we didn't reach 
although that the degree we want:


https://forum.lazarus.freepascal.org/index.php/topic,59765.0.html


I am very confused by this question.

That has nothing to do with the programming language: it has all 
to do with the GUI toolkit you use (lazarus in that case).


If you used lazarus with D instead of Pascal (if it is possible), 
you would have the same limitations in D.


If you use another GUI toolkit in D/Pascal (e.g. GTK3, Qt5, Tk, 
etc.), you will have the limitations of that toolkit, which may 
or may not support Arabic text.


Also I don't understand you saying "it's not its time"; there are 
plenty of GUI programs written in D. :/


Re: Verbosity in D

2022-08-07 Thread Emanuele Torre via Digitalmars-d-learn

On Sunday, 7 August 2022 at 16:01:08 UTC, pascal111 wrote:
It's clear by working with D that it has the same bad point 
like Pascal language; the "verbosity". Is there any plans in 
future to make some shorthanded techniques that clean verbosity 
from D?


Quote: "In terms of functionality, Pascal is pretty much 
exactly the same as C, except with some sanity-conserving 
restrictions on one hand, and more verbose syntax on the other. 
It was an okay language for the time when it was popular, and I 
would give it kudos just for having the common sense to make 
the assignment operator := instead of =, and not allowing it to 
be chained, but verbosity back then was still something to be 
avoided if possible, so C was naturally seen as superior."


https://www.quora.com/Is-there-a-value-learning-Pascal-now-Are-there-actually-any-parts-where-Pascal-is-better-than-C-Is-this-language-worth-investing-time-into-What-would-the-added-value-be-if-I-learn-it


Regaring this, I don't understand what you mean either.
How is D unnecesarily verbose?
Do you have any specific example?


Re: Verbosity in D

2022-08-07 Thread Emanuele Torre via Digitalmars-d-learn

On Sunday, 7 August 2022 at 23:44:26 UTC, Emanuele Torre wrote:


int[] arr = { 10, 12, 14 };


Oops, this is C++, not D: `int arr[] = { 10, 12, 14 };` =)



Re: Verbosity in D

2022-08-07 Thread Emanuele Torre via Digitalmars-d-learn

On Sunday, 7 August 2022 at 23:31:45 UTC, pascal111 wrote:

On Sunday, 7 August 2022 at 22:16:55 UTC, Emanuele Torre wrote:

On Sunday, 7 August 2022 at 20:15:05 UTC, pascal111 wrote:

What destructuring binds? I didn't hear about that before.


```C++
#include 

struct Point {
int x, y;
};

Point add_points(const Point& a, const Point& b)
{
return { a.x + b.x, a.y + b.y };
}

int main()
{
const auto [x, y] = add_points({ 1, 10 }, { -60, 40 });
std::cout << "x is: " << x << '\n';
std::cout << "y is: " << y << '\n';
}
```


It seems complex, I didn't get it yet, I wished I didn't ask 
about it :)


It's really trivial.
`auto [x, y] = getpoint();` is equivalent to
```C++
auto _p = getpoint();
auto x = _p.x /* first element of the struct */;
auto y = _p.y /* second element of the struct */;`
```
Also works with arrays.
```C++
int[] arr = { 10, 12, 14 };
const auto [x, y, z] = arr;
/* x=10 y=12 z=14 */
```
A lot of programming languages have this.


Re: Verbosity in D

2022-08-07 Thread Emanuele Torre via Digitalmars-d-learn

On Sunday, 7 August 2022 at 20:15:05 UTC, pascal111 wrote:

What destructuring binds? I didn't hear about that before.


```C++
#include 

struct Point {
int x, y;
};

Point add_points(const Point& a, const Point& b)
{
return { a.x + b.x, a.y + b.y };
}

int main()
{
const auto [x, y] = add_points({ 1, 10 }, { -60, 40 });
std::cout << "x is: " << x << '\n';
std::cout << "y is: " << y << '\n';
}
```


Re: Ranges

2022-08-07 Thread Emanuele Torre via Digitalmars-d-learn

On Saturday, 6 August 2022 at 15:37:32 UTC, pascal111 wrote:

On Friday, 5 August 2022 at 04:05:08 UTC, Salih Dincer wrote:

On Thursday, 4 August 2022 at 22:54:42 UTC, pascal111 wrote:


I didn't notice that all what we needs to pop a range forward 
is just a slice, yes, we don't need variable here.


Ranges and Slices are not the same thing. Slicing an array is 
easy. This is a language possibility. For example, you need an 
incrementing variable for the Fibonacci Series.


SDB@79


What!!! so where's ranges?! I thought slices of any array are 
ranges, and understood it like that, and also there's no data 
type called ranges, it's like if you are talking about Ghostly 
data type!


A range is like an iterator in any other language (Java, C++, 
python3, javascript, etc), it is how D implements (lazy) 
generators https://en.wikipedia.org/wiki/Lazy_evaluation .


Ranges/Iterators don't necessarily have to be backed by memory, 
they just have to implement the interface. In D, a `empty` bool 
function that tells you whether you are at the end of the range 
or not; a `front` function to get the current value if the range 
is not `empty`; and a void function named `popFront` to advance 
to the next value if the range is not `empty`.


Once you have implemented this interface, you can use your 
"range" object with any function that accept a range; with 
`foreach`; etc.


Example of a range that is not backed by memory is a range with 
all the integer numbers.


```D
struct Integers {
  private int z = 0;

  /* or make it a bool attribute that starts as false, and you 
set to

   * true when popFront is called while z is equal to int.min */
  public bool empty() { return false; }

  public int front() { return this.z; }

  public void popFront()
  {
/* if (this.z == int.min) { this.empty = false; return; } */
this.z *= -1;
if (this.z <= 0)
  --this.z;
  }
}

void main()
{
  import std.stdio : writeln;

  /* foreach is syntax sugar for
   *   for (auto r = Integers(); !r.empty(); r.popFront()) {
   * auto z = r.front(); /+ or  const z = r.front();  or ... 
+/

   * ...
   *   }
   * that is why it only works with ranges.
   */
  foreach (const z; Integers()) {
writeln(z);
if (z == 5)
  break;
  }
}
```

output:
```
0
-1
1
-2
2
-3
3
-4
4
-5
5
```

This will iterate all the integers, and the integers are of 
course, not
all in memory, and don't remain in memory after they are used, 
since
that would require infinite memory. (in the case of a range of 
integers,
not infinite, because they are constrained by being int.sizeof 
bytes,
but you could use a bignum implemenation that is not constrained 
by

that and they would actually be infinite.)

---

The equivalent in Java is the Iterable/Iterator interface.

```java
import java.util.Iterator;

public class Integers
  implements Iterable
{
  public class IntegersIterator
implements Iterator
  {
private int z = 0;
private boolean first = true;

public IntegersIterator(Integer z)
{
  this.z = z;
}

@Override
public boolean hasNext() { return true; }

@Override
public Integer next()
{
  if (this.first) {
this.first = false;
return this.z;
  }

  this.z *= -1;
  if (this.z <= 0)
--this.z;
  return this.z;
}
  }

  @Override
  public IntegersIterator iterator() { return new 
IntegersIterator(0); }


  public static void main(String[] args)
  {
/* syntax sugar for
 *   {
 * final var it = newIntegers.iterator();
 * while (it.hasNext()) {
 *   final int z = it.next();
 *   ...
 * }
 *   }
 */
for (final int z : new Integers()) {
  System.out.println(z);
  if (z == 5)
break;
}
  }
}
```

The equivalent in python is a generator function:
```python
def integers():
  z = 0
  yield z
  while True:
z *= -1
if z <= 0:
  z -= 1
yield z

for z in integers():
  print(z)
  if z == 5:
break
```

etc



Re: "chain" vs "~"

2022-08-06 Thread Emanuele Torre via Digitalmars-d-learn

On Sunday, 7 August 2022 at 01:22:18 UTC, pascal111 wrote:

Why we use "chain" while we have "~":

'''D
int[] x=[1,2,3];
int[] y=[4,5,6];

auto z=chain(x,y);
auto j=x~y;
'''


They are quite different:
* `chain` gives you "range" (iterator) that starts from the first 
element of `x` and ends at the last element of `y` (like e.g. 
`zip` in other languages).
* `~` creates a new `int[]` with elements from `x` and the 
elements from `y`.


If you use `z[2] = 10;`, you will change `x[2]` as well, since 
`z` is just an iterator of which the third element is the value 
stored in `x[2]`; if you use `z[4] = 11;`, you will change 
`y[1]`; if you change `y[0]`, `z[3]` will also change; etc. (n.b. 
if you replace `x` and `y` with other arrays, then this doesn't 
apply because then `x` and `y` use different memory.)


`z` is just a range you can use to access the memory of those 
`[1,2,3]` and `[4,5,6]` arrays, not a new array.


Here is some example code to show the differences:

```D
void main()
{
import std.range : chain;
import std.stdio : writefln;

int[] x = [ 1, 2, 3 ];
int[] y = [ 4, 5, 6 ];

auto z = chain(x, y);
auto j = x ~ y;

writefln("x: %(%s %)", x);
writefln("y: %(%s %)", y);
writefln("z: %(%s %)", z);
writefln("j: %(%s %)", j);

writefln("y[1]=10;---");
y[1] = 10;

writefln("x: %(%s %)", x);
writefln("y: %(%s %)", y);
writefln("z: %(%s %)", z);
writefln("j: %(%s %)", j);

writefln("z[2]=9;");
z[2] = 9;

writefln("x: %(%s %)", x);
writefln("y: %(%s %)", y);
writefln("z: %(%s %)", z);
writefln("j: %(%s %)", j);

writefln("j[2]=8;");
j[2] = 8;

writefln("x: %(%s %)", x);
writefln("y: %(%s %)", y);
writefln("z: %(%s %)", z);
writefln("j: %(%s %)", j);
}
```

output:
```
x: 1 2 3
y: 4 5 6
z: 1 2 3 4 5 6
j: 1 2 3 4 5 6
y[1]=10;---
x: 1 2 3
y: 4 10 6
z: 1 2 3 4 10 6
j: 1 2 3 4 5 6
z[2]=9;
x: 1 2 9
y: 4 10 6
z: 1 2 9 4 10 6
j: 1 2 3 4 5 6
j[2]=8;
x: 1 2 9
y: 4 10 6
z: 1 2 9 4 10 6
j: 1 2 8 4 5 6
```