Re: Piping from terminal into D program

2021-09-05 Thread Виталий Фадеев via Digitalmars-d-learn

On Saturday, 4 September 2021 at 15:41:51 UTC, eXodiquas wrote:


My question is now, can someone explain what I am doing wrong? 
Maybe I misunderstood the pipe in Linux systems and it is 
obvious for someone who knows how this works exactly, or maybe 
D works differently with pipes and I havn't found the correct 
documentation.


eXodiquas


Of course, pipe ( | ./nounscramble ) is not args ( ./nounscramble 
arg1 arg2 ).


Pipe
`stdin -> ( read buf -> write buf ) -> stdout`

Args
`main( args ) -> write arg1, arg2 -> stdout`

Check the D std.stdio. https://dlang.org/phobos/std_stdio.html
Check the shell tool `xargs`. https://en.wikipedia.org/wiki/Xargs



Re: A way to mixin during runtime?

2021-08-28 Thread Виталий Фадеев via Digitalmars-d-learn

On Friday, 27 August 2021 at 06:52:10 UTC, Kirill wrote:

Is there a way to do mixin or similar during runtime?

I'm trying to read a csv file and extract data types. Any ideas 
on how this should be approached in D are greatly appreciated.


mixin at runtime not possible.
Source code compilation and runing in runtime not possible. But!

But you can impement this!

Steps:
1. Need D compiller
2. In runtime you can compile mixin to dynamic library (.so or 
.dll) using  external command: `dmd ...`
3. Load dynamic library using `dlopen()` `dlsym()` `dlclose()`. 
(on Windows: LoadLibrary(), GetProcAdres(), FreLibrary())

4. Call mixin functions, variables
5. Done!

Packages bindbc-*, derelict-* uses dynamic loading. You can use 
both as examples.




Re: C to D convertor

2021-08-24 Thread Виталий Фадеев via Digitalmars-d-learn

On Tuesday, 24 August 2021 at 11:52:45 UTC, Dennis wrote:
On Saturday, 21 August 2021 at 08:14:22 UTC, Виталий Фадеев 
wrote:

Any more ?


CPP2D
https://github.com/lhamot/CPP2D


Dennis, thank!


Re: dlang opengl / gl / glu /glut library.

2021-08-22 Thread Виталий Фадеев via Digitalmars-d-learn

On Sunday, 22 August 2021 at 13:28:24 UTC, Mike Parker wrote:

On Sunday, 22 August 2021 at 12:22:41 UTC, Виталий Фадеев wrote:

https://forum.dlang.org/post/s5pvtq$2q83$1...@digitalmars.com

On Wednesday, 21 April 2021 at 19:54:35 UTC, rikki cattermole 
wrote:


On 22/04/2021 7:51 AM, Alain De Vos wrote:

import bindc.opengl;


bindbc


bindbc-opengl provides glu* functions ?
gluNewTess
gluTessCallback
gluTessProperty
gluTessNormal
gluDeleteTess

able?


No. As far as I know, glu isn’t maintained anymore.


Derelict GLUT dynamic loader: 
https://github.com/vitalfadeev/DerelictGLU

This is beta.



Re: dlang opengl / gl / glu /glut library.

2021-08-22 Thread Виталий Фадеев via Digitalmars-d-learn

On Sunday, 22 August 2021 at 13:28:24 UTC, Mike Parker wrote:

On Sunday, 22 August 2021 at 12:22:41 UTC, Виталий Фадеев wrote:

https://forum.dlang.org/post/s5pvtq$2q83$1...@digitalmars.com

On Wednesday, 21 April 2021 at 19:54:35 UTC, rikki cattermole 
wrote:


On 22/04/2021 7:51 AM, Alain De Vos wrote:

import bindc.opengl;


bindbc


bindbc-opengl provides glu* functions ?
gluNewTess
gluTessCallback
gluTessProperty
gluTessNormal
gluDeleteTess

able?


No. As far as I know, glu isn’t maintained anymore.


Thank you, Mike.




dlang opengl / gl / glu /glut library.

2021-08-22 Thread Виталий Фадеев via Digitalmars-d-learn

https://forum.dlang.org/post/s5pvtq$2q83$1...@digitalmars.com

On Wednesday, 21 April 2021 at 19:54:35 UTC, rikki cattermole 
wrote:


On 22/04/2021 7:51 AM, Alain De Vos wrote:

import bindc.opengl;


bindbc


bindbc-opengl provides glu* functions ?
gluNewTess
gluTessCallback
gluTessProperty
gluTessNormal
gluDeleteTess

able?


Re: C to D convertor

2021-08-21 Thread Виталий Фадеев via Digitalmars-d-learn

On Saturday, 21 August 2021 at 08:59:55 UTC, evilrat wrote:
On Saturday, 21 August 2021 at 08:14:22 UTC, Виталий Фадеев 
wrote:
I know, i know... It not possible, but part of the C code we 
can to convert to the D.

Show me, please, solutions, projects, tools, scripts, docs.
Can you give the link ?

`htod` is 1.
Any more ?


dstep
https://code.dlang.org/packages/dstep

dpp
https://code.dlang.org/packages/dpp

ohmygentool
https://github.com/Superbelko/ohmygentool


evilrat, thank you!


C to D convertor

2021-08-21 Thread Виталий Фадеев via Digitalmars-d-learn
I know, i know... It not possible, but part of the C code we can 
to convert to the D.

Show me, please, solutions, projects, tools, scripts, docs.
Can you give the link ?

Examples of what the wanted:

// C
typedef struct {
uint32_t version; /* 0x5000 */
uint16_t num_glyphs;
} MaxProTable;
// D
struct MaxProTable {
uint32_t version; /* 0x5000 */
uint16_t num_glyphs;
}


// C
#define ENABLE_SUBDIV 0
// D
enum ENABLE_SUBDIV = 0;


// C
#include "triangulate.h"
// D
import triangulate;


// C
#include 
// D
import core.stdc.string;


// C
#define cross2(a,b) ((a)[0]*(b)[1]-(a)[1]*(b)[0])
// D
pragma( inline, true )
auto cross2( T1, T2 )( T1 a, T2 b ) { return 
((a)[0]*(b)[1]-(a)[1]*(b)[0]); }



// C
double ab[2];
// D
double[2] ab;

// C
trgu = calloc( 1, sizeof(*trgu) );
// D
trgu = calloc( 1, (*trgu).sizeof );


// C
PointFlag *point_flags = gt->flags;
// D
PointFlag *point_flags = gt.flags;

`htod` is 1.
Any more ?


Re: Trivial simple OpenGl working example

2021-07-11 Thread Виталий Фадеев via Digitalmars-d-learn

On Saturday, 10 July 2021 at 19:43:07 UTC, Danny Arends wrote:

On Saturday, 10 July 2021 at 12:41:19 UTC, Виталий Фадеев wrote:

[...]


Just disable shader compilation using dub, the shader only 
needs to be compiled once. Since you're on linux it fails to 
execute glslc.exe (the windows executable). If you compiled 
them yourself you can remove it from dub prebuild commands


Thanks, Danny Arends!
I fixed the configuration in dub.json, and in the application.d I 
fixed VK_API_VERSION_1_0. It worked!


Look in to the pull requests:

https://github.com/DannyArends/CalderaD/pulls



Re: Trivial simple OpenGl working example

2021-07-10 Thread Виталий Фадеев via Digitalmars-d-learn

On Saturday, 10 July 2021 at 08:36:07 UTC, Danny Arends wrote:

On Thursday, 8 July 2021 at 13:51:51 UTC, Виталий Фадеев wrote:

Hi!

I searching trivial simple D/OpenGL working in 2021 year 
example.


It may be triangle.
It may be based on any library: SDL, GLFW, Derelict, etc.

Can you help me ?


OpenGL is being replaced by vulcan, just to plug my little 
project:


http://github.com/DannyArends/CalderaD

Pretty beafy, but well vulkan is a lot more verbose compared to 
OpenGL


Thank, Danny Arends! I see you.

I got error:

# glslc app/src/main/assets/data/shaders/wavefront.vert -o 
app/src/main/assets/data/shaders/vert.spv


# glslc app/src/main/assets/data/shaders/wavefront.frag -o 
app/src/main/assets/data/shaders/frag.spv


# dub
Performing "debug" build using /usr/bin/dmd for x86_64.
bindbc-loader 0.3.2: target for configuration "yesBC" is up to 
date.
bindbc-sdl 0.21.4: target for configuration "dynamicBC" is up to 
date.

calderad ~master: building configuration "default"...
Running pre-build commands...
/bin/sh: 1: glslc.exe: not found
Command failed with exit code 127: glslc.exe 
app/src/main/assets/data/shaders/wavefront.vert -o 
app/src/main/assets/data/shaders/vert.spv





Re: Trivial simple OpenGl working example

2021-07-09 Thread Виталий Фадеев via Digitalmars-d-learn

On Friday, 9 July 2021 at 19:15:44 UTC, H. S. Teoh wrote:
On Fri, Jul 09, 2021 at 05:11:06AM +, Виталий Фадеев via 
Digitalmars-d-learn wrote: [...]

[...]

[...]

[...]


Generally, text rendering on OpenGL is a huge pain to 
implement. This is not specific to D, but to OpenGL in general, 
because OpenGL itself does not have direct support for text 
rendering at all.  The usual route is to use an existing 
text-rendering library like FreeType to rasterize your text, 
then upload it as a texture to the GPU and render it as a quad.


[...]


Thank, H. S. Teoh!


Re: assign to property 2 values

2021-07-09 Thread Виталий Фадеев via Digitalmars-d-learn

On Friday, 9 July 2021 at 11:04:23 UTC, Dennis wrote:

On Friday, 9 July 2021 at 10:19:59 UTC, Виталий Фадеев wrote:

It possible in current version 2.097 ?


If you `import std.typecons` you can do:
```D
element.border = tuple(1, solid).expand;
```

But it's not pretty. I suggest either calling the function 
regularly, or combing all settings in a single struct:

```D
element.border = Border(/*width:*/ 1, /*solid*/ true);
```
Named arguments are not implemented yet, hence the comments.


