Re: Analyze debug condition in template

2021-10-25 Thread novice3 via Digitalmars-d-learn

i want to eliminate "debug(func1)" and "debug(func2)" from code:

```d
debug = func1;// enable logging for func1
//debug = func2;  // disable logging for func2

void func1()
{
...
debug(func1) logf("var1=%d", var1);
...
}

void func2()
{
...
debug(func2) logf("var1=%d", var1);
...
}
```



Analyze debug condition in template

2021-10-25 Thread novice3 via Digitalmars-d-learn

Hello.
Need advice:
Is it possible analyze "debug" condition in template, obtained 
from instantiation place?

Like we using __LINE__ ?

For example, i have template for logging:
```d
void logf(string func = __FUNCTION__, int line = __LINE__, 
A...)(string fmt, A args)

{
// here i want analyze, if debug == func
// and writef conditionally
writefln("%s:%d " ~ fmt, func, line, args);
}
```

And i want switch on logging for one function module1.func(),
then i declare in module1
```d
debug = func1;  // enable logging for func1

void func1()
{
...
logf("var1=%d", var1);
...
}
```

And i want to check in logf()() template: is debug condition 
equal function name, which instantiate logf() template.


Just advise me direction or proper keyword ...
Thanks.


Re: How do I create classes dynamically?

2021-04-16 Thread novice3 via Digitalmars-d-learn

On Thursday, 15 April 2021 at 20:56:18 UTC, mw wrote:


In response to user input?

e.g. createObject(userInputString);



what if user enter "Cthulhu"?

if you say "i will check user input",
then you know list of possible inputs,
then you can use some compile-time technics.

if you dont know possible user inputs,
and user can input anything,
then questions raised:
- what is object Cthulhu meaning?
- what properties and methods it has?
etc...


Is this bug ? format %(%)

2021-04-07 Thread novice3 via Digitalmars-d-learn

https://run.dlang.io/is/p4NVp8
```d
void main()
{
import std.stdio: writefln;
string[] s = ["a", "b", "c"];
writefln("%(%s, %)", s);
}
```
output
```d
"a", "b", "c"
```
expected
```d
a, b, c
```

there is extra quotes, wich not present in firmat specifier.
is this bug, or i should change something in my code?


Re: Derived type

2021-04-01 Thread novice3 via Digitalmars-d-learn

On Thursday, 1 April 2021 at 12:07:17 UTC, WebFreak001 wrote:

You can add a custom init value if you want to allow one:

```d
enum Xobj : void* { init = null }
```


Thank you!


Re: Derived type

2021-03-31 Thread novice3 via Digitalmars-d-learn

On Wednesday, 31 March 2021 at 12:09:33 UTC, Basile B. wrote:
yeah template instances are identified using the parameters 
identifiers, then the alias is just a syntactic shortcut to 
that, not producing a new symbol with a unique mangle...


so, no way to generate struct with parametrized name by template 
or mixin template?



That being said you can still use a string mixin if the 
messages have to be correct

...


thank you!


Re: Derived type

2021-03-30 Thread novice3 via Digitalmars-d-learn

On Tuesday, 30 March 2021 at 21:53:34 UTC, Basile B. wrote:

struct Typedef(TBase)
{
   TBase payload;
   alias payload this;
}

alias Xobj = Typedef!(void*);


This is how std.typecons.Typedef made, IMHO.

The problem is this code generate struct with name 
"Typedef!(void*)",

then compiler show this name (not "Xobj") in messages:

https://run.dlang.io/is/eEI2yC

  void* bad;
  foo(bad);

Error: function foo(Typedef!(void*) obj) is not callable using 
argument types (void*)
   cannot pass argument bad of type void* to parameter 
Typedef!(void*) obj




Derived type

2021-03-30 Thread novice3 via Digitalmars-d-learn

Hello.

When i adapt C code, i see new type creation:
  typedef void* Xobj;

Or code like this:
  struct _Xobj;
  typedef struct _Xobj *Xobj;


