Re: Get Dll functions at compile time

2017-08-08 Thread Johnson Jones via Digitalmars-d-learn

On Wednesday, 9 August 2017 at 02:11:13 UTC, Johnson Jones wrote:
I like to create code that automates much of the manual labor 
that we, as programmers, are generally forced to do. D 
generally makes much of this work automatable. For example, I 
have created the following code which makes loading dlls 
similar to libs:




/* Import DLL functions in to type T. The following example 
shows methodology

struct DLLImports
{
@("DLLImport") public static extern(Windows)
{
@("libgdk-3-0.dll")
{
			void* function(GdkWindow *window) 
gdk_win32_window_get_handle;

}
}
}

// Fixes static functions and function pointers to point to 
their specified DLL's

DllImport!DLLImports;
*/
void DLLImport(alias T)()
{
version(Windows)
{
		import core.sys.windows.windows, std.conv, std.meta, 
std.traits;

HINSTANCE[string] DLLs;

foreach(fname; __traits(allMembers, T))
{   
mixin("enum isf = isFunction!(T."~fname~");");
mixin("enum isfp = isFunctionPointer!(T."~fname~");");
			mixin("enum attrs = __traits(getAttributes, 
T."~fname~");");		


			static if ((isf || isfp) && attrs.length == 2 && attrs[0] == 
"DLLImport")

{
auto dllName = attrs[1];
if (dllName !in DLLs)
DLLs[dllName] = 
LoadLibrary(to!wstring(dllName~"\0").ptr);

auto dll = DLLs[dllName];
if (dll == null)
assert(0, "Cannot load DLL 
`"~dllName~"'");

auto func = GetProcAddress(dll, fname);

if (isf)
	mixin("auto p = cast(void**)&"~T.stringof~"."~fname~"; *p 
= cast(typeof(p))func;");

else
	mixin(""~T.stringof~"."~fname~" = 
cast(typeof("~T.stringof~"."~fname~"))func;");	

}

}
}



But this got me thinking that we don't even need to have to 
specify the function in D, hell, they already exist in the lib 
and we are just duplicating work.


What if, at compile time, D could get all the functions and 
their type information and build a class for them for us? We 
could then just write something like


struct DLLImports
{
@("DLLImport") string libgdk = "libgdk-3-0.dll";
}


and have some ctfe meta functions extract all the function from 
libgdk and insert them in to the struct.


There are two problems with this, one easy and one 
hard/impossible(which would be easy if people were intelligent 
enough to have foresight):



1. Get the dll function by name from the dll at compile time. 
This would probably require manually reading the dll file and 
scanning for the function.


2. Get the type information to build a declaration. This is 
probably impossible since dll's do not contain the type 
information about their parameters and return type(or do 
they?). If they did, it would be easy. I would suggest that all 
dll's generated by D include this information somewhere and an 
easy way to extract it for future programmers so such things 
could be implemented.


Alternatively, maybe a master database could be queried for 
such information by using the function names and dll name? I 
don't know if D has network capabilities at compile time though.


Alternatively, completely scrap the lethargic way things are 
done in the name of backwards compatibility and $$$ and do 
things right(learn from the past, stop repeating same mistakes, 
etc). Sure it's a lot of work, but in the end is far less than 
one thinks considering the increased productivity... but I 
guess the we gotta keep buying the kids christmas presents.




And while we are at it, here is a set of meta functions that make 
using glade files easier:


// Loads the glade interface element id's and types directly from 
the glade interface file so we do not have to declare them 
manually
// Use: @("Glade") { 
mixin(DeclareGladeInterfaceElements!gladeFile); } where ever 
variables will be defined and InstantiateGladeInterfaceElements( 
to initalize them

public string DeclareGladeInterfaceElements(string filename)()
{
auto token1 = "		// Matched class/object type, get name actual type then get id 
if it has one.
		if (i+token1.length < data.length && data[i..i+token1.length] 
== token1)

{
i += token1.length;
auto pos = i;
while(i < data.length && data[i] != '"') i++;
auto type = cast(string)data[pos..i];

// Match id if it has one.
			while(i+token2.length < data.length && data[i] != '>' && 
data[i] != '<' && data[i] != '"' && data[i..i+token2.length] != 
token2) i++;

Get Dll functions at compile time

2017-08-08 Thread Johnson Jones via Digitalmars-d-learn
I like to create code that automates much of the manual labor 
that we, as programmers, are generally forced to do. D generally 
makes much of this work automatable. For example, I have created 
the following code which makes loading dlls similar to libs:




/* Import DLL functions in to type T. The following example shows 
methodology

struct DLLImports
{
@("DLLImport") public static extern(Windows)
{
@("libgdk-3-0.dll")
{
void* function(GdkWindow *window) 
gdk_win32_window_get_handle;
}
}
}

// Fixes static functions and function pointers to point to their 
specified DLL's

DllImport!DLLImports;
*/
void DLLImport(alias T)()
{
version(Windows)
{
import core.sys.windows.windows, std.conv, std.meta, std.traits;
HINSTANCE[string] DLLs;

foreach(fname; __traits(allMembers, T))
{   
mixin("enum isf = isFunction!(T."~fname~");");
mixin("enum isfp = isFunctionPointer!(T."~fname~");");
mixin("enum attrs = __traits(getAttributes, 
T."~fname~");");

			static if ((isf || isfp) && attrs.length == 2 && attrs[0] == 
"DLLImport")

{
auto dllName = attrs[1];
if (dllName !in DLLs)
DLLs[dllName] = 
LoadLibrary(to!wstring(dllName~"\0").ptr);

auto dll = DLLs[dllName];
if (dll == null)
assert(0, "Cannot load DLL 
`"~dllName~"'");

auto func = GetProcAddress(dll, fname);

if (isf)
	mixin("auto p = cast(void**)&"~T.stringof~"."~fname~"; *p = 
cast(typeof(p))func;");

else
	mixin(""~T.stringof~"."~fname~" = 
cast(typeof("~T.stringof~"."~fname~"))func;");	

}

}
}



But this got me thinking that we don't even need to have to 
specify the function in D, hell, they already exist in the lib 
and we are just duplicating work.


What if, at compile time, D could get all the functions and their 
type information and build a class for them for us? We could then 
just write something like


struct DLLImports
{
@("DLLImport") string libgdk = "libgdk-3-0.dll";
}


and have some ctfe meta functions extract all the function from 
libgdk and insert them in to the struct.


There are two problems with this, one easy and one 
hard/impossible(which would be easy if people were intelligent 
enough to have foresight):



1. Get the dll function by name from the dll at compile time. 
This would probably require manually reading the dll file and 
scanning for the function.


2. Get the type information to build a declaration. This is 
probably impossible since dll's do not contain the type 
information about their parameters and return type(or do they?). 
If they did, it would be easy. I would suggest that all dll's 
generated by D include this information somewhere and an easy way 
to extract it for future programmers so such things could be 
implemented.


Alternatively, maybe a master database could be queried for such 
information by using the function names and dll name? I don't 
know if D has network capabilities at compile time though.


Alternatively, completely scrap the lethargic way things are done 
in the name of backwards compatibility and $$$ and do things 
right(learn from the past, stop repeating same mistakes, etc). 
Sure it's a lot of work, but in the end is far less than one 
thinks considering the increased productivity... but I guess the 
we gotta keep buying the kids christmas presents.






Why does stringof not like functions with arguments?

2017-08-08 Thread Jason Brady via Digitalmars-d-learn

Why does the following code error out with:

app.d(12,10): Error: function app.FunctionWithArguments (uint i) 
is not callable using argument types ()


Code:

import std.stdio;

void FunctionWithoutArguments() {
}

void FunctionWithArguments(uint i) {
}

void main()
{
writeln(FunctionWithoutArguments.stringof);
writeln(FunctionWithArguments.stringof);
}


Re: gtkD window centering message up and no app on taskbar

2017-08-08 Thread Johnson Jones via Digitalmars-d-learn

On Tuesday, 8 August 2017 at 21:37:40 UTC, Mike Wey wrote:

On 07-08-17 23:52, Johnson Jones wrote:

On Monday, 7 August 2017 at 20:57:08 UTC, Mike Wey wrote:

On 07-08-17 22:46, Johnson Jones wrote:

[...]


This appears to be a GTK issue, a work around might be to get 
the Window handle from gtk and use the Windows API to set the 
taskbar visibility.




Yeah, I was thinking about that but haven't yet figured out 
how to find the window handle ;/


To get the Window handle you could use this for 32bit Windows:

```
import gtk.c.functions;

// Danger, Will Robinson!

HANDLE handle = 
*(cast(HANDLE*)((cast(void*)gtk_widget_get_window(w.getWidgetStruct()))+4));



```
Where w is your application window, i used this in the map 
event signal so the handle is actually set. To complicate 
things there is a race condition in gdk some ware so at random 
the handle isn't valid.



I haven't been able to set the taskbar icon with is tough.

The two attempts:

-Setting the parent window to null as windows with no parent 
should have an taskbar icon:


```
ShowWindow(handle, SW_HIDE);
SetParent(handle, null);
ShowWindow(handle, SW_SHOW);
```

Or set the extended window style to WS_EX_APPWINDOW as that 
should fore an taskbar icon according to msdn.


```
ShowWindow(handle, SW_HIDE);
SetWindowLong(handle, GWL_EXSTYLE, WS_EX_APPWINDOW);
ShowWindow(handle, SW_SHOW);
```



Yeah, they keep returning null or some integer < 100(probably 
junk values).


There surely has to be a way to get the windows handle? We could 
use Win32 api and "search" for the window and get the handle but 
that isn't that safe unless we can specify the windows classname 
or title uniquely.


Things like ispy can find windows by coordinates but I think they 
essentially use the above methods and region compares. e.g., 
there is no FindWindowByCoordinates... which still wouldn't be 
very accurate.




// Fixup missing taskbar icon
void SetTaskBarIcon(gtk.ApplicationWindow window)
{

version(Windows)
version(X86)
{
import core.sys.windows.winuser, gdk.Window;
auto x = window.getWidgetStruct();
			auto y = 
((cast(void*)gtk.gtk_widget_get_window(window.getWidgetStruct()))+4);
			auto handle = 
*(cast(core.sys.windows.winuser.HANDLE*)((cast(void*)gtk.gtk_widget_get_window(window.getWidgetStruct()))+4));

ShowWindow(handle, SW_HIDE);
SetParent(handle, null);
SetWindowLong(handle, GWL_EXSTYLE, WS_EX_APPWINDOW);
ShowWindow(handle, SW_SHOW);
}
return;
}

Sometimes I get y = 4, which I think is what you were saying 
before. Sometimes I get handle = < 100.


I've tried in a few places but not after Main.run(), which maybe 
is needed for gtk to register the window and all that? I guess I 
can run that function in a callback and see if it is valid... 
Didn't work.


I can't really find anything on line about getting a windows 
handle from gtk ;/



There is this: 
https://stackoverflow.com/questions/23021327/how-i-can-get-drawingarea-window-handle-in-gtk3


Gdk.threads_enter()
#get the gdk window and the corresponding c gpointer
drawingareawnd = drawingarea.get_property("window")
#make sure to call ensure_native before e.g. on realize
if not drawingareawnd.has_native():
print("Your window is gonna freeze as soon as you 
move or resize it...")
ctypes.pythonapi.PyCapsule_GetPointer.restype = 
ctypes.c_void_p
ctypes.pythonapi.PyCapsule_GetPointer.argtypes = 
[ctypes.py_object]
drawingarea_gpointer = 
ctypes.pythonapi.PyCapsule_GetPointer(drawingareawnd.__gpointer__, None)

#get the win32 handle
gdkdll = ctypes.CDLL ("libgdk-3-0.dll")
hnd = 
gdkdll.gdk_win32_window_get_handle(drawingarea_gpointer)
#do what you want with it ... I pass it to a gstreamer 
videosink

Gdk.threads_leave()

which suggests that maybe gdk_win32_window_get_handle can be used


Ok, I was able to get it to work using that code. I had to do 
some wonky dll imports and stuff but that function seems to be 
the correct one. I could not use it until after the window was 
created(I used it in a button handler), else it crashed the app 
for some reason.




import core.sys.windows.winuser, gdk.Window;
			auto handle = 
cast(core.sys.windows.winuser.HANDLE)gdk_win32_window_get_handle(gtk.gtk_widget_get_window(window.getWidgetStruct()));

ShowWindow(handle, SW_HIDE);
SetParent(handle, null);
SetWindowLong(handle, GWL_EXSTYLE, WS_EX_APPWINDOW);
ShowWindow(handle, SW_SHOW);

Seems to work.

So you might want to add gdk_win32_window_get_handle as an export 
in gtkD so it can be used easier.



Th

Re: gtkD window centering message up and no app on taskbar

2017-08-08 Thread Mike Wey via Digitalmars-d-learn

On 07-08-17 23:52, Johnson Jones wrote:

On Monday, 7 August 2017 at 20:57:08 UTC, Mike Wey wrote:

On 07-08-17 22:46, Johnson Jones wrote:

[...]


This appears to be a GTK issue, a work around might be to get the 
Window handle from gtk and use the Windows API to set the taskbar 
visibility.




Yeah, I was thinking about that but haven't yet figured out how to find 
the window handle ;/


To get the Window handle you could use this for 32bit Windows:

```
import gtk.c.functions;

// Danger, Will Robinson!

HANDLE handle = 
*(cast(HANDLE*)((cast(void*)gtk_widget_get_window(w.getWidgetStruct()))+4));



```
Where w is your application window, i used this in the map event signal 
so the handle is actually set. To complicate things there is a race 
condition in gdk some ware so at random the handle isn't valid.



I haven't been able to set the taskbar icon with is tough.

The two attempts:

-Setting the parent window to null as windows with no parent should have 
an taskbar icon:


```
ShowWindow(handle, SW_HIDE);
SetParent(handle, null);
ShowWindow(handle, SW_SHOW);
```

Or set the extended window style to WS_EX_APPWINDOW as that should fore 
an taskbar icon according to msdn.


```
ShowWindow(handle, SW_HIDE);
SetWindowLong(handle, GWL_EXSTYLE, WS_EX_APPWINDOW);
ShowWindow(handle, SW_SHOW);
```

--
Mike Wey


Re: Create class on stack

2017-08-08 Thread Johan Engelen via Digitalmars-d-learn

On Monday, 7 August 2017 at 13:40:18 UTC, Moritz Maxeiner wrote:


Thanks, I wasn't aware of this. I tried fooling around scope 
classes and DIP1000 for a bit and was surprised that this is 
allowed:


Thanks for the test case :-)
It was fun to see that ASan can catch this bug too. Because 
writing the blog post about ASan will take quite some time still, 
I've pasted the demonstration below (there is a big big big 
caveat that will need more work from LDC's side, but you'll have 
to wait until the blog article).


Simplified your code for the demonstration:
```
class A {
int i;
}

void inc(A a) @safe {
a.i += 1; // Line 6
}

auto makeA() @safe {  // Line 9
import std.algorithm : move;
scope a = new A();
return move(a);
}

void main() @safe {
auto a = makeA();
a.inc(); // Line 17
}
```

```
ldc2 -fsanitize=address -disable-fp-elim scopeclass.d -g -O1 
-dip1000
ASAN_OPTIONS=detect_stack_use_after_return=1 ./scopeclass 2>&1 
| ddemangle


=
==11446==ERROR: AddressSanitizer: stack-use-after-return on 
address 0x000104929050 at pc 0x0001007a9837 bp 0x7fff5f457510 sp 
0x7fff5f457508

READ of size 4 at 0x000104929050 thread T0
#0 0x1007a9836 in @safe void scopeclass.inc(scopeclass.A) 
scopeclass.d:6

#1 0x1007a9a20 in _Dmain scopeclass.d:17
#2 0x1008e40ce in 
_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv 
(scopeclass:x86_64+0x10013c0ce)

#3 0x7fff9729b5ac in start (libdyld.dylib:x86_64+0x35ac)

Address 0x000104929050 is located in stack of thread T0 at offset 
80 in frame
#0 0x1007a984f in pure nothrow @nogc @safe scopeclass.A 
scopeclass.makeA() scopeclass.d:9

```



readText with added null-terminator that enables sentinel-based search

2017-08-08 Thread Nordlöw via Digitalmars-d-learn
Has anybody written a wrapper around `std.file.readText` (or 
similar) that appends a final zero-byte terminator in order to 
realize sentinel-based search in textual parsers.


Thread sequencer

2017-08-08 Thread Johnson Jones via Digitalmars-d-learn
I'm wondering if there is an easy way to create a single extra 
thread that one can pass delegates(code) to and it executes it 
properly. The thread never closes except at shutdown.


The idea is that isn't of creating one thread per task, there is 
one thread that executes each task.


Obviously the idea is not hard but I'd like to wrap those tasks 
up in lamdas so I can instantiate them at the point I need 
them(and possibly even reuse them). I don't wanna stick all the 
code in one thread with a giant switch nor create multiple 
threads.


The main problem is that each delegate might have different 
arguments and return values associate with it and, while 
templates probably can handle it, it seems like it could be a 
mess.


Any ideas how to accomplish this effectively? The main thread 
will effectively be a task scheduler that will will be able to 
execute tasks sequentially and in parallel and interrupt with 
interval execution(probably using fibers to do so).



An easy way to think about this is for animations. We might want 
to animate several things at the same time at different rates. So 
each task will do the frame of the animation then yield. The 
scheduler does it's job and sequences everything so it all comes 
out correctly(each animation frame is rendered as soon as it is 
suppose to. If it's a 30fps second animation then the scheduler 
tries to call the task 30 times a second. If it's 1fps then it 
does so once a second... interleaving everything etc.



e.g.,

int N = 100;
scheduler.AddTask((int N) { for(int i = 0; i < N; i++) { 
displayImage("Animation", i); yeild; } }, 100, N); // Display the 
ith frame of animation at 10fps.


Or whatever.

Essentially the scheduler should do nothing until a task is 
added, schedule them as it should, etc.


Now, the hard thing, I think is that the delegates should capture 
the local context, but they are ran on a different thread so any 
local values are passed along... which requires templates since 
they might be different for each delegate: e.g.



Event e = widgetEvent;
scheduler.AddTask((Event e) { for(int i = 0; i < e.Count; i++) { 
AddEventCycles(e.Cycle[i]); yield; } }, 10, e);



and I think that is probably the hard part since we need to store 
the delegates in a heterogeneous container of delegates.


Anyways, Would be nice if something like this already existed or 
would be easy to implement.






Re: Express "Class argument may not be null" ?

2017-08-08 Thread Adam D. Ruppe via Digitalmars-d-learn
I was about to say "use NotNull" but there still isn't one in 
std.typecons. ugh.


But it is just a wrapper type that checks null in the contracts 
too, so you can do it at the function itself.


Re: Specify dmd or ldc compiler and version in a json dub file?

2017-08-08 Thread Joakim via Digitalmars-d-learn

On Tuesday, 8 August 2017 at 10:07:54 UTC, data pulverizer wrote:
On Tuesday, 8 August 2017 at 09:51:40 UTC, Moritz Maxeiner 
wrote:
If your code depends on capabilities of a specific D compiler, 
I wouldn't depend on build tools for that, I'd make it clear 
in the source code via conditional compilation [1]:


---
version (DigitalMars)
{
}
else version (LDC)
{
}
else
{
static assert (0, "Unsupported D compiler");
}
---

There's no equivalent for the frontend version, though AFAIK.

If it's not your code that needs something compiler specific, 
but you just want to control which is used, don't use dub as a 
build tool, use another (cmake, meson, write your own 
compilation "script" in D), and set it's invocation as a 
prebuildcommand in the dub package file (make sure dub's 
source file list is empty).


[1] http://dlang.org/spec/version.html#predefined-versions


Many thanks!


I'll note that you can specify that dub use a particular D 
compiler for building all your own D packages, by creating a file 
called .dub/settings.json in your home directory, and putting the 
full path to the compiler you want in there, like so:


  {
"defaultCompiler": "/home/datapulverizer/ldc-1.3/bin/ldc2"
  }

Ldc uses a file like that to make sure the dub binary it comes 
with uses ldc, not dmd, so you can look at that file for an 
example.


However, it sounds like you want to ensure that all users of a 
package are forced to use a particular D compiler, while this 
will just make sure that your dub binary will always use the D 
compiler you want.


Re: Express "Class argument may not be null" ?

2017-08-08 Thread Andre Kostur via Digitalmars-d-learn

On 2017-08-08 12:38 PM, Steven Schveighoffer wrote:

On 8/8/17 2:56 PM, ag0aep6g wrote:

On 08/08/2017 08:34 PM, Johan Engelen wrote:
   How would you express the function interface intent that a 
reference to a class may not be null?
For a function "void foo(Klass)", calling "foo(null)" is valid. How 
do I express that that is invalid? (let's leave erroring with a 
compile error aside for now)


Something equivalent to C++'s pass by reference: "void foo(Klass&)".

[snip]


But you can pass null in a ref parameter:


void f(ref int x) @safe {}
void main() @safe
{
 int* p = null;
 f(*p);
}



Note that C++ also can do this, so I'm not sure the & is accomplishing 
the correct goal:


void foo(Klass&);

int main()
{
Klass *k = NULL;
foo(*k);
}

However, the in contract does actually enforce the requirement.


To be fair: it cannot be done in C++ without first invoking Undefined 
Behaviour (such as dereferencing a nullptr).


Re: Express "Class argument may not be null" ?

2017-08-08 Thread Johan Engelen via Digitalmars-d-learn
On Tuesday, 8 August 2017 at 19:38:19 UTC, Steven Schveighoffer 
wrote:


Note that C++ also can do this, so I'm not sure the & is 
accomplishing the correct goal:


void foo(Klass&);

int main()
{
   Klass *k = NULL;
   foo(*k);
}


In C++, it is clear that the _caller_ is doing the dereferencing, 
and the dereference is also explicit.



However, the in contract does actually enforce the requirement.


And adds null pointer checks even when clearly not needed.

- Johan





Re: Express "Class argument may not be null" ?

2017-08-08 Thread Johan Engelen via Digitalmars-d-learn
On Tuesday, 8 August 2017 at 18:57:48 UTC, Steven Schveighoffer 
wrote:

On 8/8/17 2:34 PM, Johan Engelen wrote:

Hi all,
   How would you express the function interface intent that a 
reference to a class may not be null?
For a function "void foo(Klass)", calling "foo(null)" is 
valid. How do I express that that is invalid? (let's leave 
erroring with a compile error aside for now)


There isn't a way to do this in the type itself.

One can always create a null class instance via:

MyObj obj;

There is no way to disallow this somehow in the definition of 
MyObj. With structs, you can @disable this(), and it's still 
possible but harder to do so.


Ok thanks, so this could be a reason for not being allowed to 
express the non-null-ness.
(I still haven't found peace with the absence of an explicit * 
for classes)


I would say, however, that if you wanted to express the 
*intent*, even without a compile-time error, you could use a 
contract:


void foo(Klass k) in {assert(k !is null);};


Thanks. I regret leaving compile-time errors out, because in that 
case adding it to the function documentation would suffice.


(Btw: "Error: function foo in and out contracts require function 
body". But who uses .di files anyway. ;-)


Cheers,
  Johan



Re: Efficiently streaming data to associative array

2017-08-08 Thread Anonymouse via Digitalmars-d-learn
On Tuesday, 8 August 2017 at 16:00:17 UTC, Steven Schveighoffer 
wrote:
I wouldn't use formattedRead, as I think this is going to 
allocate temporaries for a and b.


What would you suggest to use in its stead? My use-case is 
similar to the OP's in that I have a string of tokens that I want 
split into variables.


import std.stdio;
import std.format;

void main()
{
string abc, def;
int ghi, jkl;

string s = "abc,123,def,456";
s.formattedRead!"%s,%d,%s,%d"(abc, ghi, def, jkl);

writeln(abc);
writeln(def);
writeln(ghi);
writeln(jkl);
}


Re: Express "Class argument may not be null" ?

2017-08-08 Thread Steven Schveighoffer via Digitalmars-d-learn

On 8/8/17 2:56 PM, ag0aep6g wrote:

On 08/08/2017 08:34 PM, Johan Engelen wrote:
   How would you express the function interface intent that a 
reference to a class may not be null?
For a function "void foo(Klass)", calling "foo(null)" is valid. How do 
I express that that is invalid? (let's leave erroring with a compile 
error aside for now)


Something equivalent to C++'s pass by reference: "void foo(Klass&)".

[snip]


But you can pass null in a ref parameter:


void f(ref int x) @safe {}
void main() @safe
{
 int* p = null;
 f(*p);
}



Note that C++ also can do this, so I'm not sure the & is accomplishing 
the correct goal:


void foo(Klass&);

int main()
{
   Klass *k = NULL;
   foo(*k);
}

However, the in contract does actually enforce the requirement.

-Steve


Re: Express "Class argument may not be null" ?

2017-08-08 Thread ag0aep6g via Digitalmars-d-learn

On 08/08/2017 08:34 PM, Johan Engelen wrote:
   How would you express the function interface intent that a reference 
to a class may not be null?
For a function "void foo(Klass)", calling "foo(null)" is valid. How do I 
express that that is invalid? (let's leave erroring with a compile error 
aside for now)


Something equivalent to C++'s pass by reference: "void foo(Klass&)".


A contract might be the best you can do:


void foo(Klass k)
in { assert(k !is null); }
body {}


Or throw an exception.


(note: I mean D classes, for structs "ref" works)


But you can pass null in a ref parameter:


void f(ref int x) @safe {}
void main() @safe
{
int* p = null;
f(*p);
}



Re: Express "Class argument may not be null" ?

2017-08-08 Thread Steven Schveighoffer via Digitalmars-d-learn

On 8/8/17 2:34 PM, Johan Engelen wrote:

Hi all,
   How would you express the function interface intent that a reference 
to a class may not be null?
For a function "void foo(Klass)", calling "foo(null)" is valid. How do I 
express that that is invalid? (let's leave erroring with a compile error 
aside for now)


There isn't a way to do this in the type itself.

One can always create a null class instance via:

MyObj obj;

There is no way to disallow this somehow in the definition of MyObj. 
With structs, you can @disable this(), and it's still possible but 
harder to do so.


I would say, however, that if you wanted to express the *intent*, even 
without a compile-time error, you could use a contract:


void foo(Klass k) in {assert(k !is null);};

Since the contract is part of the signature, this should be symantically 
what you want.


However, this has to be done on every function that would accept a 
Klass, there's no way to bake it into the type itself.


-Steve


Express "Class argument may not be null" ?

2017-08-08 Thread Johan Engelen via Digitalmars-d-learn

Hi all,
  How would you express the function interface intent that a 
reference to a class may not be null?
For a function "void foo(Klass)", calling "foo(null)" is valid. 
How do I express that that is invalid? (let's leave erroring with 
a compile error aside for now)


Something equivalent to C++'s pass by reference: "void 
foo(Klass&)".


(note: I mean D classes, for structs "ref" works)

Thanks,
  Johan



Re: Efficiently streaming data to associative array

2017-08-08 Thread Guillaume Chatelet via Digitalmars-d-learn
On Tuesday, 8 August 2017 at 16:00:17 UTC, Steven Schveighoffer 
wrote:

On 8/8/17 11:28 AM, Guillaume Chatelet wrote:
Let's say I'm processing MB of data, I'm lazily iterating over 
the incoming lines storing data in an associative array. I 
don't want to copy unless I have to.


Contrived example follows:

input file
--
a,b,15
c,d,12


Efficient ingestion
---
void main() {

   size_t[string][string] indexed_map;

   foreach(char[] line ; stdin.byLine) {
 char[] a;
 char[] b;
 size_t value;
 line.formattedRead!"%s,%s,%d"(a,b,value);

 auto pA = a in indexed_map;
 if(pA is null) {
   pA = &(indexed_map[a.idup] = (size_t[string]).init);
 }

 auto pB = b in (*pA);
 if(pB is null) {
   pB = &((*pA)[b.idup] = size_t.init
 }

 // Technically unneeded but let's say we have more than 2 
dimensions.

 (*pB) = value;
   }

   indexed_map.writeln;
}


I qualify this code as ugly but fast. Any idea on how to make 
this less ugly? Is there something in Phobos to help?


I wouldn't use formattedRead, as I think this is going to 
allocate temporaries for a and b.


Note, this is very close to Jon Degenhardt's blog post in May: 
https://dlang.org/blog/2017/05/24/faster-command-line-tools-in-d/


-Steve


I haven't yet dug into formattedRead but thx for letting me know 
: )
I was mostly speaking about the pattern with the AA. I guess the 
best I can do is a templated function to hide the ugliness.



ref Value GetWithDefault(Value)(ref Value[string] map, const 
(char[]) key) {

  auto pValue = key in map;
  if(pValue) return *pValue;
  return map[key.idup] = Value.init;
}

void main() {

  size_t[string][string] indexed_map;

  foreach(char[] line ; stdin.byLine) {
char[] a;
char[] b;
size_t value;
line.formattedRead!"%s,%s,%d"(a,b,value);

indexed_map.GetWithDefault(a).GetWithDefault(b) = value;
  }

  indexed_map.writeln;
}


Not too bad actually !


Re: Efficiently streaming data to associative array

2017-08-08 Thread Steven Schveighoffer via Digitalmars-d-learn

On 8/8/17 11:28 AM, Guillaume Chatelet wrote:
Let's say I'm processing MB of data, I'm lazily iterating over the 
incoming lines storing data in an associative array. I don't want to 
copy unless I have to.


Contrived example follows:

input file
--
a,b,15
c,d,12


Efficient ingestion
---
void main() {

   size_t[string][string] indexed_map;

   foreach(char[] line ; stdin.byLine) {
 char[] a;
 char[] b;
 size_t value;
 line.formattedRead!"%s,%s,%d"(a,b,value);

 auto pA = a in indexed_map;
 if(pA is null) {
   pA = &(indexed_map[a.idup] = (size_t[string]).init);
 }

 auto pB = b in (*pA);
 if(pB is null) {
   pB = &((*pA)[b.idup] = size_t.init
 }

 // Technically unneeded but let's say we have more than 2 dimensions.
 (*pB) = value;
   }

   indexed_map.writeln;
}


I qualify this code as ugly but fast. Any idea on how to make this less 
ugly? Is there something in Phobos to help?


I wouldn't use formattedRead, as I think this is going to allocate 
temporaries for a and b.


Note, this is very close to Jon Degenhardt's blog post in May: 
https://dlang.org/blog/2017/05/24/faster-command-line-tools-in-d/


-Steve


Re: Is it's possible to make modular pug template in vibed?

2017-08-08 Thread Steven Schveighoffer via Digitalmars-d-learn

On 8/8/17 10:52 AM, Suliman wrote:

your examples generate me:




 
 DLANG.ru
 
 
 
 
 (c) DLANG 2017
 
 
 



That's the template without the block.


The only one modification that I did I changes pages names:
extends home

because my main page is home.dt


You have it backwards. You don't render the layout template, but the 
template that extends the layout.


-Steve


Efficiently streaming data to associative array

2017-08-08 Thread Guillaume Chatelet via Digitalmars-d-learn
Let's say I'm processing MB of data, I'm lazily iterating over 
the incoming lines storing data in an associative array. I don't 
want to copy unless I have to.


Contrived example follows:

input file
--
a,b,15
c,d,12
...

Efficient ingestion
---
void main() {

  size_t[string][string] indexed_map;

  foreach(char[] line ; stdin.byLine) {
char[] a;
char[] b;
size_t value;
line.formattedRead!"%s,%s,%d"(a,b,value);

auto pA = a in indexed_map;
if(pA is null) {
  pA = &(indexed_map[a.idup] = (size_t[string]).init);
}

auto pB = b in (*pA);
if(pB is null) {
  pB = &((*pA)[b.idup] = size_t.init);
}

// Technically unneeded but let's say we have more than 2 
dimensions.

(*pB) = value;
  }

  indexed_map.writeln;
}


I qualify this code as ugly but fast. Any idea on how to make 
this less ugly? Is there something in Phobos to help?


Re: Is it's possible to make modular pug template in vibed?

2017-08-08 Thread Suliman via Digitalmars-d-learn

your examples generate me:





DLANG.ru




(c) DLANG 2017





The only one modification that I did I changes pages names:
extends home

because my main page is home.dt




Re: Is it's possible to make modular pug template in vibed?

2017-08-08 Thread Suliman via Digitalmars-d-learn

Am I right understand that I can extend only one template?


Re: Is it's possible to make modular pug template in vibed?

2017-08-08 Thread Steven Schveighoffer via Digitalmars-d-learn

On 8/8/17 9:19 AM, Suliman wrote:

Still can't get it work.

include header
..MainContainer
..Header
   .HeaderMenu
 .HeaderBlock
   a(href="/") General
 .HeaderBlock
   a(href="/FAQ") FAQ
 .HeaderBlock
   a(href="/book") Book
   .HeaderLoginBlock Sign in
..Middle
   f
include footer


OK, you aren't thinking of this correctly then, each pug/diet file must 
be complete. It's not like C preprocessor where the structure can be 
played with via includes or macros.


That is, .MainContainer cannot be a child under something defined in header.

In this case, you need to use the block system.

it's template is compilable, but it have wrong indent. Original page 
(that I am trying to split) have next indents:


doctype html
html
   head
 title DLANG.ru
   body
 #app
   .MainContainer
 .Header
   .HeaderMenu
 .HeaderBlock
   router-link(to='/') General
 .HeaderBlock
   router-link(to='/FAQ') FAQ
 .HeaderBlock
   router-link(to='/book') Book
   .HeaderLoginBlock Sign in
 .Middle
   f
 .footer (c) DLANG 2017

But I can't get right indents when I am splition it.


So what you want is probably like:

layout.dt:
doctype html
html
   head
  title DLANG.ru
   body
  #app
 .MainContainer
block contents
.footer (c) DLANG 2017

myfile.dt:
extends layout
block contents
   .Header
  .HeaderMenu
 .HeaderBlock
router-link(to='/') General
 .HeaderBlock
router-link(to='/FAQ') FAQ
 .HeaderBlock
router-link(to='/book') Book
  .HeaderLoginBlock Sign in
   .Middle
  f

-Steve


Re: Is it's possible to make modular pug template in vibed?

2017-08-08 Thread Suliman via Digitalmars-d-learn
On Tuesday, 8 August 2017 at 13:22:58 UTC, Steven Schveighoffer 
wrote:

On 8/8/17 9:10 AM, Suliman wrote:

Yes, thanks what: extends layout mean?


It means that your final file will be layout.dt, but with the 
block statements replaced with the contents defined by the 
specific view file.


Think of it like an interface, where the "blocks" are function 
prototypes, and your specific view file is a class that 
implements the interface, where you implement the functions by 
defining the blocks.


-Steve


Could you show how to improve my code above? I can't get it 
work...


Re: Is it's possible to make modular pug template in vibed?

2017-08-08 Thread Steven Schveighoffer via Digitalmars-d-learn

On 8/8/17 9:10 AM, Suliman wrote:

Yes, thanks what: extends layout mean?


It means that your final file will be layout.dt, but with the block 
statements replaced with the contents defined by the specific view file.


Think of it like an interface, where the "blocks" are function 
prototypes, and your specific view file is a class that implements the 
interface, where you implement the functions by defining the blocks.


-Steve


Re: Is it's possible to make modular pug template in vibed?

2017-08-08 Thread Suliman via Digitalmars-d-learn

Still can't get it work.

include header
.MainContainer
.Header
  .HeaderMenu
.HeaderBlock
  a(href="/") General
.HeaderBlock
  a(href="/FAQ") FAQ
.HeaderBlock
  a(href="/book") Book
  .HeaderLoginBlock Sign in
.Middle
  f
include footer



it's template is compilable, but it have wrong indent. Original 
page (that I am trying to split) have next indents:


doctype html
html
  head
title DLANG.ru
  body
#app
  .MainContainer
.Header
  .HeaderMenu
.HeaderBlock
  router-link(to='/') General
.HeaderBlock
  router-link(to='/FAQ') FAQ
.HeaderBlock
  router-link(to='/book') Book
  .HeaderLoginBlock Sign in
.Middle
  f
.footer (c) DLANG 2017

But I can't get right indents when I am splition it.



Re: Is it's possible to make modular pug template in vibed?

2017-08-08 Thread Suliman via Digitalmars-d-learn

Yes, thanks what: extends layout mean?




Re: Is it's possible to make modular pug template in vibed?

2017-08-08 Thread Steven Schveighoffer via Digitalmars-d-learn

On 8/8/17 8:38 AM, Suliman wrote:

On Tuesday, 8 August 2017 at 11:59:38 UTC, Suliman wrote:

On Tuesday, 8 August 2017 at 11:55:09 UTC, Suliman wrote:
For example I am making simple site with header and footer. header 
and footer will be same for all pages. I do not want to do copy-paste 
it in every page. I want write it's once and than simpy import in 
every page. Is it's possible to do with vibed?


Oh, I founded answer in docs.


I can't fund way to `include` header and footer in the same docs. I cam 
getting error: "Includes cannot have children"


This is probably because you have something like:

include myfile
   hr // this is not allowed

The way I do it is this (learned from Kai's book):

layout.dt:

html
  head
...
  body
... // header here
block content
... // footer here

someview.dt:
extends layout
block content
  ... // your content


-Steve


Re: Is it's possible to make modular pug template in vibed?

2017-08-08 Thread Suliman via Digitalmars-d-learn

On Tuesday, 8 August 2017 at 11:59:38 UTC, Suliman wrote:

On Tuesday, 8 August 2017 at 11:55:09 UTC, Suliman wrote:
For example I am making simple site with header and footer. 
header and footer will be same for all pages. I do not want to 
do copy-paste it in every page. I want write it's once and 
than simpy import in every page. Is it's possible to do with 
vibed?


Oh, I founded answer in docs.


I can't fund way to `include` header and footer in the same docs. 
I cam getting error: "Includes cannot have children"


Re: Is it's possible to make modular pug template in vibed?

2017-08-08 Thread Suliman via Digitalmars-d-learn

On Tuesday, 8 August 2017 at 11:55:09 UTC, Suliman wrote:
For example I am making simple site with header and footer. 
header and footer will be same for all pages. I do not want to 
do copy-paste it in every page. I want write it's once and than 
simpy import in every page. Is it's possible to do with vibed?


Oh, I founded answer in docs.


Is it's possible to make modular pug template in vibed?

2017-08-08 Thread Suliman via Digitalmars-d-learn
For example I am making simple site with header and footer. 
header and footer will be same for all pages. I do not want to do 
copy-paste it in every page. I want write it's once and than 
simpy import in every page. Is it's possible to do with vibed?


Re: Specify dmd or ldc compiler and version in a json dub file?

2017-08-08 Thread data pulverizer via Digitalmars-d-learn

On Tuesday, 8 August 2017 at 09:51:40 UTC, Moritz Maxeiner wrote:
If your code depends on capabilities of a specific D compiler, 
I wouldn't depend on build tools for that, I'd make it clear in 
the source code via conditional compilation [1]:


---
version (DigitalMars)
{
}
else version (LDC)
{
}
else
{
static assert (0, "Unsupported D compiler");
}
---

There's no equivalent for the frontend version, though AFAIK.

If it's not your code that needs something compiler specific, 
but you just want to control which is used, don't use dub as a 
build tool, use another (cmake, meson, write your own 
compilation "script" in D), and set it's invocation as a 
prebuildcommand in the dub package file (make sure dub's source 
file list is empty).


[1] http://dlang.org/spec/version.html#predefined-versions


Many thanks!


Re: Specify dmd or ldc compiler and version in a json dub file?

2017-08-08 Thread Moritz Maxeiner via Digitalmars-d-learn

On Tuesday, 8 August 2017 at 09:31:49 UTC, data pulverizer wrote:
On Tuesday, 8 August 2017 at 09:21:54 UTC, Moritz Maxeiner 
wrote:
On Tuesday, 8 August 2017 at 09:17:02 UTC, data pulverizer 
wrote:

Hi,

I would like to know how to specify dmd or ldc compiler and 
version in a json dub file.


Thanks in advance.


You can't [1]. You can specify the compiler to use only on the 
dub command line via `--compiler=`.


[1] https://code.dlang.org/package-format?lang=json


How do you distribute packages with specific compiler 
dependencies? I guess I could write it in the readme.


If your code depends on capabilities of a specific D compiler, I 
wouldn't depend on build tools for that, I'd make it clear in the 
source code via conditional compilation [1]:


---
version (DigitalMars)
{
}
else version (LDC)
{
}
else
{
static assert (0, "Unsupported D compiler");
}
---

There's no equivalent for the frontend version, though AFAIK.

If it's not your code that needs something compiler specific, but 
you just want to control which is used, don't use dub as a build 
tool, use another (cmake, meson, write your own compilation 
"script" in D), and set it's invocation as a prebuildcommand in 
the dub package file (make sure dub's source file list is empty).


[1] http://dlang.org/spec/version.html#predefined-versions


Re: Specify dmd or ldc compiler and version in a json dub file?

2017-08-08 Thread data pulverizer via Digitalmars-d-learn

On Tuesday, 8 August 2017 at 09:21:54 UTC, Moritz Maxeiner wrote:
On Tuesday, 8 August 2017 at 09:17:02 UTC, data pulverizer 
wrote:

Hi,

I would like to know how to specify dmd or ldc compiler and 
version in a json dub file.


Thanks in advance.


You can't [1]. You can specify the compiler to use only on the 
dub command line via `--compiler=`.


[1] https://code.dlang.org/package-format?lang=json


How do you distribute packages with specific compiler 
dependencies? I guess I could write it in the readme.


Re: Specify dmd or ldc compiler and version in a json dub file?

2017-08-08 Thread Moritz Maxeiner via Digitalmars-d-learn

On Tuesday, 8 August 2017 at 09:17:02 UTC, data pulverizer wrote:

Hi,

I would like to know how to specify dmd or ldc compiler and 
version in a json dub file.


Thanks in advance.


You can't [1]. You can specify the compiler to use only on the 
dub command line via `--compiler=`.


[1] https://code.dlang.org/package-format?lang=json


Specify dmd or ldc compiler and version in a json dub file?

2017-08-08 Thread data pulverizer via Digitalmars-d-learn

Hi,

I would like to know how to specify dmd or ldc compiler and 
version in a json dub file.


Thanks in advance.


Re: Create class on stack

2017-08-08 Thread Moritz Maxeiner via Digitalmars-d-learn

On Tuesday, 8 August 2017 at 05:37:41 UTC, ANtlord wrote:

On Sunday, 6 August 2017 at 15:47:43 UTC, Moritz Maxeiner wrote:

If you use this option, do be aware that this feature has been

 > scheduled for future deprecation [1].
It's likely going to continue working for quite a while 
(years), though.


[1] 
https://dlang.org/deprecate.html#scope%20for%20allocating%20classes%20on%20the%20stack


I can't understand. Why is moved a scope allocation to a 
library. I'm pretty sure it should be a language feature.


The reason is given at the link under "Rationale":

---
scope was an unsafe feature. A reference to a scoped class could 
easily be returned from a function without errors, which would 
make using such an object undefined behavior due to the object 
being destroyed after exiting the scope of the function it was 
allocated in. To discourage it from general-use but still allow 
usage when needed a library solution was implemented.


Note that scope for other usages (e.g. scoped variables) is 
unrelated to this feature and will not be deprecated.

---

Do note that - as Mike pointed out - this rationale does predate 
DIP1000 escape analysis and is largely invalidated by it for 
@safe code.
Another reason to use the library type is the ability to move the 
class object around via std.algorithm.move (if you need such C++ 
style behaviour); I'm not sure whether scope classes will get 
this feature (I have argued for it at the bug report linked to in 
my response to Mike), but I wouldn't count on it.