Thank.


But it's not pretty.


Yes.


Named arguments are not implemented yet, hence the comments.


+1






Re: assign to property 2 values

2021-07-09 Thread Виталий Фадеев via Digitalmars-d-learn

On Friday, 9 July 2021 at 10:19:59 UTC, Виталий Фадеев wrote:

I want this feature in D:

```
element.border = 1, solid;

struct Element
{
  @property
  void border( int width, BorderStyle style )
  {
this.borderWidth = width;
this.borderStyle = style;
  }
}
```

Description:
```
element.border = 1, solid;
```

will rewriten to the

```
element.border(1, solid);
```

This is same as @property.
```
element.color  = 0xCCC;

@property void color( uint a ) { this._color = a; }
```

but with 2 arguments, and more.

It possible in current version 2.097 ?


I undestand what

element.border = 1, solid;

is

element.border = 1;
solid();



assign to property 2 values

2021-07-09 Thread Виталий Фадеев via Digitalmars-d-learn

I want this feature in D:

```
element.border = 1, solid;

struct Element
{
  @property
  void border( int width, BorderStyle style )
  {
this.borderWidth = width;
this.borderStyle = style;
  }
}
```

Description:
```
element.border = 1, solid;
```

will rewriten to the

```
element.border(1, solid);
```

This is same as @property.
```
element.color  = 0xCCC;

@property void color( uint a ) { this._color = a; }
```

but with 2 arguments, and more.

It possible in current version 2.097 ?



Re: Trivial simple OpenGl working example

2021-07-08 Thread Виталий Фадеев via Digitalmars-d-learn

On Thursday, 8 July 2021 at 16:11:17 UTC, Guillaume Piolat wrote:

On Thursday, 8 July 2021 at 14:09:30 UTC, drug wrote:

08.07.2021 16:51, Виталий Фадеев пишет:

Hi!

I searching trivial simple D/OpenGL working in 2021 year 
example.


It may be triangle.
It may be based on any library: SDL, GLFW, Derelict, etc.

Can you help me ?



https://github.com/drug007/gfm7/tree/master/examples/simpleshader

it's not trivial though but it works (tested in linux)
just `dub fetch gfm7` then go to 
`path\to\gfm7\examples\simpleshader` and run `dub`.


All kudos to Guillaume Piolat, original author of gfm library.


If like me you hate OpenGL :) you can can also get 
software-rendered DPI-aware triangles with the "turtle" package:

https://code.dlang.org/packages/turtle

(Courtesy of Cerjones for the software renderer. )


Thank, Guillaume Piolat.

I using CPU Pentium B970 It is old CPU, but even it contains a 
graphics accelerator.
Mesa DRI Intel(R) HD Graphics 2000 (SNB GT1), has 4 conveers on 
GPU.

Smartphones also contains GPU.
Because OpenGL has high priority.

CPU render, may be later.

Thanks, I noticed turtle, dg2d. It has clipping, it usefull.
What about text rendering ?



Re: Trivial simple OpenGl working example

2021-07-08 Thread Виталий Фадеев via Digitalmars-d-learn

On Thursday, 8 July 2021 at 16:32:44 UTC, drug wrote:

08.07.2021 19:11, Виталий Фадеев пишет:


I fix source code, as drug say.




I've fixed the issue upstream, shortly gfm7 v1.1.2 will be 
available.


Drug, thank! I using!


Re: Trivial simple OpenGl working example

2021-07-08 Thread Виталий Фадеев via Digitalmars-d-learn

On Thursday, 8 July 2021 at 16:01:37 UTC, Dennis wrote:

On Thursday, 8 July 2021 at 14:20:16 UTC, Виталий Фадеев wrote:

Has dub flag for disable "warnings are treated as errors" ?


You have to edit the package file to include `buildRequirements 
"allowWarnings"`, see 
https://dub.pm/package-format-sdl.html#build-requirements


Dennis. thank.

# gfm7/examples/simpleshader/dub.json
{
"targetType": "executable",
"name": "simpleshader",
"sourcePaths": [ "." ],
"importPaths": [ "." ],
"mainSourceFile": "simpleshader.d",

"dependencies":
{
"gfm7:sdl2": {"path": "../../",.
"buildRequirements": [ "allowWarnings" ]
},
"gfm7:opengl": {"path": "../../"},
"gfm7:logger": {"path": "../../"}
},
"versions": [ "SDL_205", "GL_33", "GL_ARB"],

"buildRequirements": [ "allowWarnings" ]
}


No effect. Same error.
I fix source code, as drug say.




Re: Trivial simple OpenGl working example

2021-07-08 Thread Виталий Фадеев via Digitalmars-d-learn

On Thursday, 8 July 2021 at 15:57:53 UTC, drug wrote:

08.07.2021 18:46, Виталий Фадеев пишет:

On Thursday, 8 July 2021 at 15:30:07 UTC, drug wrote:

08.07.2021 17:20, Виталий Фадеев пишет:

[...]


I failed to reproduce that. What platform you use and what is 
the compiler version?


drug, Linux, Ubuntu, x64
# uname -a
Linux unknown 5.11.0-22-generic #23-Ubuntu SMP Thu Jun 17 
00:34:23 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux


# dmd --version
DMD64 D Compiler v2.097.0

# dub --version
DUB version 1.26.0, built on Jun  3 2021

#  pkg-config --modversion sdl2
2.0.14


Yes, it's reproducible with dmd 2.097. Trivial fix is deleting 
that line `../../sdl2/gfm/sdl2/timer.d:69` then it works.


Thank, drug.
I think some like this:

  env DFLAGS=-wi dub run

or like this

  "buildRequirements": [ "allowWarnings" ]

without gfm/sdl2/timer.d editing.

Yes, you right. It can be removed. I will do it.

try
{
SDL2Timer timer = cast(SDL2Timer)param;
return timer.onTimer(interval);
}
catch (Throwable e)
{
// No Throwable is supposed to cross C callbacks 
boundaries

// Crash immediately
exit(-1);
return 0; // <--HERE WARMING
}

It is worked! Thank!




Re: Trivial simple OpenGl working example

2021-07-08 Thread Виталий Фадеев via Digitalmars-d-learn

On Thursday, 8 July 2021 at 15:30:07 UTC, drug wrote:

08.07.2021 17:20, Виталий Фадеев пишет:

[...]


I failed to reproduce that. What platform you use and what is 
the compiler version?


drug, Linux, Ubuntu, x64
# uname -a
Linux unknown 5.11.0-22-generic #23-Ubuntu SMP Thu Jun 17 
00:34:23 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux


# dmd --version
DMD64 D Compiler v2.097.0

# dub --version
DUB version 1.26.0, built on Jun  3 2021

#  pkg-config --modversion sdl2
2.0.14


Re: Trivial simple OpenGl working example

2021-07-08 Thread Виталий Фадеев via Digitalmars-d-learn

On Thursday, 8 July 2021 at 14:20:25 UTC, Adam D Ruppe wrote:

On Thursday, 8 July 2021 at 13:51:51 UTC, Виталий Фадеев wrote:

It may be based on any library: SDL, GLFW, Derelict, etc.


my library

http://arsd-official.dpldocs.info/arsd.simpledisplay.html#topic-modern-opengl

arsd-official:simpledisplay dependency on dub, or just download 
color.d and simpledisplay.d from 
https://github.com/adamdruppe/arsd and compile them with your 
sample program (dmd yourprog.d color.d simpledisplay.d) and you 
should be able to run with it.


but my bindings aren't as complete as the others suggested here.


Adam, thank!


Re: Trivial simple OpenGl working example

2021-07-08 Thread Виталий Фадеев via Digitalmars-d-learn

On Thursday, 8 July 2021 at 14:09:38 UTC, Dennis wrote:

On Thursday, 8 July 2021 at 13:51:51 UTC, Виталий Фадеев wrote:
I searching trivial simple D/OpenGL working in 2021 year 
example.


https://github.com/dkorpel/glfw-d/tree/master/examples/triangle-gl

Uses bindbc-opengl + glfw-d (my package), example uses OpenGL 
3.3. Should works on Windows and Linux out of the box, if not, 
please open an issue.


Dennis, Thank! Worked!
I happy!


Re: Trivial simple OpenGl working example

2021-07-08 Thread Виталий Фадеев via Digitalmars-d-learn

On Thursday, 8 July 2021 at 14:09:30 UTC, drug wrote:

08.07.2021 16:51, Виталий Фадеев пишет:

Hi!

I searching trivial simple D/OpenGL working in 2021 year 
example.


It may be triangle.
It may be based on any library: SDL, GLFW, Derelict, etc.

Can you help me ?



https://github.com/drug007/gfm7/tree/master/examples/simpleshader

it's not trivial though but it works (tested in linux)
just `dub fetch gfm7` then go to 
`path\to\gfm7\examples\simpleshader` and run `dub`.


All kudos to Guillaume Piolat, original author of gfm library.


Thank, drug. But llok at this:

vital@unknown:~/src/dtest/working-example/gfm7/examples/simpleshader$ dub run
Fetching bindbc-opengl 0.15.0 (getting selected version)...
Fetching colorize 1.0.5 (getting selected version)...
Fetching gfm 8.0.6 (getting selected version)...
Fetching bindbc-sdl 0.19.2 (getting selected version)...
Fetching intel-intrinsics 1.4.0 (getting selected version)...
Fetching bindbc-loader 0.3.2 (getting selected version)...
Performing "debug" build using /usr/bin/dmd for x86_64.
colorize 1.0.5: building configuration "library"...
gfm7:logger 1.1.1: building configuration "library"...
bindbc-loader 0.3.2: building configuration "noBC"...
bindbc-opengl 0.15.0: building configuration "dynamic"...
intel-intrinsics 1.4.0: building configuration "library"...
gfm:math 8.0.6: building configuration "library"...
gfm7:opengl 1.1.1: building configuration "unittest"...
bindbc-sdl 0.19.2: building configuration "dynamic"...
gfm7:sdl2 1.1.1: building configuration "library"...
../../sdl2/gfm/sdl2/timer.d(69,13): Warning: statement is not 
reachable