I want create derived type in D, found std.typecons.Typedef 
template, and write:

  alias Xobj = Typedef!(void*, (void*).init);

But compiler use long type name in error messages, like this:

  Error: function test6.foo(Typedef!(void*, null, null) obj) is 
not callable using argument types (void*)


  cannot pass argument bad of type void* to parameter 
Typedef!(void*, null, null) obj


This messages dont help me understand, which type should i use.
What i should change?
Or Typedef template should be changes?
Any Typedef alternatives?


Re: Enum template for cpp binding

2021-02-15 Thread novice3 via Digitalmars-d-learn

On Monday, 15 February 2021 at 14:03:26 UTC, Paul Backus wrote:

This will do most of it:


Thank you Paul!


Enum template for cpp binding

2021-02-15 Thread novice3 via Digitalmars-d-learn

Hello.
I want make binding for some CPP api.
I have .h file with enums like:

///
typedef enum {
SOMEAPI_PHASE_A = 91,
SOMEAPI_PHASE_B = 92,
SOMEAPI_PHASE_C = 93
} someapiPhase;
///

It used later in .cpp like:
func(SOMEAPI_PHASE_A);


I want .d file like this:

///
enum SOMEAPI_PHASE {
A = 91,
B = 92,
C = 93
}
alias SOMEAPI_PHASE_A = SOMEAPI_PHASE.A;  // for using like 
anonymous enum like C++

alias SOMEAPI_PHASE_B = SOMEAPI_PHASE.B;
alias SOMEAPI_PHASE_C = SOMEAPI_PHASE.C;
alias SomeapiPhase = SOMEAPI_PHASE;  // for using type in func 
declarations

///

This is reduced example.
I am sorry for this type of question,
but could please anybody show me template for this boring coding?
Is this possible to avoid this manual coding?
Show me direction or examples please.

Thanks.


Re: mysql-native Help required

2020-10-22 Thread novice3 via Digitalmars-d-learn

On Thursday, 22 October 2020 at 11:04:53 UTC, Vino wrote:

class Connections
{
  private Connection conn;
  auto constr = 
"host=localhost;port=3910;user=user;pwd=password#;db=testdb";

  this.conn = new Connection(constr);
}


where Connections class constructor implemented?


Memory management

2020-09-29 Thread novice3 via Digitalmars-d-learn

Naive newbie question:

Can we have (in theory) in D lang memory management like V lang?

Quote:
  
https://github.com/vlang/v/blob/master/doc/docs.md#memory-management


"V doesn't use garbage collection or reference counting. The 
compiler cleans everything up during compilation. If your V 
program compiles, it's guaranteed that it's going to be leak 
free."


Re: Installing D on Fresh Windows 10 machine is a pain

2020-08-27 Thread novice3 via Digitalmars-d-learn

DMD x86 on Windows have no dependencies, just unpack .zip and use.
It's a pitty, that DMD x64 depend on VS :(


Re: Subtyping with alias this

2020-08-18 Thread novice3 via Digitalmars-d-learn

On Tuesday, 18 August 2020 at 05:54:16 UTC, H. S. Teoh wrote:

Here's a working example:


Thank you, it works!


Re: Subtyping with alias this

2020-08-17 Thread novice3 via Digitalmars-d-learn

On Monday, 17 August 2020 at 14:43:27 UTC, H. S. Teoh wrote:
What you need is to create an overload of toString that takes a 
FormatSpec parameter, so that you can decide what should be 
output for which format spec.  Something along these lines:


Sorry, i can't make it works.
I tried ti read format.d, but it too complex for me.

The problem is:
if i use fmt.spec in overloaded toString(),
when i get error "phobos/std/format.d(2243): Error: no property 
ip for type onlineapp.IpV4Address"


reduced code https://run.dlang.io/is/Kgbhfd

