Re: Adding a method to an enum.
On Mon, 20 Jun 2011 02:48:30 +, Charles McAnany wrote: > Hi, all. I'm looking for a way to make constants that have methods > without a lot of overhead. In particular, a way to define a Direction > and then be able to rotate it right. Here's kind of what I have in mind: > > enum Direction{ > left, right, up, down; > > public Direction rotateRight(){ > switch(this){ > case left: >return up; > case up: >return right; > case right: >return down; > case down: >return left; > } > } It may be acceptable to change a Direction variable freely in a different context. So I think that the requirement on how a Direction variable change should not be on the Direction type itself. How about a Dial that has a Direction: struct Dial { Direction direction_; void rotateRight(int count = 1) { direction_ += count; direction_ %= Direction.sizeof; } void rotateLeft(int count = 1) { rotateRight(-count); } @property Direction direction() const { return direction_; } } Ali
Re: Linker errors on OSX
"Peter Alexander" wrote in message news:itlk5g$e7t$1...@digitalmars.com... > I've been having strange linker errors recently and I have no ideas why > they've started happening. > > Undefined symbols: > "_D3std9exception7bailOutFAyaixAaZv", referenced from: > _D3std9exception148__T7enforceTbVAyaa60_2f4c6962726172792f436f6d70696c6572732f646d64322f6f73782f62696e2f2e2e2f2e2e2f7372632f70686f626f732f7374642f737464696f2e64Vi1481Z7enforceFbLAxaZb > in test.o > ld: symbol(s) not found > collect2: ld returned 1 exit status Last time I got something like this it was from an outdated phobos.lib on my path. Could be what's happening to you.
Re: Adding a method to an enum.
On 2011-06-19 19:48, Charles McAnany wrote: > Hi, all. I'm looking for a way to make constants that have methods > without a lot of overhead. In particular, a way to define a > Direction and then be able to rotate it right. Here's kind of what I > have in mind: > > enum Direction{ > left, right, up, down; > > public Direction rotateRight(){ > switch(this){ > case left: >return up; > case up: >return right; > case right: >return down; > case down: >return left; > } > } > > > The best I can think of is to make a new method, which isn't > terrible, but not elegant, either. > Any thoughts? Well, you can have a function like that no problem. But you can't add it to an enum. It's just a free-standing function which takes an enum and returns the Direction to its right. Now, in theory, you could define an enum which was a struct which had a function which returned the Direction to the right, but you can't currently have enums of struct type with more than one value ( http://d.puremagic.com/issues/show_bug.cgi?id=4423 ), though it's supposed to. Of course, defining such a function could be entertaining given that it would effectively be recursive in nature as far as its definition goes (since each enum would be of the struct type and wouldn't exist unless the struct type can be instantiated, but the struct type couldn't have such a function unless it could be instantiated...). So, I don't know if you could get it to work with a struct anyway. The simplest thing to do is to simply have a free function which takes a Direction and returns the Direction to its right. There shouldn't be any need to be able to call the function on the enum itself. - Jonathan M Davis
Re: Any (working) JSON library for D2?
Doost : http://www.dsource.org/projects/doost Has a serializer that can read value to and from JSon! ;) "Johannes Pfau" wrote in message news:20110619193834.2c6afdf7@jpf-Satellite-A100... std.json doesn't work at all because of bug #2962 . I tried to remove the problematic part from std.json and got it to compile (by disabling support for floating point numbers...) but using it correctly creates very verbose code: -- JSONValue a; if(a.type == JSONTYPE.OBJECT) if("member" in a) if(a["member"].type == TYPE.NUMBER) uint count = a["member"].number; -- (pseudo-code, but that's the basic workflow when using std.json) I know there also was a std.json replacement proposed by Robert Jacques but it requires significant patches to phobos, a std.variant replacement and the patches are against phobos 2.050 or something like that, so those could be out of date. To cut a long story short: does anyone know of another JSON library for D2? -- Johannes Pfau
Re: problem with array of delegates!
There is a remove() method in std.algorithm!I even got asked why I was reimplementing it! (well, because I didn't know it existed hey!) works fine with, say, int... but not with delegate! associative array will solve the problem indeed.. (I hope) but they use way more memory! it would be nice to have remove working() :) Further, as you can see in my post, even my (reasonable) implementation of removeAt() fail! :( (but, again, it works for int!) "Andrej Mitrovic" wrote in message news:mailman.1010.1308495216.14074.digitalmars-d-le...@puremagic.com... Remove takes an offset, not a value as far as I know. If you need fast lookup and removal you could use hashes instead: int main(string[] argv) { auto a = new A; SlotDelegate x = &a.DIT; bool[SlotDelegate] _slotDg; _slotDg.remove(x); return 0; }
Adding a method to an enum.
Hi, all. I'm looking for a way to make constants that have methods without a lot of overhead. In particular, a way to define a Direction and then be able to rotate it right. Here's kind of what I have in mind: enum Direction{ left, right, up, down; public Direction rotateRight(){ switch(this){ case left: return up; case up: return right; case right: return down; case down: return left; } } The best I can think of is to make a new method, which isn't terrible, but not elegant, either. Any thoughts? Thanks, Charles.
RE: dmdscript osx.mak
Hi Robert and Dmitry, Thanks for your replies and the heads up on the current status of DMDScript! Josh
Re: Any (working) JSON library for D2?
Johannes Pfau : > The only difference is the argument order for dmd! Aye, I saw this one when I updated to 2.053 now I remember wasting an hour on that bug! Bugzilla suggests for the workaround to just put a dummy module in there as the first argument: icehack.d module icehack; import std.json; static if(__traits(compiles, parseJSON("hello"))) {} = Compile: dmd icehack.d [the rest of your arguments] It has to do with something in dmd not being initialized in the proper order... or something. But it's a fairly recent regression and pretty easily worked around if it comes up. My work project incorporated this into it's makefile and that's what made the pain stop.
Linker errors on OSX
I've been having strange linker errors recently and I have no ideas why they've started happening. When compiling/linking: import std.stdio; void main() { writeln("Hello"); } with DMD 2.053 I get linker error: Undefined symbols: "_D3std9exception7bailOutFAyaixAaZv", referenced from: _D3std9exception148__T7enforceTbVAyaa60_2f4c6962726172792f436f6d70696c6572732f646d64322f6f73782f62696e2f2e2e2f2e2e2f7372632f70686f626f732f7374642f737464696f2e64Vi1481Z7enforceFbLAxaZb in test.o ld: symbol(s) not found collect2: ld returned 1 exit status My first thought was that it isn't linking with phobos, but compiling with -v confirms that it is: gcc test.o -o test -m32 -Xlinker -L/Library/Compilers/dmd2/osx/bin/../lib -lphobos2 -lpthread -lm My GCC version is 4.2.1 $ gcc --version i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5664) Copyright (C) 2007 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. I've tried re-downloading DMD again, but this didn't work. It appeared to start happening after running an update of all my outdated packages in MacPorts, but I can't be sure that that's the issue. Any help tracking down the problem would be much appreciated. Thanks.
Re: Any (working) JSON library for D2?
Adam D. Ruppe wrote: >Johannes Pfau wrote: >> I guess you do not have similar helper functions to parse JSON? > >My other message has some. It isn't quite as nice to use though - >getting structs and such takes a little bit of work. > >For example, I use it to get stuff from Facebook, and it >looks kinda like this: > >=== > auto request = > parseSignedRequest(signed_request).get!(Variant[string]); > > if("page" in request) { > auto page = request["page"].get!(Variant[string]); > pageId = page["id"].coerce!string; > likes = page["liked"].get!bool; > } >=== > >It's not as beautiful as it could be, but it works reasonably >well anyway, which is why I'm happy enough with it as it is. > > >> Also how do you workaround bug #2962? > >I don't know anymore! For a while, I used a private fork of >std.json with the floating point functionality removed and >a utf related bug worked around, but now that fork is completely >commented out and I just use the stock std.json. > >Problem is I don't remember if it's because the bugs got fixed >upstream, or if they just didn't bother me anymore... > >Regardless though, it works in a test on my box at least. >Paste that code into a fresh file. > > >void main() { > auto v = jsonToVariant("4.2"); > writeln(v.get!real); >} > >compiles and runs correctly. That's interesting, that code works indeed. Even more interesting: dmd src/etc/curl.d src/vevo/cli/main.d src/vevo/api.d -ofvevo /usr/include/d/dmd/phobos/std/conv.d(1301): Error: function std.conv.parse!(real,string).parse compiler error, parameter 'p', bugzilla 2962? dmd: glue.c:744: virtual void FuncDeclaration::toObjFile(int): Assertion `0' failed. But this works: dmd src/vevo/api.d src/etc/curl.d src/vevo/cli/main.d -ofvevo The only difference is the argument order for dmd! Thinking of it I think I saw a similar bug when compiling dustmite. It consists of only two files, but it compiles only one way. -- Johannes Pfau
Re: Any (working) JSON library for D2?
Johannes Pfau wrote: > I guess you do not have similar helper functions to parse JSON? My other message has some. It isn't quite as nice to use though - getting structs and such takes a little bit of work. For example, I use it to get stuff from Facebook, and it looks kinda like this: === auto request = parseSignedRequest(signed_request).get!(Variant[string]); if("page" in request) { auto page = request["page"].get!(Variant[string]); pageId = page["id"].coerce!string; likes = page["liked"].get!bool; } === It's not as beautiful as it could be, but it works reasonably well anyway, which is why I'm happy enough with it as it is. > Also how do you workaround bug #2962? I don't know anymore! For a while, I used a private fork of std.json with the floating point functionality removed and a utf related bug worked around, but now that fork is completely commented out and I just use the stock std.json. Problem is I don't remember if it's because the bugs got fixed upstream, or if they just didn't bother me anymore... Regardless though, it works in a test on my box at least. Paste that code into a fresh file. void main() { auto v = jsonToVariant("4.2"); writeln(v.get!real); } compiles and runs correctly.
Re: Any (working) JSON library for D2?
Adam D. Ruppe wrote: >Oh, wait a minute, you were doing from json, not to json. > >Try this on for size. It converts from a std.json.JSONValue >to a std.variant.Varaint, which is quite a bit simpler to use. > >== > >import std.variant; >import std.json; > >Variant jsonToVariant(string json) { > auto decoded = parseJSON(json); > return jsonValueToVariant(decoded); >} > >Variant jsonValueToVariant(JSONValue v) { > Variant ret; > > final switch(v.type) { > case JSON_TYPE.STRING: > ret = v.str; > break; > case JSON_TYPE.INTEGER: > ret = v.integer; > break; > case JSON_TYPE.FLOAT: > ret = v.floating; > break; > case JSON_TYPE.OBJECT: > Variant[string] obj; > foreach(k, val; v.object) { > obj[k] = jsonValueToVariant(val); > } > > ret = obj; > break; > case JSON_TYPE.ARRAY: > Variant[] arr; > foreach(i; v.array) { > arr ~= jsonValueToVariant(i); > } > > ret = arr; > break; > case JSON_TYPE.TRUE: > ret = true; > break; > case JSON_TYPE.FALSE: > ret = false; > break; > case JSON_TYPE.NULL: > ret = null; > break; > } > > return ret; >} > >== Thanks, that looks great! Can't test it right now, but it seems this even works recursively? Awesome! -- Johannes Pfau
Re: Any (working) JSON library for D2?
Adam D. Ruppe wrote: >I use std.json with a couple helper function to make it shorter. > >To use: > >writeln(toJson(whatever)); > >or > >JSONValue val = toJsonValue(whatever); > >Works on most basic data types: int, string, array, assoc, struct, >etc. > >= > >import std.json; >import std.traits; >import std.conv; > >string toJson(T)(T a) { > auto v = toJsonValue(a); > return toJSON(&v); >} > >JSONValue toJsonValue(T)(T a) { > JSONValue val; > static if(is(T == JSONValue)) { > val = a; > } else static if(__traits(compiles, val = a.makeJsonValue())) { > val = a.makeJsonValue(); > } else static if(isIntegral!(T)) { > val.type = JSON_TYPE.INTEGER; > val.integer = to!long(a); > } else static if(isFloatingPoint!(T)) { > val.type = JSON_TYPE.FLOAT; > val.floating = to!real(a); > static assert(0); > } else static if(is(T == void*)) { > val.type = JSON_TYPE.NULL; > } else static if(is(T == bool)) { > if(a == true) > val.type = JSON_TYPE.TRUE; > if(a == false) > val.type = JSON_TYPE.FALSE; > } else static if(isSomeString!(T)) { > val.type = JSON_TYPE.STRING; > val.str = to!string(a); > } else static if(isAssociativeArray!(T)) { > val.type = JSON_TYPE.OBJECT; > foreach(k, v; a) { > val.object[to!string(k)] = toJsonValue(v); > } > } else static if(isArray!(T)) { > val.type = JSON_TYPE.ARRAY; > val.array.length = a.length; > foreach(i, v; a) { > val.array[i] = toJsonValue(v); > } > } else static if(is(T == struct)) { > val.type = JSON_TYPE.OBJECT; > > foreach(i, member; a.tupleof) { > string name = a.tupleof[i].stringof[2..$]; > static if(a.tupleof[i].stringof[2] != '_') > val.object[name] = toJsonValue(member); > } > } else { /* our catch all is to just do strings */ > val.type = JSON_TYPE.STRING; > val.str = to!string(a); > } > > return val; >} > > I guess you do not have similar helper functions to parse JSON? Also how do you workaround bug #2962? Maybe it doesn't occur as long as only formatting functionality is used, but any call to parseJSON triggers #2962. The bug is caused by std.conv.parse!real(string) but I'm not sure how that could be worked around. Maybe I'll have to remove floating point support and write some wrappers as you suggested, that could work. -- Johannes Pfau
Re: Any (working) JSON library for D2?
Oh, wait a minute, you were doing from json, not to json. Try this on for size. It converts from a std.json.JSONValue to a std.variant.Varaint, which is quite a bit simpler to use. == import std.variant; import std.json; Variant jsonToVariant(string json) { auto decoded = parseJSON(json); return jsonValueToVariant(decoded); } Variant jsonValueToVariant(JSONValue v) { Variant ret; final switch(v.type) { case JSON_TYPE.STRING: ret = v.str; break; case JSON_TYPE.INTEGER: ret = v.integer; break; case JSON_TYPE.FLOAT: ret = v.floating; break; case JSON_TYPE.OBJECT: Variant[string] obj; foreach(k, val; v.object) { obj[k] = jsonValueToVariant(val); } ret = obj; break; case JSON_TYPE.ARRAY: Variant[] arr; foreach(i; v.array) { arr ~= jsonValueToVariant(i); } ret = arr; break; case JSON_TYPE.TRUE: ret = true; break; case JSON_TYPE.FALSE: ret = false; break; case JSON_TYPE.NULL: ret = null; break; } return ret; } ==
Re: Any (working) JSON library for D2?
I use std.json with a couple helper function to make it shorter. To use: writeln(toJson(whatever)); or JSONValue val = toJsonValue(whatever); Works on most basic data types: int, string, array, assoc, struct, etc. = import std.json; import std.traits; import std.conv; string toJson(T)(T a) { auto v = toJsonValue(a); return toJSON(&v); } JSONValue toJsonValue(T)(T a) { JSONValue val; static if(is(T == JSONValue)) { val = a; } else static if(__traits(compiles, val = a.makeJsonValue())) { val = a.makeJsonValue(); } else static if(isIntegral!(T)) { val.type = JSON_TYPE.INTEGER; val.integer = to!long(a); } else static if(isFloatingPoint!(T)) { val.type = JSON_TYPE.FLOAT; val.floating = to!real(a); static assert(0); } else static if(is(T == void*)) { val.type = JSON_TYPE.NULL; } else static if(is(T == bool)) { if(a == true) val.type = JSON_TYPE.TRUE; if(a == false) val.type = JSON_TYPE.FALSE; } else static if(isSomeString!(T)) { val.type = JSON_TYPE.STRING; val.str = to!string(a); } else static if(isAssociativeArray!(T)) { val.type = JSON_TYPE.OBJECT; foreach(k, v; a) { val.object[to!string(k)] = toJsonValue(v); } } else static if(isArray!(T)) { val.type = JSON_TYPE.ARRAY; val.array.length = a.length; foreach(i, v; a) { val.array[i] = toJsonValue(v); } } else static if(is(T == struct)) { val.type = JSON_TYPE.OBJECT; foreach(i, member; a.tupleof) { string name = a.tupleof[i].stringof[2..$]; static if(a.tupleof[i].stringof[2] != '_') val.object[name] = toJsonValue(member); } } else { /* our catch all is to just do strings */ val.type = JSON_TYPE.STRING; val.str = to!string(a); } return val; }
Any (working) JSON library for D2?
std.json doesn't work at all because of bug #2962 . I tried to remove the problematic part from std.json and got it to compile (by disabling support for floating point numbers...) but using it correctly creates very verbose code: -- JSONValue a; if(a.type == JSONTYPE.OBJECT) if("member" in a) if(a["member"].type == TYPE.NUMBER) uint count = a["member"].number; -- (pseudo-code, but that's the basic workflow when using std.json) I know there also was a std.json replacement proposed by Robert Jacques but it requires significant patches to phobos, a std.variant replacement and the patches are against phobos 2.050 or something like that, so those could be out of date. To cut a long story short: does anyone know of another JSON library for D2? -- Johannes Pfau
Re: problem with array of delegates!
Seems like gmail likes to cut my code. If that didn't paste well here: http://codepad.org/cyEDHSGc
Re: problem with array of delegates!
Remove takes an offset, not a value as far as I know. If you need fast lookup and removal you could use hashes instead: int main(string[] argv) { auto a = new A; SlotDelegate x = &a.DIT; bool[SlotDelegate] _slotDg; _slotDg.remove(x); return 0; }
problem with array of delegates!
the following code seem problematic to compile... import std.algorithm; private alias void delegate(int, int) SlotDelegate; class A { void DIT(int a, int b) { } } int main(string[] argv) { A a; SlotDelegate x= &a.DIT; SlotDelegate[] _slotDg; _slotDg.remove(x); return 0; } I have some strange error: Error: incompatible types for ((pos) < (from)): 'uint' and 'void delegate(int, int)' C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d5609 I try a method like that as well (but similar problem! :( ) void removeAt(T)(ref T[] array, int index) { if(index < 0 || index >= array.length) return; const T[] empty = null; array.replaceInPlace(index, index + 1, empty); }
Re: struct as dictionary key
Never mind, just found it! http://www.digitalmars.com/d/2.0/hash-map.html
struct as dictionary key
I am creating a struct that I want to use as dictionary (associative array) key Initially it was a class and I overrode toHash and opCmp. I just made it a struct to be more lightweight on the system, but can't "override" toHash anymore! How can I make it a good dictionary key? Here is the struct struct PropertyId { TypeInfo declaringType; /// ditto string propertyName; /// ditto }