Error: warnings are treated as errors
   Use -wi if you wish to treat warnings only as 
informational.

/usr/bin/dmd failed with exit code 1.

Error.
Has dub flag for disable "warnings are treated as errors" ?


Trivial simple OpenGl working example

2021-07-08 Thread Виталий Фадеев via Digitalmars-d-learn

Hi!

I searching trivial simple D/OpenGL working in 2021 year example.

It may be triangle.
It may be based on any library: SDL, GLFW, Derelict, etc.

Can you help me ?



Re: wanting to try a GUI toolkit: needing some advice on which one to choose

2021-05-27 Thread Виталий Фадеев via Digitalmars-d-learn

On Thursday, 27 May 2021 at 01:17:44 UTC, someone wrote:

Yes, I know this is a question lacking a straightforward answer.

Requirements:

[...]


sciter, of course.  https://sciter.com/
Or write Dlang alternative.


Re: Remove own post from forum

2021-05-10 Thread Виталий Фадеев via Digitalmars-d-learn

On Monday, 10 May 2021 at 04:29:29 UTC, Paul Backus wrote:

On Monday, 10 May 2021 at 03:36:02 UTC, Виталий Фадеев wrote:
I have missformated post in thread: 
https://forum.dlang.org/thread/kwpqyzwgczdpzgsvo...@forum.dlang.org


Say, please,
how to remove own post from this forum ?


There's currently no way to remove or edit an existing post.


ok


Remove own post from forum

2021-05-09 Thread Виталий Фадеев via Digitalmars-d-learn
I have missformated post in thread: 
https://forum.dlang.org/thread/kwpqyzwgczdpzgsvo...@forum.dlang.org


Say, please,
how to remove own post from this forum ?



Re: what exactly is string length?

2021-04-01 Thread Виталий Фадеев via Digitalmars-d-learn

On Friday, 2 April 2021 at 04:32:53 UTC, mw wrote:

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

---
import std;
import std.conv : text;


void main()
{
char[6] s;
s = "abc";
writeln(s, s.length);  // abc6, ok it's the static array's 
length


string t = text("head-", s, "-tail");
writeln(t, t.length);  // head-abc-tail16, why?
}
---

Why the last output is 16 instead of 13, t's type is string 
here.


Test this:
https://run.dlang.io/is/Cq4vjP



Re: Two functions with different args. Taking address of the one

2021-03-11 Thread Виталий Фадеев via Digitalmars-d-learn

On Thursday, 11 March 2021 at 14:23:39 UTC, Adam D. Ruppe wrote:
On Thursday, 11 March 2021 at 12:26:07 UTC, Виталий Фадеев 
wrote:

_processMouseKey  =  // <-- not works
_processMouseMove =  // <-- not works


This *should* actually work. What type are those variables?

struct MouseKeyEvent {}
struct MouseMoveEvent{}
void process( ref MouseKeyEvent event ) { }
void process( ref MouseMoveEvent event ) { }

void main() {
  // this works because the type is given on the left
  // so the compiler knows which overload works
void function(ref MouseMoveEvent) processMouseMove = 


}


I was stupid.
It is really simple!

Thank a lot, bro!


Re: Two functions with different args. Taking address of the one

2021-03-11 Thread Виталий Фадеев via Digitalmars-d-learn

On Thursday, 11 March 2021 at 13:14:56 UTC, Paul Backus wrote:
On Thursday, 11 March 2021 at 12:56:34 UTC, Виталий Фадеев 
wrote:

[...]


Something like this:

template Overloads(alias symbol)
{
static if (__traits(compiles, __traits(parent, symbol)))
alias Overloads = __traits(getOverloads,
__traits(parent, symbol),
__traits(identifier, symbol)
);
else
alias Overloads = symbol;
}

auto getOverloadFor(alias fun, T)()
{
foreach (overload; Overloads!fun)
static if (__traits(compiles, (T arg) { 
overload(arg); }))

return 
}

Usage:

__processMouseKey = getOverloadFor!(process, MouseKeyEvent);


Thanks a lot, bro!


Re: Two functions with different args. Taking address of the one

2021-03-11 Thread Виталий Фадеев via Digitalmars-d-learn

On Thursday, 11 March 2021 at 12:48:13 UTC, Paul Backus wrote:
On Thursday, 11 March 2021 at 12:26:07 UTC, Виталий Фадеев 
wrote:

Have:
void process( ref MouseKeyEvent event )
{
   ...
}

void process( ref MouseMoveEvent event )
{
   ...
}

Want:
_processMouseKey  =  // <-- not works
_processMouseMove =  // <-- not works

What is correct way to get address of function with specific 
argument ?


You can use __traits(getOverloads, process) plus some 
metaprogramming to get the address of a specific overload. But 
IMO the easiest way is to use lambdas:


__processMouseKey = (ref MouseKeyEvent event) { 
process(event); };
__processMouseMove = (ref MouseMoveEvent event) { 
process(event); };


This will generate lambda:
  __processMouseKey = (ref MouseKeyEvent event) { process(event); 
};


two calls:
  call labnda;
call process;

What right way to call function directly with selecting one of 
two ?




Two functions with different args. Taking address of the one

2021-03-11 Thread Виталий Фадеев via Digitalmars-d-learn

Have:
void process( ref MouseKeyEvent event )
{
   ...
}

void process( ref MouseMoveEvent event )
{
   ...
}

Want:
_processMouseKey  =  // <-- not works
_processMouseMove =  // <-- not works

What is correct way to get address of function with specific 
argument ?




Re: string to type

2021-02-24 Thread Виталий Фадеев via Digitalmars-d-learn
On Thursday, 25 February 2021 at 06:53:28 UTC, Виталий Фадеев 
wrote:
On Thursday, 25 February 2021 at 06:51:11 UTC, Виталий Фадеев 
wrote:

Say, please,
how to convert string to type ?

[...]


Simple version of this question is:
   string t = "X";
   mixin t!();   // <-- HERE TROUBLE


I was stupid.
Solution is:
mixin ( "mixin " ~ IFACE.stringof[ 1 .. $ ] ~ "!();" );

Thanks!


string to type

2021-02-24 Thread Виталий Фадеев via Digitalmars-d-learn

Say, please,
how to convert string to type ?

Is:
interface IX
{
//
}

mixin template X()
{
//
}

// Mixin templates to class.
//   Scan class interfaces, then mix
mixin template Members()
{
alias T = typeof( this );

static
foreach ( IFACE; InterfacesTuple! T )
{
pragma( msg, IFACE );
pragma( msg, IFACE.stringof[ 1 .. $ ] );

static
if ( IFACE.stringof.length > 1 && IFACE.stringof[ 1 
.. $ ] )

{
mixin ( IFACE.stringof[ 1 .. $ ] )!(); // <-- 
HERE TROUBLE

}
}
}

class A : IX
{
mixin Members!();
}

Want:
// Mixin template
mixin IFACE.stringof[ 1 .. $ ] !();

Got:
Compilation error: "declaration expected"

source: https://run.dlang.io/is/smFaWc

Help wanted.



Re: string to type

2021-02-24 Thread Виталий Фадеев via Digitalmars-d-learn
On Thursday, 25 February 2021 at 06:51:11 UTC, Виталий Фадеев 
wrote:

Say, please,
how to convert string to type ?

[...]


Simple version of this question is:
   string t = "X";
   mixin t!();   // <-- HERE TROUBLE


Creating 1000 instances

2021-02-19 Thread Виталий Фадеев via Digitalmars-d-learn

We have:
class File
{
// WIN32_FIND_DATAW data;
}

void fastReadDir()
{
File[] files;

// reserve space, allocating instances
files = new File[]( 1000 );  // <--- trouble here ?

// filling instances
auto file = files.ptr;

writeln( file.data );// <--- or trouble here ?

// ...
}

Got:
SegFault

Goal:
Allocate memory for 1000 instances at once.

Source:
https://run.dlang.io/is/xfaXcv

Question:
What is the true, fastest, beauty way to create 1000 
instances of the class File ?





Re: Can change vtbl record at runtime ?

2021-02-03 Thread Виталий Фадеев via Digitalmars-d-learn

On Wednesday, 3 February 2021 at 10:20:44 UTC, evilrat wrote:
On Wednesday, 3 February 2021 at 08:26:05 UTC, Max Haughton 
wrote:
On Wednesday, 3 February 2021 at 05:30:37 UTC, Виталий Фадеев 
wrote:

Possible to change the vtbl record at runtime ?
Has functional for update vtbl records ?


Do you mean "Can I set onSuccess" at runtime? The virtual 
tables are relied upon by the compiler so I wouldn't play with 
them.


Not to mention that compiler can optimize away virtual calls if 
it can determine final type on call site.


evilrat, frame, thanks.


Re: Can change vtbl record at runtime ?

2021-02-03 Thread Виталий Фадеев via Digitalmars-d-learn

On Wednesday, 3 February 2021 at 08:26:05 UTC, Max Haughton wrote:
On Wednesday, 3 February 2021 at 05:30:37 UTC, Виталий Фадеев 
wrote:

Do you mean "Can I set onSuccess" at runtime?


Yes.


Can change vtbl record at runtime ?

2021-02-02 Thread Виталий Фадеев via Digitalmars-d-learn

Reason:
Reuse component,
bind custom callback without creating new class.

Concept example:
class SaveFilePopup
{
void onSuccess() { /* default operations */ }
}

auto saveFile = new SaveFilePopup();
saveFile.onSuccess = { /* New operations */ }

Delegate:
may be... but, for speed reason, is possible to set the 
default code at compile-time ?


class X
{
   void delegate() onSuccess = { /* default code */ };
}

Context:
GUI, components, callbacks

Possible to change the vtbl record at runtime ?
Has functional for update vtbl records ?




Re: list system drives

2021-02-01 Thread Виталий Фадеев via Digitalmars-d-learn
On Tuesday, 2 February 2021 at 07:18:02 UTC, Ferhat Kurtulmuş 
wrote:
On Tuesday, 2 February 2021 at 06:31:27 UTC, Виталий Фадеев 
wrote:

