Re: Problem overloading operator for a struct with an immutable member
On Thursday, 26 March 2015 at 04:57:55 UTC, ketmar wrote: by the way. do you know that you still CAN overload postincrement operation? yes, the code is still here, and it works... somethimes. ;-) Thnaks. Indeed, this works: --- struct S { int i; immutable(Object) o; void opAddAssign(int j) { i += j; } S opPostInc() { ++i; return this; } void opAssign(S other) {} } unittest { S s; ++s; assert(s.i == 1); s++; assert(s.i == 2); } --- Old operator overloading to the rescue !
Problem overloading operator for a struct with an immutable member
I don't know if this is a bug or expected behaviour. The struct is mutable, assignable and pre-increment operator works. But post-increment doesn't compile because of the immutable member. -- struct S { int i; immutable(Object) o; S opUnary(string op)() { return this; } void opAssign(S other) {} } void main() { S s, t; t = s; // OK ++s; // OK s++; // Error: cannot modify struct s S with immutable members } ---
Re: Testing implicit conversion to template instance with is() expression
On Sunday, 15 March 2015 at 18:33:32 UTC, Marc Schütz wrote: On Sunday, 15 March 2015 at 17:03:42 UTC, Marc Schütz wrote: https://issues.dlang.org/show_bug.cgi?id=14286 In the meantime, does someone know of a suitable workaround? I found the following workaround. Not beautiful, but it works: enum isValue(alias T) = __traits(compiles, typeof(T)); template isConvertibleToInstanceOf(alias From, alias To) if(isValue!From) { enum isConvertibleToInstanceOf = isConvertibleToInstanceOf!(typeof(From), To); } template isConvertibleToInstanceOf(From, alias To) if(!is(From == struct) !is(From == class) !is(From == interface)) { enum isConvertibleToInstanceOf = false; } template isConvertibleToInstanceOf(From, alias To) if(is(From == struct) || is(From == class) || is(From == interface)) { // workaround for https://issues.dlang.org/show_bug.cgi?id=14286 import std.typetuple : anySatisfy; enum aliasThisConvertible(string name) = isConvertibleToInstanceOf!(mixin(typeof(From. ~ name ~ )), To); enum isConvertibleToInstanceOf = anySatisfy!(aliasThisConvertible, __traits(getAliasThis, From)) || is(From : To!Args, Args...); } It works for your previous code example: static assert(isConvertibleToInstanceOf!(S!10, V)); // OK But this also works: static assert(!isConvertibleToInstanceOf!(S!10, V!abc)); // OK Can be reduced to: struct Foo(int i) {} alias Foo1 = Foo!1; static assert(is(Foo!2 == Foo1!T, T...)); // OK I think it's another bug.
Re: enum and static if
On Wednesday, 11 March 2015 at 17:19:20 UTC, ketmar wrote: On Wed, 11 Mar 2015 18:17:38 +0100, Artur Skawina via Digitalmars-d-learn wrote: On 03/11/15 15:41, ketmar via Digitalmars-d-learn wrote: On Wed, 11 Mar 2015 14:36:07 +, wobbles wrote: On Wednesday, 11 March 2015 at 14:34:32 UTC, ketmar wrote: On Wed, 11 Mar 2015 13:48:45 +, Namespace wrote: This code does not work: enum Test { Foo, static if (__VERSION__ = 2067) Bar, } Quatz } Any chance that this could work? nope. `static if` is statement, so it works only where statement is allowed. the same is true for `version`. this is by design. You can do something like static if (__VERSION__ = 2067) enum Test{ ... } else enum Test{ ... } as a workaround? sure, but you have to copypaste the whole enum in both places. maybe allowing `version` in enums worth a ER... mixin(` enum Test { Foo,` ~(__VERSION__=2067?` Bar,`:``) ~` Quatz }`); artur yes, it works. it also can be a participant in ugly D code of the month contest. ;-) The second prize in this contest could for: mixin(` enum Test { Foo, %s Quatz }` .format(__VERSION__ = 2067 ? Bar, : )); :)
Re: Int to float?
On Friday, 6 March 2015 at 00:57:16 UTC, Ola Fosheim Grøstad wrote: On Thursday, 5 March 2015 at 23:50:28 UTC, Jesse Phillips wrote: I think I read somewhere you don't want to use unions like this, but I think it is more because you generally don't want to reinterpret bits. It is non-portable, since some hardware architectures may use different representations (e.g. different byte order on int and float). Then maybe use std.bitmanip? import std.bitmanip; int i = 5; float f = bigEndianToNative!float(nativeToBigEndian(i)); // or float f = littleEndianToNative!float(nativeToLittleEndian(i));
Re: SQLLite driver
On Sunday, 14 December 2014 at 13:47:21 UTC, Suliman wrote: On Sunday, 14 December 2014 at 13:33:27 UTC, Suliman wrote: There is also a branch named `develop` which at least compiles, maybe it is usable. how to add to dub this branch? Compiling using dmd... Linking... OPTLINK (R) for Win32 Release 8.00.15 Copyright (C) Digital Mars 1989-2013 All rights reserved. http://www.digitalmars.com/ctg/optlink.html sqlite3.lib Warning 2: File Not Found sqlite3.lib C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_close C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_enable_shared_cache C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_open C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_changes C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_total_changes C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_errcode C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_errmsg C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_open_v2 C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_finalize C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_bind_parameter_count C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_clear_bindings C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_reset C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_step C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_prepare_v2 C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_column_count C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_column_type C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_column_name C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_column_text C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_column_double C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_column_int64 C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_column_blob C:\Users\Dima\AppData\Roaming\dub\packages\d2sqlite3-0.5.2\d2sqlite3.lib(d2sqlit e3) Error 42: Symbol Undefined _sqlite3_column_bytes --- errorlevel 22 FAIL .dub\build\application-debug-windows-x86-dmd_2066-668EB54A2EBB0CE5C55E2AC62 166BCB8\ seismodownloader executable Error executing command run: dmd failed with exit code 22. Warning 2: File Not Found sqlite3.lib Does it's mean that I should to find this lib and put it in package folder? Yes, you need to link the sqlite3 library, but I'm sorry I can't help you more, because I've never done this with dub on Windows... The develop branch is more up-to-date. It should compile with 2.066.1. And the API is supposed to be cleaner. See the examples to find the changes. -- Nicolas
Re: Delegates and C function pointers
On Saturday, 8 November 2014 at 16:01:09 UTC, anonymous wrote: On Saturday, 8 November 2014 at 12:23:45 UTC, Nicolas Sicard wrote: I would like to register a D delegate to a C API that takes a function pointer as a callback and a void* pointer to pass data to this callback. My solution is in http://dpaste.dzfl.pl/7d9b504b4b965. Is this code correct? Is there something simpler or already in Phobos that I have overlooked? Thanks -- Nicolas I think it's a little more complicated than it needs to be. Instead of going delegate-DelegateData*-delegate you can pass a pointer to the delegate: void doSomethingFromD(void delegate() dg) { static extern(C) void adapter(void* ptr) { auto dg = *cast(void delegate()*)ptr; dg(); } dosomething(adapter, dg); } dg is fine if dosomething calls the callback before doSomethingFromD returns. If the callback is stored and called later on, you have to put the delegate somewhere more lasting. And you have to make sure that the GC doesn't collect it in the meantime. In your code you're new-ing, but you don't keep the reference around in D-land. The GC would happily collect the delegate then, because it doesn't look in C-land for references. For example, you could add all callbacks to a module level array: void delegate()[] activeCallbacks; void doSomethingFromD(void delegate() dg) { static extern(C) void adapter(void* ptr) { auto dg = *cast(void delegate()*)ptr; dg(); } activeCallbacks ~= dg; dosomething(adapter, activeCallbacks[$ - 1]); } Then when everything's done and you know that the callbacks are not needed anymore, empty activeCallbacks so that the GC can collect them. Essentially, you're back to manual management, and have to think about the life times of the callbacks. Thanks for the advice about the GC managed references! I'll have a look at it. -- Nicolas
Re: Trouble with std.Variant
On Thursday, 18 September 2014 at 21:14:55 UTC, Ali Çehreli wrote: On 09/18/2014 02:06 PM, ddos wrote: The following code fails because Vec2.length() does not return int ... so Variant is only usable with types that do not have a method with name length() ?? i'm confused On Thursday, 18 September 2014 at 21:03:47 UTC, ddos wrote: struct Vec2 { float[2] vec; public float length() { return sqrt(vec[0]*vec[0]+vec[1]*vec[1]); } } int main(string[] argv) { Vec2 test; Variant v = test; return 0; } Compiles and runs without error with dmd git head, after adding the following two lines: ;) import std.math; import std.variant; Ali It shouldn't work in dmd git head. See Andrei's answer in https://issues.dlang.org/show_bug.cgi?id=5501
Re: mixin assembler does not work?
On Sunday, 20 July 2014 at 15:02:58 UTC, Foo wrote: On Sunday, 20 July 2014 at 14:55:00 UTC, Foo wrote: For clarification: how would that work without mixin + string? I tried this: mixin template Vala2(uint count, alias arr) { asm { sub ESP, count; mov arr, count; mov arr + 4, ESP; } } but I get several errors. Unfortunately it seems that asm cannot be used in mixin templates?! The reason may be that mixin templates are just for inserting declarations, which asm blocks aren't. This limitation isn't specific to asm.