Re: Cannot cast char[] to string.
On Thursday, November 14, 2013 20:45:55 Brad Anderson wrote: > On Thursday, 14 November 2013 at 19:41:13 UTC, Agustin wrote: > > I'm trying to use http://dlang.org/phobos/std_net_curl.html and > > when i compile the same example i get: > > > > cannot implicitly convert expression > > (get(cast(const(char)[])address, AutoProtocol())) of type > > char[] to string > > > > string address = "http://dlang.org";; > > string _data = get(address); > > You have two options: > > string address = "http://dlang.org";; > string _data = get(address).idup(); // create immutable copy > > or > > string address = "http://dlang.org";; > char[] _data = get(address); // store mutable reference If you want a string rather than char[], it's probably better to use std.conv.to. In the case of char[], it'll just end up calling idup for you, but if you use to!string(arr) as the normal means for converting arrays of characters to string, then you don't have to worry about the constness of the array, and if it turns out that the array was in fact string (which is quite easy to have happen if you're dealing with generic code), then it'll avoid iduping the array and will simply return it. > A string (which is just an alias of immutable(char)[]) can't be > made from a char[] without an assertion that the data pointed to > is, in fact, immutable. You can do that using assumeUnique > (inexplicably found in std.exception). AFAIK, there is no way to make such an assertion. assumeUnique simply casts to immutable (though it will set the array passed in to null if you pass it an lvalue). So, it's completely up to the programmer to guarantee that the array is indeed unique. The primary used case for it is if you have to construct an array which you need to be immutable, and you need it to be mutable while you're setting all of its elements, in which case, you use assumeUnique on the array after you've set all of its elements, and you make sure that you don't have any other references to the array when you do that. If that's not what you're doing, you probably shouldn't be using assumeUnique. Certainly, using it on the result of a function that returns char[] is almost certainly wrong, and could result in very weird behavior, because the compiler is free to optimize based on the fact that the array is immutable, and if there's a mutable reference to that array, then you've subverted the type system, and ruined the compilers guarantees. - Jonathan M Davis
Re: Cannot cast char[] to string.
On 11/14/2013 11:43 AM, Dicebot wrote: On Thursday, 14 November 2013 at 19:41:13 UTC, Agustin wrote: I'm trying to use http://dlang.org/phobos/std_net_curl.html and when i compile the same example i get: cannot implicitly convert expression (get(cast(const(char)[])address, AutoProtocol())) of type char[] to string string address = "http://dlang.org";; string _data = get(address); `get` returns mutable data, one should respect it: char[] data = get(address); // or just use `auto data = ` However, that data can automatically be converted to string if get() were pure. (I can understand how such a function cannot be.) A simple wrapper: import std.net.curl; import std.exception; string getAsString(string address) { auto result = get(address); return assumeUnique(result); } void main() { string content = getAsString("dlang.org"); } Ali
Re: Cannot cast char[] to string.
On Thursday, 14 November 2013 at 19:41:13 UTC, Agustin wrote: I'm trying to use http://dlang.org/phobos/std_net_curl.html and when i compile the same example i get: cannot implicitly convert expression (get(cast(const(char)[])address, AutoProtocol())) of type char[] to string string address = "http://dlang.org";; string _data = get(address); You have two options: string address = "http://dlang.org";; string _data = get(address).idup(); // create immutable copy or string address = "http://dlang.org";; char[] _data = get(address); // store mutable reference A string (which is just an alias of immutable(char)[]) can't be made from a char[] without an assertion that the data pointed to is, in fact, immutable. You can do that using assumeUnique (inexplicably found in std.exception).
Re: Cannot cast char[] to string.
On Thursday, 14 November 2013 at 19:41:13 UTC, Agustin wrote: I'm trying to use http://dlang.org/phobos/std_net_curl.html and when i compile the same example i get: cannot implicitly convert expression (get(cast(const(char)[])address, AutoProtocol())) of type char[] to string string address = "http://dlang.org";; string _data = get(address); `get` returns mutable data, one should respect it: char[] data = get(address); // or just use `auto data = `
Cannot cast char[] to string.
I'm trying to use http://dlang.org/phobos/std_net_curl.html and when i compile the same example i get: cannot implicitly convert expression (get(cast(const(char)[])address, AutoProtocol())) of type char[] to string string address = "http://dlang.org";; string _data = get(address);
Re: cannot cast
On Thursday, May 03, 2012 10:54:47 Namespace wrote: > On Thursday, 3 May 2012 at 08:46:26 UTC, Chris Cain wrote: > > On Thursday, 3 May 2012 at 08:00:43 UTC, Namespace wrote: > >> So, you mean that if i declared any parameter as const, it > >> have to stay const all the time? > > > > Yes. const = you can't change. Changing it is invalid behavior. > > Imagine const/immutable as bits in readonly memory and you'll > > have to right mindset. > > > >> What would you do, if you need in a special case a mutable > >> version or must change the object itself? > >> Because there is no "mutable" keyword in D you have to cast > >> away the constness. > > > > In what way do you mean? If it's something you honestly _need_ > > to change and it's const, then maybe throwing an exception > > would be appropriate. > > I thought that const = "cannot change directly" and immutable > stands for "cannot change all the time". If not, why exist both > storage classes beside? An immutable variable can never be changed by any reference to that data. It's also implicitly shared across threads (since it can never change). If a const variable is a value type, then there really isn't any difference between const and immutable. If it's a reference type, then it just indicates that that particular reference cannot alter the data. Another reference may or may not be able to (and const is _not_ implicitly shared across threads, because the data _can_ change if there are mutable references to it). But if a reference is const, it's breaking the type system to cast away const and alter the data precisely because the compiler can't know whether that data is actually mutable or not. For instance, what if if you did something like const var = new immutable(A); var may be const, but it refers to a value which is actually immutable. Depending on what the compiler does, mutating var could result in nasty stuff like segfaults. In the general case, the compiler has no way of knowing whether a const variable is really mutable or immutable underneath. So, casting away const and mutating a variable is undefined behavior. As far as the compiler is concerned, a variable is _never_ mutated through a const reference, and it will optimize code based on that. So, casting away const can not only result in segfaults if the data is actually immutable, but it can result in incorrect behavior due to optimizations that the compiler makes based on the assumption that the variable wouldn't change but which you violated by casting away const and mutating the variable. Unlike immutable, _other_ references to the data may mutate it (and the compiler must take that into account when optimizing), but you should never try and mutate a const variable. Once something is const, _leave_ it that way. If you need a mutable reference to it, then you need to get one which was mutable in the first place rather than coming from the const reference through casting. Casting away const is legal, because D is a systems language, but actually mutating the variable after casting away const is undefined behavior, so you should never do it unless you really know what you're doing. http://stackoverflow.com/questions/4219600/logical-const-in-d - Jonathan M Davis
Re: cannot cast
On Thursday, 3 May 2012 at 09:00:08 UTC, Chris Cain wrote: const = you (as in, your view of the data as you're working with it) can't change Actually, let me be even clearer with this... I mean _you_ cannot change it, but it might be changed by someone else's view, in which case it would appear to change in your view ... but not by any interaction you make with it. This means concretely that anything you do with the object will have no effect on its bit representation in memory, although the bit representation in memory might be changed by other threads/processes/views.
Re: cannot cast
On Thursday, 3 May 2012 at 08:54:48 UTC, Namespace wrote: I thought that const = "cannot change directly" and immutable stands for "cannot change all the time". If not, why exist both storage classes beside? const = you (as in, your view of the data as you're working with it) can't change immutable = no one can change it ... as in, there exists no view of the data that can mutate it. It's a subtle distinction. In fact, you can be handed a "const" and it's actually immutable underneath. And since immutable data can actually be stored in read only memory...
Re: cannot cast
On Thursday, 3 May 2012 at 08:46:26 UTC, Chris Cain wrote: On Thursday, 3 May 2012 at 08:00:43 UTC, Namespace wrote: So, you mean that if i declared any parameter as const, it have to stay const all the time? Yes. const = you can't change. Changing it is invalid behavior. Imagine const/immutable as bits in readonly memory and you'll have to right mindset. What would you do, if you need in a special case a mutable version or must change the object itself? Because there is no "mutable" keyword in D you have to cast away the constness. In what way do you mean? If it's something you honestly _need_ to change and it's const, then maybe throwing an exception would be appropriate. I thought that const = "cannot change directly" and immutable stands for "cannot change all the time". If not, why exist both storage classes beside?
Re: cannot cast
On Thursday, 3 May 2012 at 08:00:43 UTC, Namespace wrote: So, you mean that if i declared any parameter as const, it have to stay const all the time? Yes. const = you can't change. Changing it is invalid behavior. Imagine const/immutable as bits in readonly memory and you'll have to right mindset. What would you do, if you need in a special case a mutable version or must change the object itself? Because there is no "mutable" keyword in D you have to cast away the constness. In what way do you mean? If it's something you honestly _need_ to change and it's const, then maybe throwing an exception would be appropriate.
Re: cannot cast
On 05/03/2012 09:33 AM, Namespace wrote: On Wednesday, 2 May 2012 at 22:38:36 UTC, Namespace wrote: Other, shorter example: [code] import std.stdio, std.traits; class A { int val; alias val this; T opCast(T : Object)() { writeln("FOO"); return to!(T)(this); } } class B : A { } T to(T : Object, U : Object)(const U obj) { return *(cast(T*) &obj); } T const_cast(T)(const T obj) { return cast(T) obj; } void main () { A a = new B(); a.val = 42; writefln("a.val: %d", a.val); B* b = cast(B*) &a; writefln("*b.val: %d", b.val); B b1 = to!(B)(a); writefln("b1.val: %d", b1.val); B b2 = cast(B) a; writefln("b2.val: %d", b2.val); const B b3 = cast(B) a; B b4 = const_cast(b3); } [/code] print: alias_this_impl.d(24): Error: function alias_this_impl.A.opCast!(B).opCast () is not callable using argument types () alias_this_impl.d(44): Error: template instance alias_this_impl.const_cast!(B) e rror instantiating I'm not very skillful in such "template" stories. Maybe someone can help me? Solved with T const_cast(T)(const T obj) { return to!(T)(obj); } But i think that there must exist a more nicer way to cast away const, isn't there? To cast away "const" with a simple cast to "T" fails (see my post above), because i have no idea, how i can restrict my opCast. So i have to convert it again with "to". Do some of you have any ideas how i can restrict my opCast, so my const_cast doesn't match it, e.g. with some template magic? Unqual können Sie finden in std.traits. template Unqual(T) { version (none) // Error: recursive alias declaration @@@BUG1308@@@ { static if (is(T U == const U)) alias Unqual!U Unqual; else static if (is(T U == immutable U)) alias Unqual!U Unqual; else static if (is(T U == inout U)) alias Unqual!U Unqual; else static if (is(T U == shared U)) alias Unqual!U Unqual; else alias T Unqual; } else // workaround { static if (is(T U == shared(const U))) alias U Unqual; else static if (is(T U == const U )) alias U Unqual; else static if (is(T U == immutable U )) alias U Unqual; else static if (is(T U == inout U )) alias U Unqual; else static if (is(T U == shared U )) alias U Unqual; else alias T Unqual; } } unittest { static assert(is(Unqual!(int) == int)); static assert(is(Unqual!(const int) == int)); static assert(is(Unqual!(immutable int) == int)); static assert(is(Unqual!(inout int) == int)); static assert(is(Unqual!(shared int) == int)); static assert(is(Unqual!(shared(const int)) == int)); alias immutable(int[]) ImmIntArr; static assert(is(Unqual!(ImmIntArr) == immutable(int)[])); }
Re: cannot cast
On Thursday, 3 May 2012 at 07:41:32 UTC, Simen Kjaeraas wrote: On Thu, 03 May 2012 00:38:35 +0200, Namespace wrote: I'm not very skillful in such "template" stories. Maybe someone can help me? The main problem here is your opCast is non-const. (it's always an indication of const problems when DMD says " is not callable using argument types ()") Solution: class A { int val; alias val this; T opCast(T : Object)() { writeln("FOO"); return to!(T)(this); } // Add this T opCast(T : Object)() const { writeln("FOO"); return to!(T)(this); } } My tests are failed, but isn't it possible, to reduce both methods to one with the inout keyword?
Re: cannot cast
If you want to restrict opCast, then use a template constraint, constraining it to what you want to work with it. Also, casting away const is generally a bad idea in D. Casting away const and mutating a variable is an _extremely_ bad idea. You _really_ shouldn't be doing it. So, the fact that you _have_ a function which is specifically trying to cast away const is almost certainly _not_ a good idea. http://stackoverflow.com/questions/4219600/logical-const-in-d - Jonathan M Davis So, you mean that if i declared any parameter as const, it have to stay const all the time? What would you do, if you need in a special case a mutable version or must change the object itself? Because there is no "mutable" keyword in D you have to cast away the constness.
Re: cannot cast
On Thursday, 3 May 2012 at 07:41:32 UTC, Simen Kjaeraas wrote: On Thu, 03 May 2012 00:38:35 +0200, Namespace wrote: I'm not very skillful in such "template" stories. Maybe someone can help me? The main problem here is your opCast is non-const. (it's always an indication of const problems when DMD says " is not callable using argument types ()") Solution: class A { int val; alias val this; T opCast(T : Object)() { writeln("FOO"); return to!(T)(this); } // Add this T opCast(T : Object)() const { writeln("FOO"); return to!(T)(this); } } Hm, simple. Thank you, as long as the bug isn't fixed, this has to be enough. :)
Re: cannot cast
On Thursday, May 03, 2012 09:33:01 Namespace wrote: > On Wednesday, 2 May 2012 at 22:38:36 UTC, Namespace wrote: > > Other, shorter example: > > > > [code] > > import std.stdio, std.traits; > > > > class A { > > > > int val; > > > > alias val this; > > > > T opCast(T : Object)() { > > > > writeln("FOO"); > > > > return to!(T)(this); > > > > } > > > > } > > > > class B : A { > > > > } > > > > T to(T : Object, U : Object)(const U obj) { > > > > return *(cast(T*) &obj); > > > > } > > > > T const_cast(T)(const T obj) { > > > > return cast(T) obj; > > > > } > > > > void main () { > > > > A a = new B(); > > a.val = 42; > > > > writefln("a.val: %d", a.val); > > > > B* b = cast(B*) &a; > > writefln("*b.val: %d", b.val); > > > > B b1 = to!(B)(a); > > writefln("b1.val: %d", b1.val); > > > > B b2 = cast(B) a; > > writefln("b2.val: %d", b2.val); > > > > const B b3 = cast(B) a; > > > > B b4 = const_cast(b3); > > > > } > > [/code] > > > > print: > > > > alias_this_impl.d(24): Error: function > > alias_this_impl.A.opCast!(B).opCast () is > > > > not callable using argument types () > > > > alias_this_impl.d(44): Error: template instance > > alias_this_impl.const_cast!(B) e > > rror instantiating > > > > I'm not very skillful in such "template" stories. Maybe someone > > can help me? > > Solved with > > T const_cast(T)(const T obj) { > return to!(T)(obj); > } > > But i think that there must exist a more nicer way to cast away > const, isn't there? > > To cast away "const" with a simple cast to "T" fails (see my post > above), because i have no idea, how i can restrict my opCast. So > i have to convert it again with "to". Do some of you have any > ideas how i can restrict my opCast, so my const_cast doesn't > match it, e.g. with some template magic? If you want to restrict opCast, then use a template constraint, constraining it to what you want to work with it. Also, casting away const is generally a bad idea in D. Casting away const and mutating a variable is an _extremely_ bad idea. You _really_ shouldn't be doing it. So, the fact that you _have_ a function which is specifically trying to cast away const is almost certainly _not_ a good idea. http://stackoverflow.com/questions/4219600/logical-const-in-d - Jonathan M Davis
Re: cannot cast
On Thu, 03 May 2012 00:38:35 +0200, Namespace wrote: I'm not very skillful in such "template" stories. Maybe someone can help me? The main problem here is your opCast is non-const. (it's always an indication of const problems when DMD says " is not callable using argument types ()") Solution: class A { int val; alias val this; T opCast(T : Object)() { writeln("FOO"); return to!(T)(this); } // Add this T opCast(T : Object)() const { writeln("FOO"); return to!(T)(this); } }
Re: cannot cast
On Wednesday, 2 May 2012 at 22:38:36 UTC, Namespace wrote: Other, shorter example: [code] import std.stdio, std.traits; class A { int val; alias val this; T opCast(T : Object)() { writeln("FOO"); return to!(T)(this); } } class B : A { } T to(T : Object, U : Object)(const U obj) { return *(cast(T*) &obj); } T const_cast(T)(const T obj) { return cast(T) obj; } void main () { A a = new B(); a.val = 42; writefln("a.val: %d", a.val); B* b = cast(B*) &a; writefln("*b.val: %d", b.val); B b1 = to!(B)(a); writefln("b1.val: %d", b1.val); B b2 = cast(B) a; writefln("b2.val: %d", b2.val); const B b3 = cast(B) a; B b4 = const_cast(b3); } [/code] print: alias_this_impl.d(24): Error: function alias_this_impl.A.opCast!(B).opCast () is not callable using argument types () alias_this_impl.d(44): Error: template instance alias_this_impl.const_cast!(B) e rror instantiating I'm not very skillful in such "template" stories. Maybe someone can help me? Solved with T const_cast(T)(const T obj) { return to!(T)(obj); } But i think that there must exist a more nicer way to cast away const, isn't there? To cast away "const" with a simple cast to "T" fails (see my post above), because i have no idea, how i can restrict my opCast. So i have to convert it again with "to". Do some of you have any ideas how i can restrict my opCast, so my const_cast doesn't match it, e.g. with some template magic?
Re: cannot cast
Other, shorter example: [code] import std.stdio, std.traits; class A { int val; alias val this; T opCast(T : Object)() { writeln("FOO"); return to!(T)(this); } } class B : A { } T to(T : Object, U : Object)(const U obj) { return *(cast(T*) &obj); } T const_cast(T)(const T obj) { return cast(T) obj; } void main () { A a = new B(); a.val = 42; writefln("a.val: %d", a.val); B* b = cast(B*) &a; writefln("*b.val: %d", b.val); B b1 = to!(B)(a); writefln("b1.val: %d", b1.val); B b2 = cast(B) a; writefln("b2.val: %d", b2.val); const B b3 = cast(B) a; B b4 = const_cast(b3); } [/code] print: alias_this_impl.d(24): Error: function alias_this_impl.A.opCast!(B).opCast () is not callable using argument types () alias_this_impl.d(44): Error: template instance alias_this_impl.const_cast!(B) e rror instantiating I'm not very skillful in such "template" stories. Maybe someone can help me?
Re: cannot cast
Can anyone tell me, why the this code [code] module RefTest.Ref; import std.stdio : writeln; import std.conv : to, toImpl; T const_cast(T : Object)(const T obj) { return cast(T) obj; } struct Ref(T : Object) { private: T _obj; public: @disable this();// { } @disable this(typeof(null));// { } this(T obj) { assert(obj !is null, "Object is null!"); this._obj = obj; } @property inout(T) access() inout { assert(this._obj !is null, "Access: Object is null!"); return this._obj; } //alias access this; // dann kommt "Stackoverflow" oder "recursive expansion" } mixin template TRef(T : Object) { final Ref!(T) getRef(string file = __FILE__, size_t line = __LINE__) in { assert(this !is null, "Object is null! @ " ~ file ~ " in Line " ~ to!(string)(line) ~ "."); } body { return Ref!(T)(this); } final Ref!(const T) getRef(string file = __FILE__, size_t line = __LINE__) const in { assert(this !is null, "Object is null! @ " ~ file ~ " in Line " ~ to!(string)(line) ~ "."); } body { return Ref!(const T)(this); } U opCast(U : Object)() { return *(cast(U*) &this); } alias getRef this; } unittest { bool instanceof(T : Object, U : Object)(const Ref!U obj) { //return const_cast(obj.access).toString() == typeid(T).toString(); const U o = obj.access; return const_cast(o).toString() == typeid(T).toString(); } class A { mixin TRef!(A); } class B : A { } class C : B { } A a1 = new B(); A a2 = new C(); assert(instanceof!(A)(a1) == false); assert(instanceof!(B)(a1)); assert(instanceof!(C)(a1) == false); writeln(a1); B b1 = cast(B) a1; writeln(b1); writeln(); } [/code] fails with: [quote] Fehler 3 instantiated from here: instanceof!(A,A) D:\D\VisualD\Visual Studio 2010\Projects\RefTest\RefTest\Ref.d 76 Fehler 2 Error: template instance RefTest.Ref.const_cast!(A) error instantiating D:\D\VisualD\Visual Studio 2010\Projects\RefTest\RefTest\Ref.d 62 Fehler 4 Error: template instance RefTest.Ref.__unittest1.instanceof!(A,A) error instantiating D:\D\VisualD\Visual Studio 2010\Projects\RefTest\RefTest\Ref.d 76 Fehler 1 Error: function RefTest.Ref.__unittest1.A.TRef!(A).opCast!(A).opCast () is not callable using argument types () D:\D\VisualD\Visual Studio 2010\Projects\RefTest\RefTest\Ref.d 7 [/quote] ? Sounds like a bug.
Re: cannot cast
On Sunday, 29 April 2012 at 14:40:55 UTC, Jesse Phillips wrote: On Saturday, 28 April 2012 at 14:21:32 UTC, Namespace wrote: I finished my Ref/NotNull struct, but i've got a problem: If i try to cast the class, which should implicit convert to Ref!(Type) with alias this, i get the following error message: "cannot cast a1.getRef("Ref.d",72u) of type Ref!(A) to type RefTest.Ref.__unittest1.B" I would consider this a bug: http://d.puremagic.com/issues/show_bug.cgi?id=8001 Oh, that could explain a lot. Some workaround until now?
Re: cannot cast
On Saturday, 28 April 2012 at 14:21:32 UTC, Namespace wrote: I finished my Ref/NotNull struct, but i've got a problem: If i try to cast the class, which should implicit convert to Ref!(Type) with alias this, i get the following error message: "cannot cast a1.getRef("Ref.d",72u) of type Ref!(A) to type RefTest.Ref.__unittest1.B" I would consider this a bug: http://d.puremagic.com/issues/show_bug.cgi?id=8001
Re: cannot cast
On Saturday, 28 April 2012 at 14:21:32 UTC, Namespace wrote: I finished my Ref/NotNull struct, but i've got a problem: If i try to cast the class, which should implicit convert to Ref!(Type) with alias this, i get the following error message: "cannot cast a1.getRef("Ref.d",72u) of type Ref!(A) to type RefTest.Ref.__unittest1.B" Can someone explain that to me or help me with it? It seems that alias this replacement isn't smart enough to distinguish, if it has to convert. [code] import std.conv : to; struct Ref(T : Object) { private: T _obj; public: @disable this();// { } @disable this(typeof(null));// { } this(T obj) { assert(obj !is null, "Object is null!"); this._obj = obj; } @property inout(T) access() inout { assert(this._obj !is null, "Access: Object is null!"); return this._obj; } //alias access this; // print "Stackoverflow" or "recursive expansion" } mixin template TRef(T : Object) { final Ref!(T) getRef(string file = __FILE__, size_t line = __LINE__) in { assert(this !is null, "Object is null! @ " ~ file ~ " in Line " ~ to!(string)(line) ~ "."); } body { return Ref!(T)(this); } final Ref!(const T) getRef(string file = __FILE__, size_t line = __LINE__) const in { assert(this !is null, "Object is null! @ " ~ file ~ " in Line " ~ to!(string)(line) ~ "."); } body { return Ref!(const T)(this); } alias getRef this; } unittest { class A { mixin TRef!(A); } class B : A { } class C : B { } A a1 = new B(); A a2 = new C(); B b1 = cast(B) a1; // line 72 } Trying to fix it with an opCast in Ref ended in an infinity loop...
cannot cast
I finished my Ref/NotNull struct, but i've got a problem: If i try to cast the class, which should implicit convert to Ref!(Type) with alias this, i get the following error message: "cannot cast a1.getRef("Ref.d",72u) of type Ref!(A) to type RefTest.Ref.__unittest1.B" Can someone explain that to me or help me with it? It seems that alias this replacement isn't smart enough to distinguish, if it has to convert. [code] import std.conv : to; struct Ref(T : Object) { private: T _obj; public: @disable this();// { } @disable this(typeof(null));// { } this(T obj) { assert(obj !is null, "Object is null!"); this._obj = obj; } @property inout(T) access() inout { assert(this._obj !is null, "Access: Object is null!"); return this._obj; } //alias access this; // print "Stackoverflow" or "recursive expansion" } mixin template TRef(T : Object) { final Ref!(T) getRef(string file = __FILE__, size_t line = __LINE__) in { assert(this !is null, "Object is null! @ " ~ file ~ " in Line " ~ to!(string)(line) ~ "."); } body { return Ref!(T)(this); } final Ref!(const T) getRef(string file = __FILE__, size_t line = __LINE__) const in { assert(this !is null, "Object is null! @ " ~ file ~ " in Line " ~ to!(string)(line) ~ "."); } body { return Ref!(const T)(this); } alias getRef this; } unittest { class A { mixin TRef!(A); } class B : A { } class C : B { } A a1 = new B(); A a2 = new C(); B b1 = cast(B) a1; // line 72 }
Re: Cannot cast void* to arrays..?
On Fri, 24 Feb 2012 21:36:21 +0100, Andrej Mitrovic wrote: On 2/24/12, simendsjo wrote: I don't get it. This gives me a dynamic array, not a static: char[1] a; auto b = cast(void*)a; auto c = (cast(char*)b)[0..1]; c.length = 10; // auch! You can do: char[1] c = (cast(char*)b)[0..1]; Thanks! I had to do an explicit cast(char[1]) (or actually char[1][1] in my case.)
Re: Cannot cast void* to arrays..?
On 2/24/12, simendsjo wrote: > I don't get it. This gives me a dynamic array, not a static: > char[1] a; > auto b = cast(void*)a; > auto c = (cast(char*)b)[0..1]; > c.length = 10; // auch! > You can do: char[1] c = (cast(char*)b)[0..1];
Re: Cannot cast void* to arrays..?
On Fri, Feb 24, 2012 at 11:56:18AM -0800, Ali Çehreli wrote: [...] > char[1] a; > auto c = a.ptr[0..a.length]; [...] Hey, that's an awesome way to implement copy-on-write static arrays! I'll have to use that sometime. :) T -- Sometimes the best solution to morale problems is just to fire all of the unhappy people. -- despair.com
Re: Cannot cast void* to arrays..?
On Fri, 24 Feb 2012 20:56:22 +0100, H. S. Teoh wrote: 24.02.2012 21:34, simendsjo пишет: >char[] a; >auto b = cast(void*)a; >auto c = cast(char[])b; // Error: e2ir: cannot cast b of type >void* to type char[] [...] Just out of curiosity, what are you trying to accomplish with this cast? In almost all normal D code, there's no need for any casting at all. T Interacting with a C callback taking a void*. In my callback, I want to get the same type back. See my previous question: http://forum.dlang.org/post/op.v963zyg0x8p62v@simendsjo-desktop (although I didn't include the parameters in that example)
Re: Cannot cast void* to arrays..?
On Fri, 24 Feb 2012 20:56:18 +0100, Ali Çehreli wrote: On 02/24/2012 11:44 AM, simendsjo wrote: On Fri, 24 Feb 2012 20:42:20 +0100, Justin Whear wrote: On Fri, 24 Feb 2012 20:34:19 +0100, simendsjo wrote: char[] a; auto b = cast(void*)a; auto c = cast(char[])b; // Error: e2ir: cannot cast b of type void* to type char[] Arrays have a length--you need to cast the pointer to a char*, then slice it. Ah, of course, thanks. But what about static arrays? char[1] a; //a.length = 10; // constant a.length is not an lvalue auto b = cast(void*)a; auto c = cast(char[1])b; // Error: e2ir: cannot cast b of type void* to type char[1LU] char[1] a; auto c = a.ptr[0..a.length]; Ali I don't get it. This gives me a dynamic array, not a static: char[1] a; auto b = cast(void*)a; auto c = (cast(char*)b)[0..1]; c.length = 10; // auch!
Re: Cannot cast void* to arrays..?
On 02/24/2012 11:44 AM, simendsjo wrote: On Fri, 24 Feb 2012 20:42:20 +0100, Justin Whear wrote: On Fri, 24 Feb 2012 20:34:19 +0100, simendsjo wrote: char[] a; auto b = cast(void*)a; auto c = cast(char[])b; // Error: e2ir: cannot cast b of type void* to type char[] Arrays have a length--you need to cast the pointer to a char*, then slice it. Ah, of course, thanks. But what about static arrays? char[1] a; //a.length = 10; // constant a.length is not an lvalue auto b = cast(void*)a; auto c = cast(char[1])b; // Error: e2ir: cannot cast b of type void* to type char[1LU] char[1] a; auto c = a.ptr[0..a.length]; Ali
Re: Cannot cast void* to arrays..?
> 24.02.2012 21:34, simendsjo пишет: > >char[] a; > >auto b = cast(void*)a; > >auto c = cast(char[])b; // Error: e2ir: cannot cast b of type > >void* to type char[] [...] Just out of curiosity, what are you trying to accomplish with this cast? In almost all normal D code, there's no need for any casting at all. T -- The right half of the brain controls the left half of the body. This means that only left-handed people are in their right mind. -- Manoj Srivastava
Re: Cannot cast void* to arrays..?
24.02.2012 21:34, simendsjo пишет: char[] a; auto b = cast(void*)a; auto c = cast(char[])b; // Error: e2ir: cannot cast b of type void* to type char[] Generally, you should not cast a struct to pointer and vise-versa. Besides, size of array structure is larger than size of pointer, and that triggers error in your case.
Re: Cannot cast void* to arrays..?
On Fri, 24 Feb 2012 20:42:20 +0100, Justin Whear wrote: On Fri, 24 Feb 2012 20:34:19 +0100, simendsjo wrote: char[] a; auto b = cast(void*)a; auto c = cast(char[])b; // Error: e2ir: cannot cast b of type void* to type char[] Arrays have a length--you need to cast the pointer to a char*, then slice it. Ah, of course, thanks. But what about static arrays? char[1] a; //a.length = 10; // constant a.length is not an lvalue auto b = cast(void*)a; auto c = cast(char[1])b; // Error: e2ir: cannot cast b of type void* to type char[1LU]
Re: Cannot cast void* to arrays..?
On Fri, 24 Feb 2012 20:34:19 +0100, simendsjo wrote: > char[] a; > auto b = cast(void*)a; > auto c = cast(char[])b; // Error: e2ir: cannot cast b of type void* > to > type char[] Arrays have a length--you need to cast the pointer to a char*, then slice it.
Re: Cannot cast void* to arrays..?
On Fri, Feb 24, 2012 at 08:34:19PM +0100, simendsjo wrote: > char[] a; > auto b = cast(void*)a; > auto c = cast(char[])b; // Error: e2ir: cannot cast b of type > void* to type char[] D arrays are not the same as C arrays. D arrays also include length in addition to the pointer, so you can't just cast a void* to an array. T -- Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
Cannot cast void* to arrays..?
char[] a; auto b = cast(void*)a; auto c = cast(char[])b; // Error: e2ir: cannot cast b of type void* to type char[]