Re: Debugging D code with GDB

2021-11-27 Thread Alexey via Digitalmars-d-learn
I found what Nemiver is much better for debugging D programs. 
With GDB I've got many problems, don't remember exactly. Thou 
I've used it through ddd, so maybe it's ddd problems, not exactly 
GDB's




Re: RFC to: my need for 'static switch' and CT 'static variables'

2021-11-25 Thread Alexey via Digitalmars-d-learn

On Friday, 26 November 2021 at 00:41:34 UTC, Elronnd wrote:

you are right. thanks


Re: RFC to: my need for 'static switch' and CT 'static variables'

2021-11-25 Thread Alexey via Digitalmars-d-learn

On Thursday, 25 November 2021 at 22:00:15 UTC, Alexey wrote:

I would want an static 'switch here',


I mean something like
```D
static switch (v.mode)
{
default:
   static assert(false, "v.mode" is invalid)
case "gs_w_d":
 // etc...
}
```


RFC to: my need for 'static switch' and CT 'static variables'

2021-11-25 Thread Alexey via Digitalmars-d-learn
for example, I have the code (code in sample generates different 
types of Properties dependingly on struct's mode value):

```D
struct PropSetting
{
string mode;
string type;
string var_name;
string title_name;
string default_value;
}

mixin template mixin_install_multiple_properties(PropSetting[] 
settings)

{
import std.format;

static foreach (v; settings)
{
static if (v.mode == "gs_w_d")
{
mixin(
q{
private {
mixin Property_%1$s!(%2$s, "%3$s", %5$s);
}

mixin Property_forwarding!(%2$s, %3$s, 
"%4$s");


}.format(
v.mode,
v.type,
v.var_name,
v.title_name,
v.default_value,
)
);
}

static if (v.mode == "gsu" || v.mode == "gs" || v.mode == 
"gsun")

{
mixin(
q{
private {
mixin Property_%1$s!(%2$s, "%3$s");
}

mixin Property_forwarding!(%2$s, %3$s, 
"%4$s");


}.format(
v.mode,
v.type,
v.var_name,
v.title_name,
)
);
}

}
}
```
currently it works kind of ok. but what it's missing is check on 
what none of [static if]s have worked. Ideally, I would want an 
static 'switch here', so I could make `static assert(false)` in 
it. and/or I'd like some sort of [compile time variable] to 
change it if  [static if]s have worked and check such [compile 
time variable] later.




Re: How to read a single character in D language?

2021-11-23 Thread Alexey via Digitalmars-d-learn

On Wednesday, 24 November 2021 at 04:51:03 UTC, Alexey wrote:

On Wednesday, 24 November 2021 at 04:48:46 UTC, Alexey wrote:


oh, also I completely missed what you need it to work without 
Enter key presses. so here is fixed version


```D
import std.stdio;

import core.sys.posix.termios;

void main()
{
termios input_settings;

tcgetattr(stdin.fileno, _settings);
input_settings.c_lflag &= ~ICANON;
tcsetattr(stdin.fileno, TCSANOW, _settings);

while (true)
{
char c;
stdin.readf("%c", );

writefln("\n%c (0x%1$x %1$d) is inputed", c);
}
}
```


Re: How to read a single character in D language?

2021-11-23 Thread Alexey via Digitalmars-d-learn

On Wednesday, 24 November 2021 at 04:48:46 UTC, Alexey wrote:

writefln(0x"%c (%1$x %1$d) is inputed", c);

sorry:
```diff
10c10


Re: How to read a single character in D language?

2021-11-23 Thread Alexey via Digitalmars-d-learn

On Friday, 19 November 2021 at 17:36:55 UTC, BoQsc wrote:
Let's say I want to write a simple program that asks for an 
input of a single character.
After pressing a single key on a keyboard, the character is 
printed out and the program  should stop.


```D
import std.stdio;

void main()
{
while (true)
{
char c;
scanf("%c", );

writefln(0x"%c (%1$x %1$d) is inputed", c);
}
}
```


Re: mixins

2021-11-21 Thread Alexey via Digitalmars-d-learn

On Wednesday, 17 November 2021 at 14:51:58 UTC, Abby wrote:
Hello I would like to create validation mixin or mixin template 
which would return on error.


Something like this:


```
mixin template Validate(a, b)
{
if(a > b)
{
   writeln("invalid input");
   return false;
}
}

bool test(a,b)
{
mixin Validate!(a,b);


on valid code


return true;
}
```


I'll add My 5 cents: with aliases this a little bit flexibilier, 
but without temporary function declaration this doesn't work


```D
import std.stdio;

mixin template validation(alias a, alias b)
{
mixin(
q{
bool x(){
writeln("a: ",a, " b: ", b);
if (a > b)
{
writeln("invalid input");
return false;
}
return true;
}
}
);
}

bool test(int a, int b)
{
mixin validation!(a, b);
if (!x()) return false;
return true;
}

void main()
{
writeln("test result: ", test(2,1));
}
```


Re: need `this` on trying to use class method with parallelism Task! or task!

2021-11-14 Thread Alexey via Digitalmars-d-learn

On Sunday, 14 November 2021 at 17:42:18 UTC, Imperatorn wrote:

Try task() instead of task!()


worked! huge thanks!


Re: need `this` on trying to use class method with parallelism Task! or task!

2021-11-14 Thread Alexey via Digitalmars-d-learn
On Sunday, 14 November 2021 at 16:40:58 UTC, Andrey Zherikov 
wrote:

Just do `auto t1 = task()` in `threadCreator` then.



Error: value of `this` is not known at compile time

```D
import std.stdio;
import std.parallelism;

class TC
{
void threadFunc()
{
import core.thread;
import core.time;

scope (exit)
writeln("threadFunc done");

core.thread.osthread.Thread.getThis.sleep(msecs(2000));
}

void threadCreator()
{
scope (exit)
writeln("threadCreator done");

auto t1 = task!();
t1.executeInNewThread();
}
}

void main()
{
scope (exit)
writeln("main done");

auto tc = new TC;
tc.threadCreator();
}
```



Re: need `this` on trying to use class method with parallelism Task! or task!

2021-11-14 Thread Alexey via Digitalmars-d-learn
On Sunday, 14 November 2021 at 14:24:00 UTC, Andrey Zherikov 
wrote:

On Sunday, 14 November 2021 at 12:01:36 UTC, Alexey wrote:




You just need two changes:
- make `threadFunc` function static: `static void threadFunc()`
- use `task` instead of `Task`: `auto t1 = task!threadFunc;`


I need `this` to be available inside of threadFunc


Re: need `this` on trying to use class method with parallelism Task! or task!

2021-11-14 Thread Alexey via Digitalmars-d-learn

On Sunday, 14 November 2021 at 12:01:36 UTC, Alexey wrote:

/home/animuspexus/dlang/d_v2.098.0/dmd/generated/linux/release/64/../../../../../phobos/std/parallelism.d(436):
 Error: need `this` for `threadFunc` of type `void()`


Go example for contrast. Ideally, I'd like something similar;

```Go
package main

import (
"fmt"
"time"
)

type TC struct {
}

func (self *TC) threadFunc() {
defer fmt.Println("threadFunc done")
time.Sleep(time.Duration(time.Second * 2))
}

func (self *TC) threadCreator() {
defer fmt.Println("threadCreator done")
go self.threadFunc()
}

func main() {
defer fmt.Println("main done")
tc := new(TC)
tc.threadCreator()
}
```


Re: need `this` on trying to use class method with parallelism Task! or task!

2021-11-14 Thread Alexey via Digitalmars-d-learn

On Sunday, 14 November 2021 at 12:01:36 UTC, Alexey wrote:

auto t1 = Task!threadFunc;


if this line rewritten as `auto t1 = Task!(TC.threadFunc, this);`

```text
/home/animuspexus/dlang/d_v2.098.0/dmd/generated/linux/release/64/../../../../../phobos/std/parallelism.d(451):
 Error: tuple `Args` is used as a type
/home/animuspexus/dlang/d_v2.098.0/dmd/generated/linux/release/64/../../../../../phobos/std/parallelism.d(533):
 Error: tuple `Args` is used as a type
/home/animuspexus/dlang/d_v2.098.0/dmd/generated/linux/release/64/../../../../../druntime/import/core/internal/traits.d(191):
 Error: template instance `pred!(this)` does not match template declaration 
`isAssignable(Lhs, Rhs = Lhs)`
/home/animuspexus/dlang/d_v2.098.0/dmd/generated/linux/release/64/../../../../../phobos/std/meta.d(842):
 Error: template instance `t.TC.threadCreator.allSat!(isAssignable, this)` 
error instantiating
/home/animuspexus/dlang/d_v2.098.0/dmd/generated/linux/release/64/../../../../../phobos/std/parallelism.d(541):
instantiated from here: `allSatisfy!(isAssignable, this)`
./t.d(22):instantiated from here: `Task!(threadFunc, 
this)`

```


need `this` on trying to use class method with parallelism Task! or task!

2021-11-14 Thread Alexey via Digitalmars-d-learn

/home/animuspexus/dlang/d_v2.098.0/dmd/generated/linux/release/64/../../../../../phobos/std/parallelism.d(436):
 Error: need `this` for `threadFunc` of type `void()`
./t.d(22): Error: template instance 
`std.parallelism.Task!(threadFunc)` error instantiating



thou documentation says `a function (or delegate or other 
callable)`


```D

import std.stdio;
import std.parallelism;


class TC {

void threadFunc()
{
import core.thread;
import core.time;

scope(exit) writeln("threadFunc done");

core.thread.osthread.Thread.getThis.sleep(msecs(2000));

}

void threadCreator()
{
scope(exit) writeln("threadCreator done");
auto t1 = Task!threadFunc;
t1.executeInNewThread();

}

}


void main()
{
scope(exit) writeln("main done");

auto tc = new TC;
tc.threadCreator();

}

```


Re: Inheriting function template from super class

2021-09-27 Thread Alexey via Digitalmars-d-learn

On Monday, 27 September 2021 at 16:10:54 UTC, Adam D Ruppe wrote:

This is by design, overloads only consider things declared in 
the same place.


see here https://dlang.org/articles/hijack.html


thanks, Adam
I'd also like to ask about similar code.
If I'm writing code like so, I'm getting error.
What I wanted to, is `this` to be the instance of C2. how can I 
do this?
the second snippet is how I worked it around (explicitly defined 
and used template type  parameter), but is there better solution?

```D
import std.stdio;

class C1
{
void writetext()
{
this.writetext_x();
}

void writetext_x(this T)()
{
const id = __traits(identifier, T);
pragma(msg, "generating writetext for ", id);
static assert(is(T == typeof(this)));
}
}

class C2 : C1
{

}

void main()
{
auto c2 = new C2;
c2.writetext_x();
}
```

```D
import std.stdio;

class C1
{
void writetext()
{
this.writetext_x();
}

void writetext_x(T)(T new_this)
{
const id = __traits(identifier, T);
pragma(msg, "generating writetext for ", id);
}
}

class C2 : C1
{

}

void main()
{
auto c2 = new C2;
c2.writetext_x();
}
```


Re: Inheriting function template from super class

2021-09-27 Thread Alexey via Digitalmars-d-learn

On Monday, 27 September 2021 at 14:54:41 UTC, Alexey wrote:

hello.
here I have some code sample.


consequently, If I want C3 to inherit form C2, I'll have to 
forward template to from C1 to C2 by hand:


```D
import std.stdio;

class C1
{
void writetext()
{
this.writetext!(typeof(this))();
}

void writetext(alias A1)()
{
writeln("C1 writetext(alias A1) ", this);
}
}

class C2 : C1
{
override void writetext()
{
this.writetext!(typeof(this))();
}

void writetext(alias A1)()
{
super.writetext!(A1)();
}
}

class C3 : C2
{
override void writetext()
{
super.writetext!(typeof(this))();
}

}

void main()
{
auto c3 = new C3;
c3.writetext();
}
```


Inheriting function template from super class

2021-09-27 Thread Alexey via Digitalmars-d-learn

hello.
here I have some code sample.
I'm not sure if this is my incorrect code or incorrect dmd 
behavior.
Intention is C2 to inherit C1's `void writetext(alias A1)()` and 
instantiate it with `this.writetext!(typeof(this))();`

```D
import std.stdio;

class C1
{
void writetext()
{
this.writetext!(typeof(this))();
}

void writetext(alias A1)()
{
writeln("C1 writetext(alias A1) ", this);
}
}

class C2 : C1
{
override void writetext()
{
// if line 21 is commented and line 22 is uncommented - 
this compiles ok

this.writetext!(typeof(this))();
// super.writetext!(typeof(this))();
}
}

void main()
{
auto c2 = new C2;
c2.writetext();
}

```


Re: implimenting interface function by inheriting from other class

2021-08-22 Thread Alexey via Digitalmars-d-learn

On Sunday, 22 August 2021 at 12:20:56 UTC, Alexey wrote:

On Saturday, 21 August 2021 at 20:35:43 UTC, Alexey wrote:

Hello
```D
interface Int
{
void coolFunc();
}

class C1
{
void coolFunc()
{
return;
}
}

class C2 : C1, Int
{

}

void main()
{
auto c = new C2;
}
```
dmd says it's not Ok:
t.d(14): Error: class `t.C2` interface function `void 
coolFunc()` is not implemented


how to make dmd happy?


so, Is this a bug in dmd or not?


some answers have been given here and here
https://discord.com/channels/242094594181955585/242122752436338688/878996818904612864
https://issues.dlang.org/show_bug.cgi?id=22232


Re: implimenting interface function by inheriting from other class

2021-08-22 Thread Alexey via Digitalmars-d-learn

On Saturday, 21 August 2021 at 20:35:43 UTC, Alexey wrote:

Hello
```D
interface Int
{
void coolFunc();
}

class C1
{
void coolFunc()
{
return;
}
}

class C2 : C1, Int
{

}

void main()
{
auto c = new C2;
}
```
dmd says it's not Ok:
t.d(14): Error: class `t.C2` interface function `void 
coolFunc()` is not implemented


how to make dmd happy?


so, Is this a bug in dmd or not?


Re: implimenting interface function by inheriting from other class

2021-08-21 Thread Alexey via Digitalmars-d-learn

On Saturday, 21 August 2021 at 20:35:43 UTC, Alexey wrote:

Hello
```D
interface Int
{
void coolFunc();
}

class C1
{
void coolFunc()
{
return;
}
}

class C2 : C1, Int
{

}

void main()
{
auto c = new C2;
}
```
dmd says it's not Ok:
t.d(14): Error: class `t.C2` interface function `void 
coolFunc()` is not implemented


how to make dmd happy?


we're with friends tried similar thing with c++, java, groovy and 
go - worked ok.

```Java
interface Int
{
public void coolFunc();
};

class C1
{
public void coolFunc()
{
return;
}
};

class C2 extends C1 implements Int
{

};


public class Main {

public static void main(String args[])
{
Int c = new C2();
}

}
```
```Groovy
interface Int
{
void coolFunc()
}

class C1
{
void coolFunc()
{  print "hello" }
}

class C2 extends C1 implements Int
{ }

def printHello = { Int a -> a.coolFunc(); print " world: accept 
$Int" }


C2 a = new C2()
printHello(a)
```
```C++
class Int;
class C1;
class C2;

class Int
{
public:
virtual void coolFunc() = delete;
};

class C1
{
public:
virtual void coolFunc()
{
return;
}
};

class C2 : public C1, public Int
{
public:
};

int main(int argc, char* argv[])
{
auto c = new C2;
}
```
```Go
package main

import "fmt"

type Int interface {
  CoolFunc()
}

type C1 struct {
}

func (self *C1) CoolFunc() {
  fmt.Println("worked")
  return
}

type C2 struct {
  C1
}

func main() {

  var c Int

  c = new(C2)

  c.CoolFunc()

}
```


Re: implimenting interface function by inheriting from other class

2021-08-21 Thread Alexey via Digitalmars-d-learn

On Saturday, 21 August 2021 at 23:14:14 UTC, Alexey wrote:
I want `this` inside of C1::coolFunc to return C2 if called as 
C2::coolFunc


so executing `cast(C2) this !is null` inside of C1::coolFunc 
would work


If this would work, I'd farther used this like so
```D
interface Int
{
void coolFunc();
}

class C1
{
void coolFunc()
{
{
auto obj = (cast(C1)(cast(Int) this));
if (obj !is null)
{
do_one_thing();
}
}
{
auto obj = (cast(C2)(cast(Int) this));
if (obj !is null)
{
do_another_thing();
}
}
{
auto obj = (cast(C3)(cast(Int) this));
if (obj !is null)
{
do_something_else();
}
}
return;
}
}

class C2 : C1, Int
{

}

class C3 : C1, Int
{

}

class C4 : C1, Int
{

}

void main()
{
auto c = new C2;
c.coolFunc();
}

```


Re: implimenting interface function by inheriting from other class

2021-08-21 Thread Alexey via Digitalmars-d-learn
I want `this` inside of C1::coolFunc to return C2 if called as 
C2::coolFunc


so executing `cast(C2) this !is null` inside of C1::coolFunc 
would work


Re: implimenting interface function by inheriting from other class

2021-08-21 Thread Alexey via Digitalmars-d-learn

On Saturday, 21 August 2021 at 22:56:40 UTC, Bastiaan Veelo wrote:

On Saturday, 21 August 2021 at 20:35:43 UTC, Alexey wrote:

Hello
```D
interface Int
{
void coolFunc();
}

class C1
{
void coolFunc()
{
return;
}
}

class C2 : C1, Int
{

}

void main()
{
auto c = new C2;
}
```
dmd says it's not Ok:
t.d(14): Error: class `t.C2` interface function `void 
coolFunc()` is not implemented


how to make dmd happy?


Not sure if this is the best way, but it does make dmd happy: 
https://run.dlang.io/is/44F3AE


```d

class C2 : C1, Int
{
override void coolFunc()
{
C1.coolFunc;
}
}
```

It looks lame, I admit.

— Bastiaan.


I want `this` inside of C1::coolFunc to return C2 if called as 
C2::coolFunc


implimenting interface function by inheriting from other class

2021-08-21 Thread Alexey via Digitalmars-d-learn

Hello
```D
interface Int
{
void coolFunc();
}

class C1
{
void coolFunc()
{
return;
}
}

class C2 : C1, Int
{

}

void main()
{
auto c = new C2;
}
```
dmd says it's not Ok:
t.d(14): Error: class `t.C2` interface function `void coolFunc()` 
is not implemented


how to make dmd happy?


Re: How to check if variable of some type can be of null value?

2021-07-24 Thread Alexey via Digitalmars-d-learn

On Saturday, 24 July 2021 at 18:10:07 UTC, Alexey wrote:
I've tried to use ```typeof(t) is cast(t)null```, but compiler 
exits with error and so this can't be used for checking this 
issue.


The goal I with to achieve by this check - is to use template 
and to assign value to variable basing on it's ability to 
accept null as a value.




currently I ended up using ```__traits(compiles, cast(T1)null)``` 
for this check. but don't know is this really semantically 
correct.





How to check if variable of some type can be of null value?

2021-07-24 Thread Alexey via Digitalmars-d-learn
I've tried to use ```typeof(t) is cast(t)null```, but compiler 
exits with error and so this can't be used for checking this 
issue.


The goal I with to achieve by this check - is to use template and 
to assign value to variable basing on it's ability to accept null 
as a value.


some testing code below.

```D
import std.stdio;

interface I1
{
}

class C1 : I1
{
}

struct S1
{
}

struct S2
{
int a=1;
}

void main()
{

auto c1 = new C1;

I1 i1 = c1;

auto s1 = S1();
auto s2 = S2();

static assert(typeof(c1).init is cast(typeof(c1)) null);
static assert(typeof(i1).init is cast(typeof(i1)) null);
static assert(typeof(s1).init is cast(typeof(s1)) null);
static assert(typeof(s2).init is cast(typeof(s2)) null);
static assert(int.init is cast(int) null);

}

```


Visual Studio 2019

2021-03-07 Thread Alexey via Digitalmars-d-learn

Does Visual Studio 2019 support D development?
If yes, please give me instructions on how to work in this 
environment.