Re: help me learn to read documentation
On Friday, 2 October 2015 at 01:20:50 UTC, Adam D. Ruppe wrote: On Thursday, 1 October 2015 at 19:15:39 UTC, Robin wrote: [...] Those describe simple class members, so you can set them through assignment: Text CurrentFps = new Text(dejavu); // change to white on black CurrentFps.foreground = Color4b.White; CurrentFps.background = Color4b.Black; // change mode CurrentFps.mode = Font.Mode.Shaded; I haven't actually used this library, but since the doc describes it with variable syntax (`Color4b foreground;`) that means you should be able to just assign to them like ordinary object member variables. Wow, thank you so much. I have never seen code like that before but the documentation there made me scratch my head. Thank you.
help me learn to read documentation
Hi. I like to learn programming by examples but I need help learning how to read documentation. I have some idea of how it works in some aspects but in others i get completely stuck because there are no examples or code snippets. I am using dgame (dgame-dev.de) and im reading the documentation for changing the background text of my font in my program. I have the basic code for font creation but i need to make changes to the colors. import std.stdio; import std.system; import Dgame.System; import Dgame.Window; import Dgame.Graphic; import Dgame.Audio; import Dgame.Math; void main() { Font dejavu = Font("resources/dejavu.ttf", 12); Text CurrentFps = new Text(dejavu); ..window loop } The documentation here (http://dgame-dev.de/index.php?controller=learn&mode=package&package=graphic&module=Text&version=0.6)... gives me the Text() class but i dont know how to use "foreground, background, and Font mode" or at least turn it into usable syntax.
Re: Rational of some DMD decisions
Thank you for your reply. I didn't know that it isn't possible to overload the post-inc and decrement operators in Dlang but I think I don't even miss this feature if pre-inc/dec is still available. =)
Rational of some DMD decisions
Hiho, as I am currently very insterested in compiler implementation I often look into the DMD github and look how things are done there. Often I find myself curious about things at first but find out the rational behind certain implementation decisions. One thing I can't figure out is the different approach with pre- and postfix expressions. While prefix expressions inherit from UnaExp (unary expression) which makes sense to me as it is an expression with only one parameter (in general) the postfix inc and decrement expressions are inherited from BinExpr (binary expression) which define their second expression to be an integer expression with a value of 1. So I am very curious about the rational behind this design decision. Why do Postfix expression not inherit from UnaExp, too? Am I missing something very important? Thanks in advance! =) I hope this is the right place to ask questions about the DMD compiler. Regards, Robin
Re: Code doesn't work - why?
This is actually a good question as this code isn't really complex or doesn't require the best possible performance. But in case I will ever need optimum performance I should have learned how to handle tasks with value types which is the main reason why I chose them instead of reference types - for learning purposes. - can't hurt! ;) Regards, Rob
Re: Code doesn't work - why?
Here is the fully working code for everyone experiencing similar bugs or problems with pointers and value types. =) struct DeterministicState { public: this(string name, bool isFinal, DeterministicState *[char] transits...) { this.name = name; this.finalState = isFinal; this.addTransits(transits); } this(string name, bool isFinal) { this.name = name; this.finalState = isFinal; } this(bool isFinal, DeterministicState *[char] transits...) { this("", isFinal, transits); } this(DeterministicState *[char] transits...) { this("", false, transits); } void addTransits(DeterministicState *[char] newTransits) { foreach (immutable key; newTransits.keys) { transits[key] = newTransits[key]; } } string getName() const { return name; } bool isFinalState() const { return finalState; } bool hasNext(char input) const { return (input in transits) ? true : false; } DeterministicState * getNext(char input) { return transits[input]; } string toString() const { return name; } private: string name; DeterministicState *[char] transits; bool finalState; } struct DeterministicFiniteAutomaton { public: DeterministicState *[] input(char[] input) { DeterministicState *[] trace = [ start ]; auto currentState = trace[0]; foreach (immutable c; input) { if (!currentState.hasNext(c)) { writeln(currentState.toString() ~ " has no next for " ~ to!string(c)); break; } else { writeln(currentState.toString() ~ " has next for " ~ to!string(c)); } currentState = currentState.getNext(c); trace ~= currentState; } return trace; } this(DeterministicState * start) { this.start = start; } private: DeterministicState * start; } void main() { auto s0 = DeterministicState("s0", false); auto s1 = DeterministicState("s1", false); auto s2 = DeterministicState("s2", true); s0.addTransits(['0' : & s1, '1' : & s2]); s1.addTransits(['0' : & s0, '1' : & s2]); s2.addTransits(['0' : & s2, '1' : & s2]); auto dfa = DeterministicFiniteAutomaton(& s0); auto trace = dfa.input("0001".dup); foreach (t; trace) { writeln(t.toString()); } writeln("Trace Length = " ~ to!string(trace.length)); } Regards, Rob
Re: Code doesn't work - why?
Hiho, thank you for your response! You just showed me my flaws while programming with value types. I think the only close solution is to work with pointers to the created states within the associative array instead of direct value types. Thanks for clearing this up to me. =) Regards, Rob
Re: Code doesn't work - why?
Hiho, thank you for your response on my topic. However, I still do not understand why it didn't work for struct value types since I do not perform any mutations on the state objects during execution of the code. The only thing happening with them is that they are getting copied bitwise and thus should have the same entries in the associative array as the original source, or am I wrong with this? When value types are copied bitwise then the associative array should also be copied that way or at least point to the same mapping as the source and thus shouldn't be empty after copying. What changes are required in order to make it work with struct value types as well? I even tried to change getNext to work with pointer return values instead but that did not help either. Regards, Rob
Code doesn't work - why?
Hello, I came back to D after a longer break and just wanted to set up a small project to further get in contact with this language. However, it seems that the codes which simply shall simulate a deterministic finit automaton do not work correctly. CODE --- struct DeterministicState { public: this(string name, bool isFinal, DeterministicState[char] transits...) { this.name = name; this.finalState = isFinal; this.addTransits(transits); } this(string name, bool isFinal) { this.name = name; this.finalState = isFinal; } this(bool isFinal, DeterministicState[char] transits...) { this("", isFinal, transits); } this(DeterministicState[char] transits...) { this("", false, transits); } void addTransits(DeterministicState[char] newTransits) { foreach (immutable key; newTransits.keys) { transits[key] = newTransits[key]; } } string getName() const { return name; } bool isFinalState() const { return finalState; } bool hasNext(char input) const { return (input in transits) ? true : false; } DeterministicState getNext(char input) { return transits[input]; } string toString() const { return name; } private: string name; DeterministicState[char] transits; bool finalState; } struct DeterministicFiniteAutomaton { public: DeterministicState[] input(char[] input) { DeterministicState[] trace = [ start ]; auto currentState = trace[0]; foreach (immutable c; input) { if (currentState.hasNext(c) == false) { writeln(currentState.toString() ~ " has next for " ~ to!string(c)); break; } else { writeln(currentState.toString() ~ " has NO next for " ~ to!string(c)); } currentState = currentState.getNext(c); trace ~= currentState; } return trace; } this(DeterministicState start) { this.start = start; } private: DeterministicState start; } void main() { auto s0 = DeterministicState("s0", false); auto s1 = DeterministicState("s1", false); auto s2 = DeterministicState("s2", true); s0.addTransits(['0' : s1, '1' : s2]); s1.addTransits(['0' : s0, '1' : s2]); s2.addTransits(['0' : s2, '1' : s2]); auto dfa = DeterministicFiniteAutomaton(s0); auto trace = dfa.input("0001".dup); foreach (t; trace) { writeln(t.toString()); } writeln("Trace Length = " ~ to!string(trace.length)); } --- The output is the following: s0 has NO next for 0 s1 has next for 0 s0 s1 Trace Length = 2 Which states that the trace for input "0001" has just a length of 2 instead of 4. And I do not really understand why s1 has no next item while it was defined in main. I hope someone can clear things up for me. I really don't get why this isn't working as intended. Regards, Rob