Re: Get return type statically
On Tue, Jun 28, 2016 at 01:41:03AM +, Smoke Adams via Digitalmars-d-learn wrote: > I have a type > > public class SuperFunction(T) > { > T t; > return(T) Do() { return t(); } > } > > where T is a delegate or function. First, I would like to be able to > specify that this must be the case for SuperFunction so we can't pass > non-function/delegates for T. Try: class SuperFunction(T) if (is(T == function) || is(T == delegate)) { ... } > Second, How to specify the return type of Do to match that of T. Maybe this? import std.traits : ReturnType, Parameters; ReturnType!T Do(Parameters!T args) { return t(args); } T -- INTEL = Only half of "intelligence".
Re: Registration-free COM client
On Monday, 27 June 2016 at 21:17:52 UTC, Thalamus wrote: Hi everyone, I've succeeded in using D as a client for regular (registered) COM servers in the past, but in this case, I'm building the server as well. I would like to avoid registering it if possible so XCOPY-like deployment remains an option. Can a registration-free COM client be built in D? If so, how can the code be setup to consume the manifest file, or alternately, is there a way to just point D to the correct assembly directly in code? To load load a COM object from a given file (DLL or AX) without having it registered, just load the file with CoLoadLibrary() and use its DllGetClassObject() function to get IClassFactory which will give you any kind of object in this library. Here's how I do it (in my case the requested object has "IBaseFilter" COM interface): IBaseFilter createDSFilterFromFile(GUID guid, string dir, string fname) { auto curdir = getcwd(); scope(exit) chdir(curdir); chdir(dir); // in case the lib depends on other dlls nearby auto hinst = CoLoadLibrary( cast(wchar*) buildPath(dir, fname).toUTF16z, TRUE); enforce!COMException(hinst); auto fnGetClassObject = cast(LPFNGETCLASSOBJECT) GetProcAddress(hinst, "DllGetClassObject"); enforce!COMException(fnGetClassObject); IClassFactory factory; auto iid = IID_IClassFactory; fnGetClassObject(, , cast(void**)).checkHR("fnGetClassObject failed"); enforce!COMException(factory); IBaseFilter ibf; factory.CreateInstance(null, _IBaseFilter, cast(void**)).checkHR("factory.CreateInstance"); return ibf; }
Re: Get return type statically
On 06/27/2016 06:41 PM, Smoke Adams wrote: I have a type public class SuperFunction(T) { T t; return(T) Do() { return t(); } } where T is a delegate or function. First, I would like to be able to specify that this must be the case for SuperFunction so we can't pass non-function/delegates for T. Second, How to specify the return type of Do to match that of T. e.g., SuperFunction!(bool function()) then return(T) should be bool. The simplest thing is to define the return type as 'auto'. Similarly, I would like to extra the T's parameters and make Do have them also. This way, SuperFunction!T.Do emulates T in every way. std.traits is your friend. :) import std.traits; public class SuperFunction(alias func) if (isCallable!func) { auto Do(Parameters!func args) { return func(args); } } void main() { auto sf = new SuperFunction!((int i) => i * 2); assert(sf.Do(42) == 84); } Ali
Re: Get return type statically
I should point out also that this should be inheritable. Eventually I would like to create an algebra of SuperFunctions. e.g., SF3 = SF1 + SF2 is a new super function that combines the parameter list of SF1 and SF2 and unionizes their return type. Both functions are called by Do(which will ultimately be handled by opCall). Other operations on the parameters can be created(intersection or subtraction, multiplication, etc...). I believe, to do this, I will have to create a string mixin that formulates Do(...) properly using a CTFE.
Re: Local fixed sized arrays
On 06/27/2016 04:02 PM, Smoke Adams wrote: > On Monday, 27 June 2016 at 22:56:35 UTC, Ali Çehreli wrote: >> On 06/27/2016 02:58 PM, Smoke Adams wrote: >>> I'm in need of a way to create a local array that isn't GC'ed. It must >>> be dynamic in the sense of setting the size at compile time but it will >>> be used only in scope and only on structs. >>> >>> function x(int y) >>> { >>> bool[y] arr; >>> >>> arr ~= 3; >>> >>> } >>> >>> I care about slicing or anything but appending, removal, and indexing. I >>> don't even need bounds checking. I don't see a need to get locked in to >>> the GC for such simple cases. >>> >>> >> >> One way is to make x() a function template: >> >> import std.stdio; >> >> void x(int y)() { >>bool[y] arr; >>arr[y/2] = true; >>writeln(arr); >> } >> >> void main() { >> x!5(); >> } >> >> Ali > > But the length depends on runtime behavior. You said "setting the size at compile time", so I got confused. :) > Might be 5 or 100. This doesn't handle it, does it? No, it doesn't handle it. However, if the maximum length is reasonably small, then you can create the largest array and use first part of it: import std.stdio; enum maxElements = 100; void foo(int y) { bool[maxElements] storage; bool[] x = storage[0..y]; x[y/2] = true; writeln(x); } void main() { foo(5); } However, the ~= operator on x would still involve the GC. > I already make a simple malloc based array that does what I want. Looks > like a normal array with ~=, [], foreach. That's basically the same as std.container.Array, which already comes with a bool specialization: import std.stdio; import std.container; void foo(int y) { auto x = Array!bool(); x.length = y; x[y/2] = true; writeln(x[]); } void main() { foo(5); } > Does what I need it to do. Only works with BasicTypes of course. I would expect yours to work with user-defined types as well. Ali
Re: Get return type statically
On Tuesday, 28 June 2016 at 01:41:03 UTC, "Smoke" Adams wrote: I have a type public class SuperFunction(T) { T t; return(T) Do() { return t(); } } where T is a delegate or function. First, I would like to be able to specify that this must be the case for SuperFunction so we can't pass non-function/delegates for T. Second, How to specify the return type of Do to match that of T. e.g., SuperFunction!(bool function()) then return(T) should be bool. Similarly, I would like to extra the T's parameters and make Do have them also. This way, SuperFunction!T.Do emulates T in every way. I should mention that I am looking to make this as type safe as possible as if Do was declared exactly like T manually.
Get return type statically
I have a type public class SuperFunction(T) { T t; return(T) Do() { return t(); } } where T is a delegate or function. First, I would like to be able to specify that this must be the case for SuperFunction so we can't pass non-function/delegates for T. Second, How to specify the return type of Do to match that of T. e.g., SuperFunction!(bool function()) then return(T) should be bool. Similarly, I would like to extra the T's parameters and make Do have them also. This way, SuperFunction!T.Do emulates T in every way.
Re: Local fixed sized arrays
On Monday, 27 June 2016 at 22:56:35 UTC, Ali Çehreli wrote: On 06/27/2016 02:58 PM, Smoke Adams wrote: I'm in need of a way to create a local array that isn't GC'ed. It must be dynamic in the sense of setting the size at compile time but it will be used only in scope and only on structs. function x(int y) { bool[y] arr; arr ~= 3; } I care about slicing or anything but appending, removal, and indexing. I don't even need bounds checking. I don't see a need to get locked in to the GC for such simple cases. One way is to make x() a function template: import std.stdio; void x(int y)() { bool[y] arr; arr[y/2] = true; writeln(arr); } void main() { x!5(); } Ali But the length depends on runtime behavior. Might be 5 or 100. This doesn't handle it, does it? I already make a simple malloc based array that does what I want. Looks like a normal array with ~=, [], foreach. Does what I need it to do. Only works with BasicTypes of course.
Re: Local fixed sized arrays
On 06/27/2016 02:58 PM, Smoke Adams wrote: I'm in need of a way to create a local array that isn't GC'ed. It must be dynamic in the sense of setting the size at compile time but it will be used only in scope and only on structs. function x(int y) { bool[y] arr; arr ~= 3; } I care about slicing or anything but appending, removal, and indexing. I don't even need bounds checking. I don't see a need to get locked in to the GC for such simple cases. One way is to make x() a function template: import std.stdio; void x(int y)() { bool[y] arr; arr[y/2] = true; writeln(arr); } void main() { x!5(); } Ali
Re: Dynamic array of objects
On Monday, 27 June 2016 at 22:00:15 UTC, gummybears wrote: Hi, Today thought lets learn D. I am writing a compiler for a language and read D compiles very fast. Switched my compiler from C++ to D and ran my test suite to use D. Doing somethin wrong as creating array of objects gives me a segmentation fault Example import std.stdio; class Pair { float x; float y; this() { x = 0; y = 0; } } void main() { Pair[] arr; // segmentation fault on next line arr = new Pair[](10); arr[0].x = 3; arr[0].y = 4; writef("arr[0] = (%f,%f)",arr[0].x,arr[0].y); } You've allocated an array of 10 objects but didn't put any objects into it, so each of the entries is null (since classes are reference types in D). The line after the allocation fails as you try to access a null object. Either fill out the array with new objects (`arr[0] = new Pair()`), or convert Pair to a struct (structs are value types).
Dynamic array of objects
Hi, Today thought lets learn D. I am writing a compiler for a language and read D compiles very fast. Switched my compiler from C++ to D and ran my test suite to use D. Doing somethin wrong as creating array of objects gives me a segmentation fault Example import std.stdio; class Pair { float x; float y; this() { x = 0; y = 0; } } void main() { Pair[] arr; // segmentation fault on next line arr = new Pair[](10); arr[0].x = 3; arr[0].y = 4; writef("arr[0] = (%f,%f)",arr[0].x,arr[0].y); }
Local fixed sized arrays
I'm in need of a way to create a local array that isn't GC'ed. It must be dynamic in the sense of setting the size at compile time but it will be used only in scope and only on structs. function x(int y) { bool[y] arr; arr ~= 3; } I care about slicing or anything but appending, removal, and indexing. I don't even need bounds checking. I don't see a need to get locked in to the GC for such simple cases.
Forward References
Do the various D compilers use multiple passes to handle forward references or some other technique?
Registration-free COM client
Hi everyone, I've succeeded in using D as a client for regular (registered) COM servers in the past, but in this case, I'm building the server as well. I would like to avoid registering it if possible so XCOPY-like deployment remains an option. Can a registration-free COM client be built in D? If so, how can the code be setup to consume the manifest file, or alternately, is there a way to just point D to the correct assembly directly in code? Some background info: The project I'm working on requires a high degree of D and C# interop. Getting C# to invoke D via C linkage is simple, but I've encountered a lot of problems the other way around. Although it's technically possible to get pointers to C# objects such that the same approach could be used, doing so would require large portions of the code to be marked unsafe, the objects to be instantiated as fixed, use of GCHandle.Alloc, etc., which has a high dev learning curve and scalability issues. The typical solution is to use delegates as callbacks. This works, but it doesn't scale well to more complex scenarios. You can send a struct of delegates to the D layer and use that to access public methods in a class, but if one of those methods would normally return a instance of a different class and the caller will need to invoke methods within that other class, this approach breaks down. For example, MyClassInstance.MyProperty.DoSomething() can't be modeled as MyClassInstanceDelegate.MyPropertyDelegate.DoSomethingDelegate(). This fails marshaling, because delegates are not blittable. There's very likely a way to structure complex delegate hierarchies that would in the end be marshalable, but the implementation and maintenance overhead would be sizable. This leaves COM, which seems like it would work fine, on Windows anyway. (I'm not sure about Linux, but maybe some combo of Mono and WINE would do it? Not a high prio right now.) I'm hoping, though, to avoid having to register the C# COM server to keep things as simple as possible. thanks!
Re: Getting the template name of a template instantiation
On Monday, 27 June 2016 at 17:17:19 UTC, John wrote: import std.traits; __traits(identifier, TemplateOf!(S!int)); Scratch that, this is what you want: import std.traits; static assert(__traits(isSame, TemplateOf!(S!int), S)); I believe this is what import std.traits : isInstanceOf; is for. Thanks! I found both useful.
Re: Diff between function and delegate
On Monday, 27 June 2016 at 19:34:06 UTC, "Smoke" Adams wrote: I have alias fnc = void function(Object); alias del = void delegate(); Does func avoid the GC? I am passing in this to Object so I don't technically need a delegate or a "context". I want to be sure that I'm actually gaining something here by doing this. I read somewhere that delegates only require the GC when they use objects outside their scope. Do delegates always use the GC or only in certain cases? Delegate don't GC allocate when: - You take a pointer to a member function - The function accept a `scope` delegate and you pass a literal - You use `scope myDG = (Params) { body... }`
Diff between function and delegate
I have alias fnc = void function(Object); alias del = void delegate(); Does func avoid the GC? I am passing in this to Object so I don't technically need a delegate or a "context". I want to be sure that I'm actually gaining something here by doing this. I read somewhere that delegates only require the GC when they use objects outside their scope. Do delegates always use the GC or only in certain cases?
Re: executeShell doesn't work but system does
On Sunday, 26 June 2016 at 16:02:18 UTC, ag0aep6g wrote: On 06/26/2016 05:37 PM, Smoke Adams wrote: [...] Unsolicited spelling correction: no 'i' in "deprecated". [...] `system` directly prints its output, `executeShell` returns it in a tuple with the status code. Maybe cls works by printing some specific clear code. If so, you have to print the output of the command. [...] neither work but wait(spawnShell("cls"));
Re: Getting the template name of a template instantiation
On Monday, 27 June 2016 at 17:14:23 UTC, John wrote: On Monday, 27 June 2016 at 16:40:09 UTC, Nordlöw wrote: If I have a template parameter E = S!int where struct S(T) { S x; } how can I extract the template name part `S` from E`? Something like: static assert(is(templateName!(S!int) == S)); Is this already in Phobos somewhere? import std.traits; __traits(identifier, TemplateOf!(S!int)); Scratch that, this is what you want: import std.traits; static assert(__traits(isSame, TemplateOf!(S!int), S));
Re: Getting the template name of a template instantiation
On 06/27/2016 09:54 AM, Lodovico Giaretta wrote: On Monday, 27 June 2016 at 16:40:09 UTC, Nordlöw wrote: If I have a template parameter E = S!int where struct S(T) { S x; } how can I extract the template name part `S` from E`? Something like: static assert(is(templateName!(S!int) == S)); Is this already in Phobos somewhere? If I recall correctly, std.traits contains a TemplateOf trait, that should do exactly what you want. Yes, isIntanceOf: https://dlang.org/phobos/std_traits.html#isInstanceOf Ali
Re: Getting the template name of a template instantiation
On Monday, 27 June 2016 at 16:40:09 UTC, Nordlöw wrote: If I have a template parameter E = S!int where struct S(T) { S x; } how can I extract the template name part `S` from E`? Something like: static assert(is(templateName!(S!int) == S)); Is this already in Phobos somewhere? import std.traits; __traits(identifier, TemplateOf!(S!int));
Re: Getting the template name of a template instantiation
On Monday, 27 June 2016 at 16:40:09 UTC, Nordlöw wrote: If I have a template parameter E = S!int where struct S(T) { S x; } how can I extract the template name part `S` from E`? Something like: static assert(is(templateName!(S!int) == S)); Is this already in Phobos somewhere? If I recall correctly, std.traits contains a TemplateOf trait, that should do exactly what you want.
Getting the template name of a template instantiation
If I have a template parameter E = S!int where struct S(T) { S x; } how can I extract the template name part `S` from E`? Something like: static assert(is(templateName!(S!int) == S)); Is this already in Phobos somewhere?
Re: executeShell doesn't work but system does
On Sunday, 26 June 2016 at 19:01:07 UTC, cym13 wrote: On Sunday, 26 June 2016 at 17:56:08 UTC, Satoshi wrote: On Sunday, 26 June 2016 at 15:37:03 UTC, "Smoke" Adams wrote: system("cls") works but executeShell doesn't. system is depreciated. What's going on? The docs say that it creates a new process. I simply want to clear the console! I have problem with executeShell on windows 10 (LDC 1.0.0) too. When I rewrote it into http://prntscr.com/blc9j8 it works. OT but please, refrain from using screenshots. I know it's very customary on windows but I can't copy paste code from a screenshot to play with it and manually copying is error-prone. We manipulate text, let's stay with it. Sorry... It's same function as executeImpl just with changed byChunk to 1. It corrupt stack when I called the original function. https://github.com/ldc-developers/phobos/blob/aa133b5927bbc5f9669374d5bb0f206f6f68cfe4/std/process.d#L2130
Re: executeShell doesn't work but system does
On 6/26/16 12:02 PM, ag0aep6g wrote: On 06/26/2016 05:37 PM, Smoke Adams wrote: system("cls") works but executeShell doesn't. system is depreciated. Use spawn-related function, and avoid capturing output instead. Not sure if you need to call spawnShell (which creates a new system shell to execute the command), it depends on whether cls is a shell builtin or an executable. Unsolicited spelling correction: no 'i' in "deprecated". What's going on? The docs say that it creates a new process. I simply want to clear the console! `system` directly prints its output, `executeShell` returns it in a tuple with the status code. Maybe cls works by printing some specific clear code. If so, you have to print the output of the command. Perhaps not exactly correct, but close enough. When you call "executeShell", the subprocess is started with stdout/err sent to a pipe, and no console is passed to the subprocess. Probably cls sees it's not talking to a console and exits. -Steve