Give, please, Dlang tools for list system drives.

Some like a:
enumVolumes(); // [ 'C:\', 'D:\' ]


I have found this code by a google search. I don't know who the 
author was. I had to touch it a little since the codebase was 
old.


https://gist.github.com/aferust/5cc3209a6b6caf1062271a082c093b87


Thank!


list system drives

2021-02-01 Thread Виталий Фадеев via Digitalmars-d-learn

Give, please, Dlang tools for list system drives.

Some like a:
enumVolumes(); // [ 'C:\', 'D:\' ]


Re: Deduct and return class type

2021-01-23 Thread Виталий Фадеев via Digitalmars-d-learn

On Saturday, 23 January 2021 at 19:19:29 UTC, Paul Backus wrote:
On Saturday, 23 January 2021 at 07:17:38 UTC, Виталий Фадеев 
wrote:

Where source ?
Where deduction implementation ?


Here:

https://github.com/dlang/dmd/blob/v2.095.0/src/dmd/dtemplate.d#L1308


Thank!


Re: Deduct and return class type

2021-01-22 Thread Виталий Фадеев via Digitalmars-d-learn
On Saturday, 23 January 2021 at 05:54:09 UTC, Виталий Фадеев 
wrote:
On Saturday, 23 January 2021 at 05:39:18 UTC, Виталий Фадеев 
wrote:

Context:
data + GUI List

Goal:
auto list = new List( data );


I want 'this( T )( T data )' deduction:

class A( T )
{
this( T )( T data )
{
// ...
}
}

What way to implement this ?

Rules:
   1. if class is template
   2. if ctor is template
   3. if ctor template arg have same name with class template arg 
name (example: T )

   4. deduct T of ctor
   5. set type T for class template ( or put in suggestion queue )

Verify, please.

Where source ?
Where deduction implementation ?



Re: Deduct and return class type

2021-01-22 Thread Виталий Фадеев via Digitalmars-d-learn
On Saturday, 23 January 2021 at 05:39:18 UTC, Виталий Фадеев 
wrote:

Context:
data + GUI List

Goal:
auto list = new List( data );


Of course, we can use 'factory':

import std.stdio;


template List( alias T )
{
class List
{
T data;

this( T data )
{
this.data = data;
}


// data usage...
}
}


auto  listFactory( T )( T data )
{
return new List!( T )( data );
}

void main()
{
string[] extensions = [ ".d", ".di" ];

auto list = listFactory( extensions );

//auto list = new List( extensions );
}

source: https://run.dlang.io/is/y167tu


But, how to create class instance with type deduction in usual 
way ?


auto list = new List( extensions );




Deduct and return class type

2021-01-22 Thread Виталий Фадеев via Digitalmars-d-learn

Context:
data + GUI List

Goal:
auto list = new List( data );

Concept:
class is created in the usual way : new List( data )
store inside the class: T data;
type T deducted   : ( T )( T data )


Tried way:
template List( T )
{
class List
{
T data;

this( T data )
{
this.data = data;
}


// data usage...
}
}


void main()
{
string[] extensions = [ ".d", ".di" ];

auto list = new List( extensions );
}


Source: https://run.dlang.io/is/Bw2zHB

Question:
How to implement on D beauty clean flexible code ?
like a:
auto list = new List( data );

How to return from 'List( data )' class type ?



Re: Why many programmers don't like GC?

2021-01-14 Thread Виталий Фадеев via Digitalmars-d-learn

On Wednesday, 13 January 2021 at 18:58:56 UTC, Marcone wrote:
I've always heard programmers complain about Garbage Collector 
GC. But I never understood why they complain. What's bad about 
GC?


I like GC.

How write quickly without GC ?



Re: gdb + Windows 64: No debugging symbols found

2020-12-26 Thread Виталий Фадеев via Digitalmars-d-learn

On Saturday, 26 December 2020 at 14:10:46 UTC, Basile B. wrote:
On Saturday, 26 December 2020 at 11:55:58 UTC, Виталий Фадеев 
wrote:

We have:
[...]
Problem is:
$ gdb ./app.exe
GNU gdb (GDB) 9.2
...
(No debugging symbols found in ./app.exe)

What is a right way to build .exe and debug with gdb ?


Try to build with latest version of LDC and use the -gdwarf 
option.

This will generate debug info that GDB can understand.


Mike Parker, Basile B., thanks!



gdb + Windows 64: No debugging symbols found

2020-12-26 Thread Виталий Фадеев via Digitalmars-d-learn

We have:

// app.d
import std.stdio;

void main()
{
writeln( "OK" );
}

Build:
dmd -m64 -g app.d

OS:
Windows 10, x86_64
MSYS2: gdb

Goal:
gdb app.exe


Problem is:
$ gdb ./app.exe
GNU gdb (GDB) 9.2
...
(No debugging symbols found in ./app.exe)

What is a right way to build .exe and debug with gdb ?



Re: find regex in backward direction ?

2020-12-19 Thread Виталий Фадеев via Digitalmars-d-learn

On Sunday, 20 December 2020 at 04:33:21 UTC, Виталий Фадеев wrote:

On Saturday, 19 December 2020 at 23:16:18 UTC, kdevel wrote:
On Saturday, 19 December 2020 at 12:52:54 UTC, Виталий Фадеев 
wrote:

...


"retro" possible when using simple expression "abc".
For complex "ab\w" or "(?Pregex)" should be parsing: [ "a", 
"b", "\w" ],  [ "(", "?", "P", "", "regex", ")"]..., i 
think.


up.


Re: find regex in backward direction ?

2020-12-19 Thread Виталий Фадеев via Digitalmars-d-learn

On Saturday, 19 December 2020 at 23:16:18 UTC, kdevel wrote:
On Saturday, 19 December 2020 at 12:52:54 UTC, Виталий Фадеев 
wrote:

Goal:
size_t pos = findRegexBackward( r"abc"d );
assert( pos == 4 );



module LastOccurrence;

size_t findRegexBackward_1 (dstring s, dstring pattern)
{
   import std.regex : matchAll;
   auto results = matchAll (s, pattern);
   if (results.empty)
  throw new Exception ("could not match");
   size_t siz;
   foreach (rm; results)
  siz = rm.pre.length;
   return siz;
}

size_t findRegexBackward_2 (dstring s, dstring pattern)
// this does not work with irreversible patterns ...
{
   import std.regex : matchFirst;
   import std.array : array;
   import std.range: retro;
   auto result = matchFirst (s.retro.array, 
pattern.retro.array);

   if (result.empty)
  throw new Exception ("could not match");
   return result.post.length;
}

unittest {
   import std.exception : assertThrown;
   static foreach (f; [_1, 
_2]) {

  assert (f ("abc3abc7", r""d) == 8);
  assert (f ("abc3abc7", r"abc"d) == 4);
  assertThrown (f ("abc3abc7", r"abx"d));
  assert (f ("abababababab", r"ab"d) == 10);
   }
}


Thanks.
But, not perfect.

We can't use reverse, becausу "ab\w" will be "w\ba" ( expect 
matching "abc". revesed is "cba" ).



size_t findRegexBackward_2 (dstring s, dstring pattern)
...
   assert (f ("abc3abc7", r"ab\w"d) == 4);
...


Of course, I using matchAll. But it scan all text in forward 
direction.



  size_t findRegexBackward_1 (dstring s, dstring pattern)


/** */
size_t findRegexBackwardMatchCase( dstring s, dstring needle, 
out size_t matchedLength )

{
auto matches = matchAll( s, needle );
if ( matches.empty )
{
return -1;
}
else
{
auto last = matches.front;
foreach ( m; matches )
{
last = m;
}
matchedLength = last.hit.length;
return last.pre.length;
}
}

Thank!
Fastest solution wanted!

May be... some like a "RightToLeft" in Win32 API...

https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regexoptions?view=net-5.0#System_Text_RegularExpressions_RegexOptions_RightToLeft

but how on Linux? MS-regex and Linux-regex is identical ?



find regex in backward direction ?

2020-12-19 Thread Виталий Фадеев via Digitalmars-d-learn

We have:
dstring s = "abc3abc7";

Source:
https://run.dlang.io/is/PtjN4T

Goal:
size_t pos = findRegexBackward( r"abc"d );
assert( pos == 4 );


How to find regex in backward direction ?



Re: Hex constant method starts from ".a": 0xCCCCCC.argb

2020-12-14 Thread Виталий Фадеев via Digitalmars-d-learn
On Tuesday, 15 December 2020 at 05:10:27 UTC, Виталий Фадеев 
wrote:
On Tuesday, 15 December 2020 at 05:04:46 UTC, Виталий Фадеев 
wrote:
On Monday, 14 December 2020 at 16:19:18 UTC, Adam D. Ruppe 
wrote:
On Monday, 14 December 2020 at 16:11:16 UTC, Jacob Carlborg 
wrote:
Or you can call it `rgba`. It seems to be what Wikipedia 
prefers [1].


The ordering here tends to reflect the bytes. So argb puts 
the alpha byte first in the array whereas rgba puts red first.


But there's other ways here including just saying argb(...) 
or foo.as!"argb" or whatever well.


I think about .rgba.

Yes. The ordering! The logic.

ARGB = .argb

Thanks!


0x00AABBCC.argb

0x00AABBCC - is ARGB value
.argb  - is unit

...in my logic.

120.px
120   - is Pixel value
.px   - is unit

...in my logic.

100.percent
100   - is Percent value
.percent  - is unit

...in my logic.
)

Thanks!


This concept:
class X
{
this()
{
props.fontFace   = "Ubuntu Mono";
props.fontSize   = 24.px;
props.lineHeight = 15.px;
props.color  = 0xFF;
props.backColor  = 0x22;
// or
props.color  = 0xFF.rgb;
props.backColor  = 0x22.rgb;
// or
props.color  = 0x88FF.argb;
props.backColor  = 0x8822.argb;
}
}

Thanks!
)


Re: Hex constant method starts from ".a": 0xCCCCCC.argb