```
import std.format;

struct IpV4Address
{
  private uint ip;
  alias ip this;

  void toString(W,Char)(W sink, FormatSpec!Char fmt)
  {
sink(fmt.spec);  // ERROR
//sink("s");   // OK
  }
}
void main()
{
  IpV4Address x;
  assert( format("%s", x) == "s");
}
```
/dlang/dmd-nightly/linux/bin64/../../src/phobos/std/format.d(2243): Error: no 
property ip for type onlineapp.IpV4Address
/dlang/dmd-nightly/linux/bin64/../../src/phobos/std/format.d(1875): Error: 
template instance std.format.formatValueImpl!(Appender!string, IpV4Address, 
char) error instantiating
/dlang/dmd-nightly/linux/bin64/../../src/phobos/std/format.d(576):
instantiated from here: formatValue!(Appender!string, IpV4Address, char)
/dlang/dmd-nightly/linux/bin64/../../src/phobos/std/format.d(6630):
instantiated from here: formattedWrite!(Appender!string, char, IpV4Address)
onlineapp.d(17):instantiated from here: format!(char, 
IpV4Address)




Subtyping with alias this

2020-08-17 Thread novice3 via Digitalmars-d-learn

Hello.
I need subtype uint to store ipv4 address.
It should be like ordinary uint,
except conversion to string with %s format.

My try  https://run.dlang.io/is/fwTc0H  failed on last assert:

```
struct IpV4Address
{
  private uint ip;
  alias ip this;

  string toString()
  {
import std.conv: to;
return to!string((ip >>> 24) & 0xFF) ~ "." ~
   to!string((ip >>> 16) & 0xFF) ~ "." ~
   to!string((ip >>> 8) & 0xFF) ~ "." ~
   to!string(ip & 0xFF);
  }
}
void main()
{
  import std.format: format;
  IpV4Address x;
  x = 0x01020304;  // 1.2.3.4
  assert( x ==  0x01020304 );
  assert( format("%s", x) == "1.2.3.4" );
  assert( format("%x", x) == "01020304" );  // FAILED
  /+
std.format.FormatException@/dlang/dmd-nightly/linux/bin64/../../src/phobos/std/format.d(4065):
 Expected '%s' format specifier for type 'IpV4Address'
  +/
}
```

Is my goal (subtype should be normal uint, but with pretty 
to-string conversion) possible?


Thanks.


Re: Template: get function name

2020-08-17 Thread novice3 via Digitalmars-d-learn
On Monday, 17 August 2020 at 10:11:29 UTC, MoonlightSentinel 
wrote:

On Monday, 17 August 2020 at 09:59:21 UTC, novice3 wrote:

On Monday, 17 August 2020 at 09:45:55 UTC, novice3 wrote:

access violation occur.


reduced code https://run.dlang.io/is/U58t9R


The wrapper parameters don't inherit the storage classes from  
the wrapped function. Try using std.traits.Parameters instead: 
https://run.dlang.io/is/wTyJWD.


Nice, it works!


Re: Template: get function name

2020-08-17 Thread novice3 via Digitalmars-d-learn

On Monday, 17 August 2020 at 09:45:55 UTC, novice3 wrote:

access violation occur.


reduced code https://run.dlang.io/is/U58t9R

void test(out int x) { x = 42; }

void call (alias fn, Args ...)(Args args) { fn(args); }

void main(){
int a;

a = 111;
test(a);
assert(a == 42);  // OK

a = 222;
call!test(a);
assert(a == 42);  // FAIL
}



Re: Template: get function name

2020-08-17 Thread novice3 via Digitalmars-d-learn

On Monday, 17 August 2020 at 08:55:49 UTC, Simen Kjærås wrote:
Take the function as an alias parameter and wrap the entire 
call:


Simen, for some reasons, your code dont respect api arg with 
"out" attribute.


For example, i have
```
extern (Windows)
DhcpEnumServers(
in  DWORDFlags, // must be 
zero
in  LPVOID   IdInfo,// must be 
NULL
out LPDHCP_SERVER_INFO_ARRAY Servers,   // output 
servers list
in  LPVOID   CallbackFn,// must be 
NULL
in  LPVOID   CallbackData   // must be 
NULL

);
```

