Re: Is there a way to set an alias to specific form of a template?
On 10/7/2011 7:33 PM, Andrej Mitrovic wrote: You don't have to rewrite Vector for multiple dimensions, methinks: class Vector(T...) { this(T t) {} } void main() { alias Vector!(float, float) vec2f; auto v = new vec2f(1.0,1.0); } You'll probably have to play with `static if`, template constraints, and stuff like that. I got this working, found an odd bug though. This code, alias Vector!(float, float) vec2f; alias Vector!(double, double) vec2d; alias Vector!(float, float, float) vec3f; public struct Vector(T...) { int dim = 0; this(T...)(T args) { dim = args.length; } unittest { auto v = new vec2f(1.2f, 1.5f); vec2f d = new vec2f(1.1f, 1.4f); assert(v.dim == 2); assert(d.dim == 2); } } will pass the first assert and fail on the second. Checking out the contents of v.length and d.length with writeln gives the correct answer on the first and 2 1 RANDOMHEXCODE on the second. Very strange.
Re: Is there a way to set an alias to specific form of a template?
On Fri, 07 Oct 2011 18:29:26 -0700, Roderick Gibson wrote: > This may be the completely wrong approach, but I am basically thinking > of something like this (I am aware this will not compile, it's > psuedocode): > > class Vector(T) { > ... //definition here > } > > alias Vector(float, float) vec2f; > auto v = new vec2f(1.0,1.0); > > I am making a templated Vector class (a mathematical vector) that will > have varying types (thus a template) and dimensions (via variadic > functions), so that the same template definition will work for 2d or 3d > vectors (or 4d, etc). > > I then want the programmer to be able to define > the specific forms that he wants so he can easily keep track of them > (without getting confused about which is a 2d integer vector and which > is a 3d float vector), and then use those forms in a type safe manner. > Is this even possible? If it is, but it's the wrong way to do it, what's > the right way? > > Basically I wanted to write it once and not worry about writing it again > to handle different types and dimensions (no vec2i class, or vec2f, or > vec3f, or vec3i, etc). Templates easily handles the type requirement, > but what about the dimensional requirement? Am I just going to have to > rewrite it when I add dimensions? You can take advantage of 'Template Value Parameters' and 'Typesafe Variadic Functions': http://www.d-programming-language.org/ template.html#TemplateValueParameter http://www.d-programming-language.org/function.html class Vector(T, int N) { T[N] elements; this(T[] elements ...) { this.elements = elements; } } alias Vector!(double, 2) Vec2D; alias Vector!(double, 3) Vec3D; void main() { auto v2d = new Vec2D(2.2, 2.2); auto v3d = new Vec3D(3.3, 3.3, 3.3); // Alternatively, all parameters at once: auto v3d_too = new Vec3D([ 33, 33, 33, ]); } (Some would find 'size_t N' to be more appropriate since N is a dimension.) Ali
Re: Simple I know, but could use some help compiling with make
"Ola Ost" wrote in message news:j6hqkh$lk0$1...@digitalmars.com... >I had exactly this problem too, I asked on the Derelict forums: > http://www.dsource.org/forums/viewtopic.php?t=5856&sid=8ebff671fafec3bd8962ddfceaf99eb8 > > At the moment I've resolved this by building Derelict with make, first a > normal full > build, then a second run using the cleandi target, which removes the > generated di files. > I've set up the Derelict lib and include paths in dmd's sc.ini file, so I > just have to > call 'rdmd main.d' to build and run. If someone can make a minimal test case for this issue (ideally no external dependencies), then I'll try my hand at fixing rdmd.
Re: Is there a way to set an alias to specific form of a template?
On 10/7/2011 7:37 PM, Andrej Mitrovic wrote: There's an LGPL-licensed Vector module here: https://github.com/Zardoz89/zmath/blob/master/src/vector.d Not mine, but it's interesting. Awesome, thanks for both answers. I figured there was already an implementation, this is mostly just to play with/learn templates :)
Re: Is there a way to set an alias to specific form of a template?
There's an LGPL-licensed Vector module here: https://github.com/Zardoz89/zmath/blob/master/src/vector.d Not mine, but it's interesting.
Re: Is there a way to set an alias to specific form of a template?
You don't have to rewrite Vector for multiple dimensions, methinks: class Vector(T...) { this(T t) {} } void main() { alias Vector!(float, float) vec2f; auto v = new vec2f(1.0,1.0); } You'll probably have to play with `static if`, template constraints, and stuff like that.
Is there a way to set an alias to specific form of a template?
This may be the completely wrong approach, but I am basically thinking of something like this (I am aware this will not compile, it's psuedocode): class Vector(T) { ... //definition here } alias Vector(float, float) vec2f; auto v = new vec2f(1.0,1.0); I am making a templated Vector class (a mathematical vector) that will have varying types (thus a template) and dimensions (via variadic functions), so that the same template definition will work for 2d or 3d vectors (or 4d, etc). I then want the programmer to be able to define the specific forms that he wants so he can easily keep track of them (without getting confused about which is a 2d integer vector and which is a 3d float vector), and then use those forms in a type safe manner. Is this even possible? If it is, but it's the wrong way to do it, what's the right way? Basically I wanted to write it once and not worry about writing it again to handle different types and dimensions (no vec2i class, or vec2f, or vec3f, or vec3i, etc). Templates easily handles the type requirement, but what about the dimensional requirement? Am I just going to have to rewrite it when I add dimensions?
Re: Simple I know, but could use some help compiling with make
On 10/5/2011 7:46 AM, Ola Ost wrote: I had exactly this problem too, I asked on the Derelict forums: http://www.dsource.org/forums/viewtopic.php?t=5856&sid=8ebff671fafec3bd8962ddfceaf99eb8 At the moment I've resolved this by building Derelict with make, first a normal full build, then a second run using the cleandi target, which removes the generated di files. I've set up the Derelict lib and include paths in dmd's sc.ini file, so I just have to call 'rdmd main.d' to build and run. Hmm, so it's a bug in rdmd trying to compile the .di files or something?
Order of base-class constructor calls
Is there any way to enforce the user to call the base-class ctor via super(), so it's the first statement in his class ctor? e.g.: class Base { this(int) { } } class Derived : Base { this(int x) { super(x); // user statements } } The problem I'm having is that Base does some required initialization in its ctor, and Derived shouldn't be allowed to call any Base class methods (well, virtual methods) before calling the Base ctor. This is somewhat mitigated if I have a default constructor in the base class, e.g.: class Base { this() { /* init section */ } this(int) { this(); } } class Derived : Base { this(int x) { // Base class ctor automatically called *before* any other statements // user statements.. } } But this is only partially safe as the user could still call super() or super(int) after he's made some calls of his own, e.g.: class Base { this() { /* init section */ } this(int) { this(); } void foo() { /* expects Base.this() having been already called */ } } class Derived : Base { this(int x) { foo(); // boom super(); // or super(int); } } So I'm looking for some techniques or tricks (or, dare I say, design patterns :x) you guys might have if you've ever ran into this kind of problem.
Re: contrary of std.utf.toUTFz!(const(wchar)*)
Just wanted to point out how it's implemented with language tools.
Re: contrary of std.utf.toUTFz!(const(wchar)*)
Trass3r , dans le message (digitalmars.D.learn:29978), a écrit : >> I feel a little stupid, but how to convert a wchar* zero terminated >> string into a wstring (DMD 2.055)? > > wstring w = cstr[0 .. strlenw(cstr)]; if cstr comes from c code, you cannot guarantee it is immutable. Moreover, cstr might point to 0.
Re: contrary of std.utf.toUTFz!(const(wchar)*)
I feel a little stupid, but how to convert a wchar* zero terminated string into a wstring (DMD 2.055)? wstring w = cstr[0 .. strlenw(cstr)];
Re: Get current date and time with std.datetime
On 2011-10-07 08:54, Jonathan M Davis wrote: On Thursday, October 06, 2011 23:31:26 Jonathan M Davis wrote: On Friday, October 07, 2011 08:23:10 Jacob Carlborg wrote: On 2011-10-07 08:15, Jonathan M Davis wrote: On Friday, October 07, 2011 19:08:33 Joel Christensen wrote: Hi, I have a program that uses the old time stuff before the module std.datetime. I have a DateTime object, but I can't seem to set its properties to the current time. Some thing like: DateTime dateTime; dateTime = getCurrentDateTime(); http://d-programming-language.org/intro-to-datetime.html May I suggest that you put an example on top of that article that gets the current date and time. It's already in the documentation at the top of the module. What I need to do is put a link to the article in that documetantion. I just added a link to the article to std.datetime, so it'll be there starting with the next release. What I'd _really_ like to see fixed though is the anchor-generation with ddoc so that I can actually properly organize the links at the top of std.datetime. - Jonathan M Davis That would be nice too. -- /Jacob Carlborg
Re: Stack Overflow error missing
Andrej Mitrovic: > I'm fairly sure this used to give me a stack overflow error: It's a regression: http://d.puremagic.com/issues/show_bug.cgi?id=6088 Bye, bearophile
Re: contrary of std.utf.toUTFz!(const(wchar)*)
Paolo Invernizzi , dans le message (digitalmars.D.learn:29974), a écrit : > Hi all, > > I feel a little stupid, but how to convert a wchar* zero terminated string > into a wstring (DMD 2.055)? > I'm digging into Phobos, but right now I've found no way... > > Thanks, Paolo. std.conv.to!wstring
contrary of std.utf.toUTFz!(const(wchar)*)
Hi all, I feel a little stupid, but how to convert a wchar* zero terminated string into a wstring (DMD 2.055)? I'm digging into Phobos, but right now I've found no way... Thanks, Paolo
polymorphic call from variant?
Hey, i had almost no problem in d-coding since i deferred creation of my messaging system. Although, i can't do that anymore - so, remembering what you told me, my expectations are much smaller now. This time i'd like to make 'basic' version, but STILL wasting hours on that o,o. OK, so one most important thing i'd really need to make it at least usable (rather than make Msg struct + statically check which message has been sent...) is: i got my client class like: class Listener : IMsgListener!MsgWindowRefresh { mixin InjectMsgProvider!MsgWindowRefresh _prvdrRefresh; void handle(MsgWindowRefresh msg) {} } 'interface' like: interface IMsgListener(MSG) { mixin template InjectMsgProvider(MSG) { public bool register(IMsgListener!MSG what) { return MSG.mediator.registerListener(typeid(MSG), Variant(what)); } } void handle(MSG); } So, basically i got everything - type, variant, fireworks :) The only thing that would make it all work is: void recvMsg(Variant msg) { foreach(ref l; _listeners[msg.type]) (l.get).handle(msg.get); } Unfortunately it's impossible because variant wants type as template argument. Which is a bit strange for me because it already have typeinfo BUT i suppose that author wouldn't make this template argument with no purpose... The thing i'm counting at: is there any slower :( solution to this? So, is there ANY way to call handle() methodS with concrete message rather than having to add: switch (variant.type) { case MsgA: case MsgB: case MsgC: } inside my Listener class? In other words, can i call MANY -> SINGLE -> MANY via dynamic pointer(vtable) rather than static branching? Thanks, Mariusz Gliwiński
Re: Get current date and time with std.datetime
http://d-programming-language.org/intro-to-datetime.html Thanks Jonathan, that helped I think, (haven't read it all, though). But I've got errors with some of the date times not being able to change them with int's values. task.d(44): Error: function std.datetime.DateTime.month () const is not callable using argument types (int) task.d(44): Error: cannot implicitly convert expression (month0) of type int to Month -JoelCNZ