2020-12-14 Thread Виталий Фадеев via Digitalmars-d-learn
On Tuesday, 15 December 2020 at 05:04:46 UTC, Виталий Фадеев 
wrote:
On Monday, 14 December 2020 at 16:19:18 UTC, Adam D. Ruppe 
wrote:
On Monday, 14 December 2020 at 16:11:16 UTC, Jacob Carlborg 
wrote:
Or you can call it `rgba`. It seems to be what Wikipedia 
prefers [1].


The ordering here tends to reflect the bytes. So argb puts the 
alpha byte first in the array whereas rgba puts red first.


But there's other ways here including just saying argb(...) or 
foo.as!"argb" or whatever well.


I think about .rgba.

Yes. The ordering! The logic.

ARGB = .argb

Thanks!


0x00AABBCC.argb

0x00AABBCC - is ARGB value
.argb  - is unit

...in my logic.

120.px
120   - is Pixel value
.px   - is unit

...in my logic.

100.percent
100   - is Percent value
.percent  - is unit

...in my logic.
)

Thanks!



Re: Hex constant method starts from ".a": 0xCCCCCC.argb

2020-12-14 Thread Виталий Фадеев via Digitalmars-d-learn

On Monday, 14 December 2020 at 16:19:18 UTC, Adam D. Ruppe wrote:
On Monday, 14 December 2020 at 16:11:16 UTC, Jacob Carlborg 
wrote:
Or you can call it `rgba`. It seems to be what Wikipedia 
prefers [1].


The ordering here tends to reflect the bytes. So argb puts the 
alpha byte first in the array whereas rgba puts red first.


But there's other ways here including just saying argb(...) or 
foo.as!"argb" or whatever well.


I think about .rgba.

Yes. The ordering! The logic.

ARGB = .argb

Thanks!



Re: Hex constant method starts from ".a": 0xCCCCCC.argb

2020-12-13 Thread Виталий Фадеев via Digitalmars-d-learn

On Monday, 14 December 2020 at 05:37:21 UTC, Paul Backus wrote:
On Monday, 14 December 2020 at 05:27:40 UTC, Виталий Фадеев 
wrote:

".rgb"  Compiled fine.
".argb" Compilation error.


Source:
https://run.dlang.io/is/ULQ4kh


It's parsing the `.a` in `.argb` as part of the number:

auto color = 0x00AABBCC.a rgb; // what the compiler sees

You can fix it with parentheses:

auto color = (0x00AABBCC).argb;


Thanks!

It is not perfect, but also beauty!



Re: Hex constant method starts from ".a": 0xCCCCCC.argb

2020-12-13 Thread Виталий Фадеев via Digitalmars-d-learn

On Monday, 14 December 2020 at 05:24:39 UTC, Виталий Фадеев wrote:


... msg ...



But...:
Color rgb( uint color )
{
return
Color( cast( uint ) (
( ( color & 0x00FF ) << 16 )  |
( ( color & 0xFF00 ) )  |
( ( color & 0x00FF ) >> 16 )
) );
}

unittest
{
auto color2 = 0x00AABBCC.rgb;
}

".rgb"  Compiled fine.
".argb" Compilation error.


Source:
https://run.dlang.io/is/ULQ4kh




Hex constant method starts from ".a": 0xCCCCCC.argb

2020-12-13 Thread Виталий Фадеев via Digitalmars-d-learn

We have:

static import winapi=core.sys.windows.windows;

struct Color
{
union
{
 winapi.COLORREF native;
 struct
{
ubyte r;
ubyte g;
ubyte b;
}
}
ubyte a = 0xFF;
}


Color argb( uint color )
{
Color c;

c.native =
cast( uint ) (
( ( color & 0x00FF ) << 16 )  |
( ( color & 0xFF00 ) )  |
( ( color & 0x00FF ) >> 16 )
);

c.a = ( color & 0xFF00 ) >> 24;

return c;
}


Goal:
auto color = 0x00AABBCC.argb;

Has error:
Error: exponent required for hex float
( at 0x00AABBCC.argb )

What is it ?
How to implement beauty code like the: #CC.rgb ?



Re: where is the memory corruption?

2020-12-10 Thread Виталий Фадеев via Digitalmars-d-learn

On Wednesday, 9 December 2020 at 20:35:21 UTC, Jack wrote:
I'm on linux/opensuse, trying to pass a wchar_* from C to D but 
I'm getting only the first letter of that string. Could someone 
help figure out why?


[...]



May be this help to you:

auto s2 = to!string(s);
to
auto s2 = fromWChar( s );

wstring fromWChar( const wchar* s )
{
import std.conv : to;
return s[ 0 .. wcslen( s ) ].to!wstring;
}


Example: https://run.dlang.io/is/PkCeTZ


Re: binary search

2020-12-06 Thread Виталий Фадеев via Digitalmars-d-learn

On Monday, 7 December 2020 at 06:24:27 UTC, drug wrote:

Phobos provides this by SortedRange:
https://dlang.org/phobos/std_range.html#.SortedRange

Example of usage:
https://run.dlang.io/is/WW2bn0


Thanks!
:-)


binary search

2020-12-06 Thread Виталий Фадеев via Digitalmars-d-learn

We have:
// sorted values
size_t lines  = [20, 1755, 1756, 1757, 1798, 1824, 1825, 
1839, 1840];

size_t search = 21;

Goal:
// Fast find index of the '21' in ordered array 'lines'
auto found = lines.binarySearch( 20 ); // 0 - index
auto low   = lines.binarySearchLow( 21 );  // 0 - near lowest 
index


Where is the implementation of "binary search", please?




Re: converting D's string to use with C API with unicode

2020-12-05 Thread Виталий Фадеев via Digitalmars-d-learn

On Saturday, 5 December 2020 at 19:51:14 UTC, Jack wrote:

So in D I have a struct like this:


struct ProcessResult
{
string[] output;
bool ok;
}


in order to use output from C WINAPI with unicode, I need to 
convert each string to wchar* so that i can acess it from C 
with wchar_t*. Is that right or am I missing anything?




struct ProcessResult
{
string[] output;
bool ok;

C_ProcessResult toCResult()
{
auto r = C_ProcessResult();
r.ok = this.ok; // just copy, no conversion needed
foreach(s; this.output)
r.output ~= cast(wchar*)s.ptr;
return r;
}
}



version(Windows) extern(C) export
struct C_ProcessResult
{
wchar*[] output;
bool ok;
}


Drawing string via WinAPI. As example.

// UTF-16. wchar*
wstring ws = "Abc"w;
ExtTextOutW( hdc, x, y, 0, , cast( LPCWSTR ) ws.ptr, 
cast( uint ) ws.length, NULL );


// UTF-8. char*
string s = "Abc";
import std.utf : toUTF16;
string ws = s.toUTF16;
ExtTextOutW( hdc, x, y, 0, , cast( LPCWSTR ) ws.ptr, 
cast( uint ) ws.length, NULL );


// UTF-32. dchar*
dstring ds = "Abc"d;
import std.utf : toUTF16;
string ws = ds.toUTF16;
ExtTextOutW( hdc, x, y, 0, , cast( LPCWSTR ) ws.ptr, 
cast( uint ) ws.length, NULL );


One char.
// UTF-16. wchar
wchar wc = 'A';
ExtTextOutW( hdc, x, y, 0, , cast( LPCWSTR ) , 1, 
NULL );


// UTF-32. dchar
dchar dc = 'A';
import std.utf : encode;
wchar[ 2 ] ws;
auto l = encode( ws, dc );
ExtTextOutW( hdc, x, y, 0, , cast( LPCWSTR ) , 
cast( uint ) l, NULL );


//
// Font API
string face = "Arial";
LOGFONT lf;
import std.utf : toUTF16;
lf.lfFaceName[ 0 .. face.length ] = face.toUTF16;
HFONT hfont = CreateFontIndirect(  );

// Common case
LPWSTR toLPWSTR( string s ) nothrow // wchar_t*. UTF-16
{
import std.utf : toUTFz, toUTF16z, UTFException;
try{ return toUTFz!( LPWSTR )( s ); }
catch ( UTFException e )   { return cast( LPWSTR ) 
"ERR"w.ptr; }
catch ( Exception e )  { return cast( LPWSTR ) 
"ERR"w.ptr; }

}
alias toLPWSTR toPWSTR;
alias toLPWSTR toLPOLESTR;
alias toLPWSTR toPOLESTR;

// WinAPI
string windowName = "Abc";
HWND hwnd =
CreateWindowEx(
...
windowName.toLPWSTR,
...
);




Re: How to build dll?

2020-12-03 Thread Виталий Фадеев via Digitalmars-d-learn

On Wednesday, 2 December 2020 at 19:42:37 UTC, Jack wrote:

D code:


import core.sys.windows.dll;

mixin SimpleDllMain;

version(Windows) extern(C) export
int foo() { return 42; }


compiled with:

dmd -ofmydll.dll -shared -m32 dll.d


called from:


typedef int (__cdecl *MYPROC)(void);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE 
hPrevInstance,

  PWSTR szCmdLine, int CmdShow)
{
   HINSTANCE hinstLib;
   MYPROC ProcAdd;
   BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;

   // Get a handle to the DLL module.

   hinstLib = LoadLibrary(TEXT("mydll.dll"));



but hinstLib is NULL and GetLastError() = 193:


ERROR_BAD_EXE_FORMAT 193 (0xC1)
%1 is not a valid Win32 application.


What am I missing?


May be this will help: https://github.com/vitalfadeev/dlang-dll


Re: Selective unittesting in DUB

2020-11-30 Thread Виталий Фадеев via Digitalmars-d-learn

On Wednesday, 21 December 2016 at 18:07:23 UTC, Nordlöw wrote:
Is there a way to specify in dub.json (or any other file) that 
only a subset of the sources compiled and linked to a library 
or app should have they're unittests enabled?


Check this one:

app.d
--
Version ( DoubleBuffer )
{
  unittest
  {
// ...
  }
}
else
{
  unittest
  {
// ...
  }
}



or

unittest
{
  Version ( DoubleBuffer )
  {
  // ...
  }
  // ...
}


dub.json

{
...,
"buildTypes":
{
"debug":
{
"versions": [ "DoubleBuffer" ]
}
}
}


Or may be "pragma" can help you...
version ( Windows )
{
  pragma( lib, "gdi32.lib" );
}

How to specify "DoubleBuffer" via dub's command line?...


Selective unittesting in DUB

2020-11-30 Thread Виталий Фадеев via Digitalmars-d-learn

https://forum.dlang.org/post/owhmsgkhszkwcjkxl...@forum.dlang.org

On Wednesday, 21 December 2016 at 18:07:23 UTC, Nordlöw wrote:
Is there a way to specify in dub.json (or any other file) that 
only a subset of the sources compiled and linked to a library 
or app should have they're unittests enabled?


Check this one:

app.d
--
Version ( DoubleBuffer )
{
  unittest
  {
// ...
  }
}
else
{
  unittest
  {
// ...
  }
}



or

unittest
{
  Version ( DoubleBuffer )
  {
  // ...
  }
  // ...
}


dub.json

{
...,
"buildTypes":
{
"debug":
{
"versions": [ "DoubleBuffer" ]
}
}
}

How to specify "DoubleBuffer" via dub's command line?...

I think you can improve the dub.



Re: Iterating chars by Word

2020-11-12 Thread Виталий Фадеев via Digitalmars-d-learn

On Friday, 13 November 2020 at 07:23:13 UTC, Виталий Фадеев wrote:
On Friday, 13 November 2020 at 06:52:38 UTC, Виталий Фадеев 
wrote:

On Friday, 13 November 2020 at 06:42:24 UTC, evilrat wrote:

[...]


Thanks, Ali. Thanks, Evilrat.
I taste it now: https://run.dlang.io/is/HlSFVY



Latest: https://run.dlang.io/is/riY5BI


Splitted on each White space...

Thanks. I going to the Lexers/Parsers world.



Re: Iterating chars by Word

2020-11-12 Thread Виталий Фадеев via Digitalmars-d-learn

On Friday, 13 November 2020 at 06:52:38 UTC, Виталий Фадеев wrote:

On Friday, 13 November 2020 at 06:42:24 UTC, evilrat wrote:

[...]


Thanks, Ali. Thanks, Evilrat.
I taste it now: https://run.dlang.io/is/HlSFVY



Latest: https://run.dlang.io/is/dfrcYj


Re: Iterating chars by Word

2020-11-12 Thread Виталий Фадеев via Digitalmars-d-learn

On Friday, 13 November 2020 at 06:42:24 UTC, evilrat wrote:
On Friday, 13 November 2020 at 05:14:08 UTC, Виталий Фадеев 
wrote:

[...]


You can make your own range, however look at this function 
first (second example)

https://dlang.org/phobos/std_algorithm_iteration.html#.splitter

// 1) might need to cast your wchar[] to wstring first
// 2) also assumes that 'the word' is separated by 
whitespace

foreach( word; chars.splitter(' '))
{

}

or this one, which is a bit more smarter about what "the word" 
means

https://dlang.org/phobos/std_array.html#.split

import std.array : split;

wchar[] str = cast(wchar[]) "some random stuff blah blah"w;
foreach(w; str.split())
{
writeln(w);
}

Anyway in both cases using dmd -vgc flag shows no GC 
allocations done.


Thanks, Ali. Thanks, Evilrat.
I taste it now: https://run.dlang.io/is/HlSFVY


Iterating chars by Word

2020-11-12 Thread Виталий Фадеев via Digitalmars-d-learn

Is:
wchar[] chars;  // like a: "import 
core.sys.windows.windows;\nimport std.conv  : to;\n"


Goal:
foreach ( word; chars.byWord )
{
// ...
}

Iterating chars by Word...
How to ? ( simple, fast, low memory, beauty, perfect )



Re: Extract sub string from a string

2020-11-09 Thread Виталий Фадеев via Digitalmars-d-learn

On Monday, 9 November 2020 at 16:00:28 UTC, Vino wrote:

Hi All,

   Request your help on how to extract sub string from a 
string, below is an example in PHP, need your help on how to do 
the same in D.


$text = "welcome2worldinfo";
$hg = strtoupper(substr($text , 0, 7).'-'.substr($text, 7, 
1).'-'.substr($text, 8, 5));

print_r($hg) \\ Output : WELCOME-2-WORLD

From,
Vino.B


Some like this:

substr($text , 0, 7) // --> text[ 0 .. 7 ]
substr($text, 7, 1)  // --> text[ 7 .. 7+1 ]
substr($text, 8, 5)  // --> text[ 8 .. 8+5 ]

$hg = a . b . c  // --> string hg = a ~ b ~ c;

strtoupper( s )  // --> s.toUpper

print_r($hg) // --> writeln( hg )

And import names:
import std.uni : toUpper;
import std.stdio : writeln;

Help:
https://dlang.org/phobos/std_uni.html
https://dlang.org/phobos/std_stdio.html
https://dlang.org/spec/arrays.html#strings



Re: New vs length on dymamic array

2020-11-09 Thread Виталий Фадеев via Digitalmars-d-learn

On Monday, 9 November 2020 at 09:05:58 UTC, Imperatorn wrote:

On Monday, 9 November 2020 at 08:06:54 UTC, Andrey wrote:

Hello,

Are here any differences in creation of dynamic array with 
known size?



auto array = new wchar[](111);


and


wchar[] array;
array.length = 111;


You can check using compiler explorer:
https://godbolt.org/


Cool tool!

Array creation disassemble: https://godbolt.org/z/GzxWao



Re: Question about: ("1.1").to!int;

2020-10-24 Thread Виталий Фадеев via Digitalmars-d-learn

On Saturday, 24 October 2020 at 14:10:02 UTC, matheus wrote:
On Saturday, 24 October 2020 at 04:04:18 UTC, Виталий Фадеев 
wrote:

On Friday, 23 October 2020 at 16:59:06 UTC, matheus wrote:
On Friday, 23 October 2020 at 13:57:41 UTC, Joseph Rushton 
Wakeling wrote:

On Wednesday, 21 October 2020 at 22:50:27 UTC, matheus wrote:

I don't get...
(1.1).to!int = 1.
("1.1").to!int = Current is an error and IMO should be 1.
Matheus.


You should write converter from string to int.
Then you will understand.

You converter input:
"1"
"1.1"
"1.1.1"
"1a"
"1.1a"
"a"
".1"
".1.1"

You converter must be fast.



Re: Question about: ("1.1").to!int;

2020-10-23 Thread Виталий Фадеев via Digitalmars-d-learn

On Friday, 23 October 2020 at 16:59:06 UTC, matheus wrote:
On Friday, 23 October 2020 at 13:57:41 UTC, Joseph Rushton 
Wakeling wrote:

On Wednesday, 21 October 2020 at 22:50:27 UTC, matheus wrote:
Well since the caller is handling a string, shouldn't the 
caller verify the content before any conversion?


What if:
"1.1.1".to!int
?

The algorithm will become more complicated?
Will the speed decrease?



Re: Question about: ("1.1").to!int;

2020-10-23 Thread Виталий Фадеев via Digitalmars-d-learn

On Friday, 23 October 2020 at 16:59:06 UTC, matheus wrote:
On Friday, 23 October 2020 at 13:57:41 UTC, Joseph Rushton 
Wakeling wrote:

On Wednesday, 21 October 2020 at 22:50:27 UTC, matheus wrote:
Since (1.1).to!int = 1, shouldn't the string value 
("1.1").to!int at least try to convert to float/double and 
then to int?


The thing is, that's a great way for hard-to-identify bugs to 
creep into code.  In these cases:


auto a = (1).to!int; // this works
auto b = ("1").to!int;   // this works
auto c = (1.1).to!int;   // this works and c = 1

... then what the programmer wants is unambiguous.  In the 
first case it's just converting int => int.  In the second, 
it's converting from a string that unambiguously represents an 
integer value, to an int.  And in the third, it's converting 
_at programmer request_ from a double to an int (which has a 
well-defined behaviour).


However, if ("1.1").to!int were to work, this would be the 
`to` function making a judgement call on how to handle 
something ambiguous.  And while that judgement call may be 
acceptable for your current use-case, it won't be for others.


I got it everything you said, but like a said previously:

(1.1).to!int vs ("1.1").to!int

One is a decimal literal while the other is a string 
representation of a decimal.


To be honest I think the function is already making a judgment 
call when I do (1.1).to!int and returns 1, I really fail to see 
the difference when is ("1.1").to!int.


I agree with user1234: "The third case is just like `cast(int) 
1.1` it's not _at programmer request_ from my point of view, 
it's just that the `to` template has not be more restrictive 
than the D `cast` expression. `to` should do at least what a 
`cast` do and do more when there's no rule for the two types 
that are involved."


In particular, if `to` just accepted any string numerical 
representation for conversion to int, how could the caller 
explicitly _exclude_ non-integer input, if that is their 
use-case?


Well since the caller is handling a string, shouldn't the 
caller verify the content before any conversion?


Because a string may contain a integer, decimal representation 
or neither one.


Finally I don't want to make a fuss of it, I just thought it 
was a bit weird but it can be solved easily.


Thanks,

Matheus.


For _execution speed_ reason we need low-level functions.
What if on each call ("1").to!int we will do parsing "1" for 
decimal point "." ?

.
Should be Low-level functions and High-level functions.
"to" is Low-level function.
.
You can create "To" function with parsing & validating.
You right: if "to" generating exception for non-numeric symbol, 
then it doing test each char... and "to" can just break loop at 
non-numeric symbol.

.
And...
.
"1".to!int
"1.1".to!int
"1abc".to!int
...they all can be == 1
.
Some like this:
foreach( c; str )
  if ( c.between( '0', '9' ) )
result *= factor; result += ( c-'0' );
  else
break;

.
You can write patch.



Re: Question about: ("1.1").to!int;

2020-10-23 Thread Виталий Фадеев via Digitalmars-d-learn

On Wednesday, 21 October 2020 at 22:50:27 UTC, matheus wrote:

Hi,

import std.stdio, std.conv;
void main(string[ ] args) {
auto a = (1).to!int; // this works
auto b = ("1").to!int;   // this works
auto c = (1.1).to!int;   // this works and c = 1
auto d = ("1.1").to!int; // Doesn't work
}

[...]


1.1 is not int.
"to" works fine.

As solution,... "1.1" should be splitted to lexems: "1", ".", 
"1". Then analyze and then converted to int.




Re: Docs generation example

2020-10-10 Thread Виталий Фадеев via Digitalmars-d-learn

On Saturday, 10 October 2020 at 05:04:54 UTC, Anonymouse wrote:
On Saturday, 10 October 2020 at 02:07:02 UTC, Виталий Фадеев 
wrote:

Wanted!
Docs generation example.

I have dub project, sources/*.d.
I want html-index with all classes/functions.

Is exists simple, hi-level, one-line command line solution ?


dub run adrdox -- -i sources

Files will be in generated-docs/


Nice !
https://github.com/vitalfadeev/dlang-doc-example/raw/main/docs.png

Thank a lot !



Docs generation example

2020-10-09 Thread Виталий Фадеев via Digitalmars-d-learn

Wanted!
Docs generation example.

I have dub project, sources/*.d.
I want html-index with all classes/functions.

Is exists simple, hi-level, one-line command line solution ?



Wanted! Tree Node implementation.

2020-10-07 Thread Виталий Фадеев via Digitalmars-d-learn

Wanted! Tree Node implementation.

Like a:

mixin template TreeNode( T )
{
T parent;
T firstChild;
T lastChild;
T prevSibling;
T nextSibling;

// ForwardRange implementation
@property T front() { ... }
@property bool empty() { ... }
void popFront() { ... }

// BackwardRange implementation
@property T back() { ... }
void popBack();

// RandomAccessRange implementation
T opIndex( ... ) { ... }

// length implementation
@property size_t length() { ... }
}

It would be nice to get a link or package or source...




Re: Trying to create a trivial 64 bit D Lang DLL on a Windows 10 machine and cant get past linking.

2020-10-01 Thread Виталий Фадеев via Digitalmars-d-learn

On Tuesday, 29 September 2020 at 21:22:21 UTC, WhatMeWorry wrote:

module user;

export { int myAddSeven(int a, int b); }

[...]


As example, DLL: https://github.com/vitalfadeev/dlang-dll


Re: How to implement fastcall ?

2020-09-21 Thread Виталий Фадеев via Digitalmars-d-learn
On Monday, 21 September 2020 at 11:14:06 UTC, Виталий Фадеев 
wrote:

How to implement fastcall ?
( stdcall is calling convention for pass function arguments via 
registers )


On Monday, 21 September 2020 at 11:14:06 UTC, Виталий Фадеев 
wrote:

fix...
( fastcall is calling convention for pass function arguments via 
registers )




How to implement fastcall ?

2020-09-21 Thread Виталий Фадеев via Digitalmars-d-learn

How to implement fastcall ?
( stdcall is calling convention for pass function arguments via 
registers )





Re: Good repos to learn D

2020-09-21 Thread Виталий Фадеев via Digitalmars-d-learn

On Saturday, 19 September 2020 at 08:26:36 UTC, Imperatorn wrote:
What are some good examples of pretty large/medium size, good 
structured repos in D? I'm looking for examples to learn from


Thanks!


I often looked into the D std source code: 
C:\D\dmd2\src\phobos\std




Re: dub sub-projects

2020-09-21 Thread Виталий Фадеев via Digitalmars-d-learn
On Sunday, 20 September 2020 at 18:24:31 UTC, Vladimirs Nordholm 
wrote:
Hello. I wonder what the best-practice is for dub projects with 
sub-projects. Excuse me if the terminology is wrong. Let me 
explain my situation.


[...]


Check this: https://github.com/vitalfadeev/dub-subprojects
may like it as an example.


Light-weight runtime

2020-06-28 Thread Виталий Фадеев via Digitalmars-d-learn

I want light-weight runtime !

How to ?



Re: Why infinite loops are faster than finite loops?

2020-06-21 Thread Виталий Фадеев via Digitalmars-d-learn

On Saturday, 20 June 2020 at 21:11:57 UTC, tastyminerals wrote:
I am not sure that this is a question about D or a more general 
one. I have watched this nice presentation "Speed Is Found In 
The Minds of People" by Andrei: 
https://www.youtube.com/watch?v=FJJTYQYB1JQ=youtu.be?t=2596 and on 43:20 he says that "push_heap" is slow because of structured loops and finite for (throughout the presentation Andrei shows algorithm examples with infinite loops). I wonder why is that? Is it because the finite loop needs to keep track of the number of iterations it performs? Wouldn't the compiler optimize it better than the infinite one because it knows the number of iterations the for loop needs?


I think the Answer depended from the Task.

I think Alexandrescu is joking. :)
"Always* Use Infinite Loops. (*Except in most cases)" :)


For code:

di = "String";
needle = "A";
ascan = di.ptr;

for( int cx = di.length; cx != 0; cx--, scan++ )
{
if ( *scan == neelde )
goto found;
}
return;

found:
// Found!

in best case generated instructions will be like this:

CLD ;Scan string in forward 
direction

MOV AL,'A'  ;Scan for 'A'
LEA DI, STRING  ;Address to start scanning at
MOV CX,100  ;Scanning 100 bytes
REPNE   SCASB   ;   ...and scan it
JE  found   ;
JMP exit;
found:
DEC DI  ;Back up DI to point to the 
'A'


...var CX translated to register CX.
...pointer translated to register DI.
...needle translated to register AX.
All in registers!


What asm-code generaged for C-code showed by Alexandrescu ?



Re: Win32 DLLs in D

2020-06-05 Thread Виталий Фадеев via Digitalmars-d-learn

On Friday, 5 June 2020 at 18:00:40 UTC, Poyeyo wrote:

Hello everyone.

I want to create a windows plugin.dll that could be called from 
rFactor, and I want to try D first, instead of going directly 
to C++ as the rFactor example.


I am trying to wrap my head around this:

https://wiki.dlang.org/Win32_DLLs_in_D

... which seems quite outdated,

and this:

https://forum.dlang.org/post/mscgsclxwtjcferfx...@forum.dlang.org

... which seems incomplete,

... and dub, which makes a mess of all my efforts.

Can someone with the right knowledge please update the wiki 
with a modern example that also uses dub for both the Dll and 
the test program?


Dub is not a requirement, just a convenience that would be much 
appreciated.


Best regards.


D language dll example.
Creating DLL for hook create, activate, destroy window and hook 
keyboard events.


https://github.com/vitalfadeev/dlang-dll


Re: I want Sublime 3 D auto import !

2020-06-04 Thread Виталий Фадеев via Digitalmars-d-learn

On Thursday, 4 June 2020 at 04:48:22 UTC, bauss wrote:

On Wednesday, 3 June 2020 at 11:54:57 UTC, Виталий Фадеев wrote:

On Tuesday, 2 June 2020 at 20:08:09 UTC, bauss wrote:
What happens if you have the same symbol in multiple modules? 
Ex. two libraries that implement symbols with same name.


Is there a way to be selective?


I want it too! :)


Updated:
- implemented simple way to be selective !




Re: How to make the compile time red color warning ?

2020-06-03 Thread Виталий Фадеев via Digitalmars-d-learn

On Wednesday, 3 June 2020 at 11:56:26 UTC, MrSmith wrote:

On Wednesday, 3 June 2020 at 11:48:27 UTC, Виталий Фадеев wrote:

Use case:
I have class for Windows OS.
I not implement class for Linux.
I want message about it. When code compiled under Linux.


You could use `static assert(false, "Message");` to make it an 
error.


That is true!
I happy!



Re: I want Sublime 3 D auto import !

2020-06-03 Thread Виталий Фадеев via Digitalmars-d-learn

On Tuesday, 2 June 2020 at 20:08:09 UTC, bauss wrote:
What happens if you have the same symbol in multiple modules? 
Ex. two libraries that implement symbols with same name.




First module will inserted.


Is there a way to be selective?


I want it too! :)


And what about keyboard shortcut?


A specially for you !

Sublime 3 / Preferences / Key bindings:
[
{ "keys": ["alt+a"], "command": "dlang_auto_import" },
]


How to make the compile time red color warning ?

2020-06-03 Thread Виталий Фадеев via Digitalmars-d-learn

Use case:
I have class for Windows OS.
I not implement class for Linux.
I want message about it. When code compiled under Linux.


Example:


version ( Windows )
{
public import ui.sys.windows.event.keyboardevent;
}
else
{
pragma( msg, "Unsupported OS" );
}


How to make red color message ?
pragma( msg, "Unsupported OS" );



Or may be call compiller warning_message() function ? Such exists 
?


Thanks.



Re: I want Sublime 3 D auto import !

2020-06-02 Thread Виталий Фадеев via Digitalmars-d-learn

On Monday, 1 June 2020 at 18:55:03 UTC, Paul Backus wrote:

On Monday, 1 June 2020 at 16:18:44 UTC, Виталий Фадеев wrote:


I do it!

https://github.com/vitalfadeev/SublimeDlangAutoImport



I want Sublime 3 D auto import !

2020-06-01 Thread Виталий Фадеев via Digitalmars-d-learn

I want Sublime D auto import !

When typing code like this:


class Uno : IStylable
{
//
}


I want will be auto added "import IStylable" at begin of file. 
Like this:



import ui.istylable : IStylable;

class Uno : IStylable
{
//
}


1. I want for plugin will scan all files in project, and grep for 
"module ".

2. Then "module " replaced to "import ".
3. Then "import " inserted in text. At top. After line 
"module ..." if it exist, else just at top.

4. Check for "module " not exists before insert.




Re: Assign Range: layout = X, AlignRight;

2020-05-26 Thread Виталий Фадеев via Digitalmars-d-learn

On Tuesday, 26 May 2020 at 13:36:34 UTC, Виталий Фадеев wrote:

I want this:

layout = X, AlignRight;



Use case:
class Widget
{
struct Layout
{
ILayout[] _layouts;

void opAssign( args... )
{
foreach( a; args )
{
_layouts ~= a;
}
}


alias _layouts this;
}
}


in front-end code:

with( new Widget() )
{
layout = X, AlignRight;
}


I think the solution:
   void opAssign( args... ) { ... }



I want this feature in D!


Main goal is: Readable, clean, beauty code.



Assign Range: layout = X, AlignRight;

2020-05-26 Thread Виталий Фадеев via Digitalmars-d-learn

I want this:

layout = X, AlignRight;



Use case:
class Widget
{
struct Layout
{
ILayout[] _layouts;

void opAssign( args... )
{
foreach( a; args )
{
_layouts ~= a;
}
}


alias _layouts this;
}
}


in front-end code:

with( new Widget() )
{
layout = X, AlignRight;
}


I think the solution:
   void opAssign( args... ) { ... }



I want this feature in D!



Re: dub libs from home directory on windows

2020-03-18 Thread Виталий Фадеев via Digitalmars-d-learn

On Wednesday, 18 March 2020 at 13:52:20 UTC, Abby wrote:


I cannot build my app, so I was wondering if there is some 
clever way to solve this without hardcoded path to my profile 
name.


Thank you very much for your help.


I see, you want without hardcoded path...



Re: dub libs from home directory on windows

2020-03-18 Thread Виталий Фадеев via Digitalmars-d-learn

On Wednesday, 18 March 2020 at 13:52:20 UTC, Abby wrote:

Hi there,
I'm using d2sqlite3 which has dependency on sqlite3.lib. When 
I'm building my app on windows I have a dub.sdl which has a line


libs 
"%USERPROFILE%/AppData/Local/dub/packages/d2sqlite3-0.18.3/d2sqlite3/lib/win64/sqlite3" platform="windows-x86_64-dmd"


but unless I specify full path using specific user profile name 
on windows like so


"c:/user/abby/AppData/Local/dub/packages/d2sqlite3-0.18.3/d2sqlite3/lib/win64/sqlite3"

I cannot build my app, so I was wondering if there is some 
clever way to solve this without hardcoded path to my profile 
name.


Thank you very much for your help.


I see it in CairoD.
dub.json

{
...

"configurations": [
{
"name": "unittest",
"targetType": "executable",
"mainSourceFile": "unittest.d",
"versions": ["CairoPNG"],
"targetPath": "bin",
"libs-posix": ["cairo"],
"libs-windows-x86-dmd": ["lib/32/mars/cairo"],
"libs-windows-x86-gdc": ["lib/32/msvc_mingw/cairo"],
"libs-windows-x86-ldc": ["lib/32/msvc_mingw/cairo"],
"libs-windows-x86_64": ["lib/64/cairo"],
"copyFiles-windows-x86": ["lib/32/*.dll"],
"copyFiles-windows-x86_64": ["lib/64/*.dll"]
}
]
}


folders:
./
  lib/
 32/
   mars/
 cairo.lib
   msvc_mingw/
 cairo.lib
 64/
   cairo.lib




Re: dub libs from home directory on windows

2020-03-18 Thread Виталий Фадеев via Digitalmars-d-learn

On Wednesday, 18 March 2020 at 13:52:20 UTC, Abby wrote:

Hi there,
I'm using d2sqlite3 which has dependency on sqlite3.lib. When 
I'm building my app on windows I have a dub.sdl which has a line


libs 
"%USERPROFILE%/AppData/Local/dub/packages/d2sqlite3-0.18.3/d2sqlite3/lib/win64/sqlite3" platform="windows-x86_64-dmd"


but unless I specify full path using specific user profile name 
on windows like so


"c:/user/abby/AppData/Local/dub/packages/d2sqlite3-0.18.3/d2sqlite3/lib/win64/sqlite3"

I cannot build my app, so I was wondering if there is some 
clever way to solve this without hardcoded path to my profile 
name.


Thank you very much for your help.


Of course, you will use "sqlite3.lib" instead "cairo.lib".



Re: Best way to learn 2d games with D?

2020-03-18 Thread Виталий Фадеев via Digitalmars-d-learn
On Sunday, 15 March 2020 at 17:58:58 UTC, Steven Schveighoffer 
wrote:
I want to try and learn how to write 2d games. I'd prefer to do 
it with D.


I've found a ton of tutorials on learning 2d gaming with other 
languages. Is there a place to look that uses D for learning? 
Should I just start with another language and then migrate to D 
later? Anyone recommend any specific tutorial/book?


-Steve


Writing own framework - is the best !



Re: b.content.x - get property of ...structure|class|...

2020-03-17 Thread Виталий Фадеев via Digitalmars-d-learn

On Tuesday, 17 March 2020 at 09:31:21 UTC, Виталий Фадеев wrote:

Main goal is: get content "xywh" from given rect and padding.
Of course content.x is dynamic.

//
import core.sys.windows.windows;
import std.stdio;


class Base
{
RECT rect= { 0, 0, 500, 400 };
RECT padding = { 10, 10, 10, 10 };

// ...content...
??? content
{
@property int x() const { return rect.left + 
padding.left; }

}
}

int main()
{
auto b = new Base();
writeln( b.content.x );
}



How to implement "b.content.x" ( sequence call "content" of the 
"b" and then "x" of that ) ?

How to implement beauty ?



Now working with this variant:

struct Content
{
Base This;

int  left(){ return This.rect.left   + This.padding.left; 
  }
int  right()   { return This.rect.right  - 
This.padding.right;  }
int  top() { return This.rect.top+ This.padding.top;  
  }
int  bottom()  { return This.rect.bottom - 
This.padding.bottom; }


int  x()   { return left; }
int  y()   { return top;  }
int  w()   { return right - left; }
int  h()   { return bottom - top; }

RECT rect(){ return This.rect; }
}


class Base
{
RECT rect;
RECT padding;

Content content()
{
return Content( this );
}
}





b.content.x - get property of ...structure|class|...

2020-03-17 Thread Виталий Фадеев via Digitalmars-d-learn

Main goal is: get content "xywh" from given rect and padding.
Of course content.x is dynamic.

//
import core.sys.windows.windows;
import std.stdio;


class Base
{
RECT rect= { 0, 0, 500, 400 };
RECT padding = { 10, 10, 10, 10 };

// ...content...
??? content
{
@property int x() const { return rect.left + 
padding.left; }

}
}

int main()
{
auto b = new Base();
writeln( b.content.x );
}



How to implement "b.content.x" ( sequence call "content" of the 
"b" and then "x" of that ) ?

How to implement beauty ?




Re: Nice readable code with initializer

2020-03-06 Thread Виталий Фадеев via Digitalmars-d-learn

On Thursday, 5 March 2020 at 07:01:27 UTC, Виталий Фадеев wrote:

On Thursday, 5 March 2020 at 06:48:53 UTC, Виталий Фадеев wrote:

Searching for beauty code implementation.

Goal is: Create new object with "custom initializer".

"custom initializer" - is function, executed in _ctor, in 
object scope.


Main Goal is: "clean readable beauty code".

Like this and better:


class DataGrid : Base
{
this()
{
super();

CR!VBoxLayout({
sizeWidthMode  = SIZE_MODE.PARENT;
sizeHeightMode = SIZE_MODE.PARENT;

CR!GridLayout({
id = "grid";
sizeWidthMode  = SIZE_MODE.PARENT;
sizeHeightMode = SIZE_MODE.FIXED;
h  = 400;
});

CR!HCenterLayout({
CR!(Pager!TextButton) ({
   id = "pager";
});
});
});
}
}


class Base
{
T CR( T, F )( F func )
{
auto c = new T();
c.Apply( func );
return c;
}

void Apply( F )( F func )
{
func();
}
}

In code above created tree and configured nodes.

Trouble in code above is: "custom initializer" - executed in 
DataGrid context.


How to execute "custom initializer" in VBoxLayout context and 
keep readable beauty ?


Conceptual question.

How to implement next pseudo-code in D ?

VBoxLayout
sizeWidthMode  = SIZE_MODE.PARENT
sizeHeightMode = SIZE_MODE.PARENT

GridLayout
id = "grid"
sizeWidthMode  = SIZE_MODE.PARENT
sizeHeightMode = SIZE_MODE.FIXED
h  = 400

HCenterLayout
Pager!TextButton
   id = "pager"


Thank!
Now using like this:

with ( CR!VBoxLayout )
{
sizeWidthMode  = SIZE_MODE.PARENT;
sizeHeightMode = SIZE_MODE.PARENT;

with ( CR!GridLayout )
{
id = "grid";
sizeWidthMode  = SIZE_MODE.PARENT;
sizeHeightMode = SIZE_MODE.FIXED;
h  = 400;
}

with ( CR!HCenterLayout )
{
with ( CR!( Pager!TextButton ) )
{
   id = "pager";
}
}
}




Re: Safe cast

2020-03-06 Thread Виталий Фадеев via Digitalmars-d-learn

On Friday, 6 March 2020 at 12:35:29 UTC, Виталий Фадеев wrote:
Searching info for object casting with checking class type at 
runtime.


Like this:

class A
{
//
}

class B
{
int bVar;
}


unittest
{
A a = new A();

A x = cast( A )a; // ok
A x = cast( B )a; // ok, but unsafe
A x = safeCast( B )a; // throw exception
A x = safeCast( A )a; // ok
}


Searching some like next:

T safeCast( CLS )( CLS o )
{
  ... // checking type of o
  ... // may be check ClassInfo...
  return T;
}

Has function like a safeCast() ?
Or other solution ? ...



I using now next code:

import std.stdio;

class Base {}

class A : Base {}
class B : Base {}


T safeCast( T, CLS )( CLS o )
{
if ( typeid( o ) == typeid( T ) )
return cast(T)o;
else
throw new Exception( "casting error" );
}


void main()
{   
Base a = new A();

A x1 = cast( A )a;  // ok
B x2 = cast( B )a;  // ok, but unsafe
B x3 = safeCast!B( a ); // throw exception
A x4 = safeCast!A( a ); // ok
}




  1   2   >