and then i use it:
```
LPDHCP_SERVER_INFO_ARRAY servers;
denforce!DhcpEnumServers(0, null, servers, null, null);
```

access violation occur.
In disassembler we can see that servers parameter passed by value 
(initial 0 instead of address).

In my earler code it wass passed properly, by ref.

May be this not matter of your code,
and i can bypass it by change extern declaration to

  LPDHCP_SERVER_INFO_ARRAY *Servers,

but it will be interest to know the reasons...

Any way, tnaks you for your code, i will use it.


Re: Template: get function name

2020-08-17 Thread novice3 via Digitalmars-d-learn

On Monday, 17 August 2020 at 08:55:49 UTC, Simen Kjærås wrote:
Take the function as an alias parameter and wrap the entire 
call:


auto denforce(alias fn, string file = __FILE__, size_t line = 
__LINE__, Args...)(Args args)


Thank you, Simen!


Template: get function name

2020-08-17 Thread novice3 via Digitalmars-d-learn

Hello.
I have wrapping Windows API functions, wich return 0 on success 
and erroro code on failure.


I copy wenforce template as:
```
private T denforce(T, S)(T value, lazy S msg = null, string file 
= __FILE__, size_t line = __LINE__)

{
  import core.sys.windows.winerror: NO_ERROR;
  import std.conv: to;
  if (value != NO_ERROR)
throw new WindowsException(value, to!string(msg), file, line);
  return value;
}
```

and use it:
```
DhcpEnumServers(0, null, servers, null, 
null).denforce("DhcpEnumServers");



Then windows api - extern (Windows)DhcpEnumServers - return 
error, then Windows exception throwed with name of failed api 
"DhcpEnumServers".


Can we change template to avoid api name dublicate?
Can denforce template obtain "DhcpEnumServers" function name to 
format message "DhcpEnumServers api failed"?


Thanks.



Re: DMD: how to restore old unittest+main

2020-08-13 Thread novice3 via Digitalmars-d-learn

On Thursday, 13 August 2020 at 09:02:28 UTC, Nils Lankila wrote:
programmatically, in a way it is already, but by calling 
function


Better with compiler switch, may be...



Re: DMD: how to restore old unittest+main

2020-08-13 Thread novice3 via Digitalmars-d-learn

On Thursday, 13 August 2020 at 08:30:44 UTC, Jonathan wrote:
Is there a reason you need to run all unittests every time you 
want to run the program?


Starting app with unittests while develop - frequent event for me.
Releasing app - rare event for me.
I want do frequent action without efforts (just start and see all 
ok - unitests and main),
and can do rare action (release) with some efforts (compile with 
other switches).




Re: DMD: how to restore old unittest+main

2020-08-13 Thread novice3 via Digitalmars-d-learn

On Thursday, 13 August 2020 at 08:49:21 UTC, WebFreak001 wrote:

Try

version (unittest) extern(C) __gshared string[] rt_options = 
["testmode=run-main" ];


Thanks! It works as needed.


Re: DMD: how to restore old unittest+main

2020-08-13 Thread novice3 via Digitalmars-d-learn

On Thursday, 13 August 2020 at 08:30:44 UTC, Jonathan wrote:
Is there a reason you need to run all unittests every time you 
want to run the program?


App will be used by other peoples,
and i will release it after developing without unittests.
Release is rare action for me.
Running while developing is frequent action.
I want see is all ok - unittests and main{} - for every test run 
while develop whithout special efforts from me.
Then release with some efforts (compile without "-unittest", with 
"-release" switch etc).





DMD: how to restore old unittest+main

2020-08-13 Thread novice3 via Digitalmars-d-learn

Hello.

I don't use dub.
I use Windows and *.d file association to compile small apps by 
dmd with "-i -unittest -g" switches.
Now i update dmd, and found, that apps compiled with "-unittest" 
not runs main().


How i can restore old behaviour (run all unittests then main())
*without use "--DRT-testmode=run-main" switch every time then i 
start compiled app.exe*?
I want just press Enter on app.d file, then press Enter on 
app.exe.

Any advises?

Thanks.