Re: toString() through interface

2014-04-19 Thread bearophile via Digitalmars-d-learn
David Held: class Baz { override string toString() pure const { return cast(Object)(foo).toString(); } Foo foo; } This really makes the compiler go bonkers: src\Bug.d(11): Error: pure nested function 'toString' cannot access mutable data 'foo' src\Bug.d(11): Error: pure nested fu

Re: arrays in DMD V2

2014-04-19 Thread Ali Çehreli via Digitalmars-d-learn
On 04/19/2014 11:59 AM, steven kladitis wrote: > I am getting a much clearer picture of arrays Great! :) > a D manual or book just for D V2.0. They are all here: http://wiki.dlang.org/Books Ali

Re: toString() through interface

2014-04-19 Thread David Held via Digitalmars-d-learn
On 4/19/2014 5:45 PM, Adam D. Ruppe wrote: On Sunday, 20 April 2014 at 00:35:30 UTC, David Held wrote: Since all implementations of an interface must derive from Object That's not true. They can also come from IUnknown or a C++ interface. Ok, that's a good reason! cast(Object)(foo).toS

Re: Template method and type resolution of return type

2014-04-19 Thread David Held via Digitalmars-d-learn
On 4/19/2014 3:31 PM, Andrej Mitrovic via Digitalmars-d-learn wrote: [...] struct S { int get() { return 0; } T get(T)() { return T.init; } } void main() { S s; float x = s.get(); // which overload? (currently int get()) } Isn't this just because concrete methods are bett

Re: toString() through interface

2014-04-19 Thread David Held via Digitalmars-d-learn
On 4/19/2014 5:35 PM, David Held wrote: interface Foo { } class Bar : Foo { override string toString() pure const { return "Bar"; } } void main() { Foo foo = new Bar; foo.toString(); } To make things more interesting, consider the call to toString() from inside a class (which

Re: toString() through interface

2014-04-19 Thread Adam D. Ruppe via Digitalmars-d-learn
On Sunday, 20 April 2014 at 00:35:30 UTC, David Held wrote: Since all implementations of an interface must derive from Object That's not true. They can also come from IUnknown or a C++ interface. cast(Object)(foo).toString(); (cast(Object)foo).toString() might work This might return

toString() through interface

2014-04-19 Thread David Held via Digitalmars-d-learn
interface Foo { } class Bar : Foo { override string toString() pure const { return "Bar"; } } void main() { Foo foo = new Bar; foo.toString(); } src\Bug.d(14): Error: no property 'toString' for type 'Bug.Foo' Since all implementations of an interface must derive from Object, why c

Re: Template method and type resolution of return type

2014-04-19 Thread Andrej Mitrovic via Digitalmars-d-learn
On 4/19/14, matovitch via Digitalmars-d-learn wrote: > This won't compile : > > import std.stdio; > > void main() > { > Test t; > t.data = [152, 32, 64, 28, 95]; > float b = t.get; > writefln("%s", b); > } Because it's probably overkill. At some point it becomes too much m

Re: Reducing an array

2014-04-19 Thread MattCoder via Digitalmars-d-learn
On Saturday, 19 April 2014 at 17:12:10 UTC, Ali Çehreli wrote: On 04/19/2014 09:55 AM, MattCoder wrote: > On Friday, 18 April 2014 at 20:02:41 UTC, Tim Holzschuh via > Digitalmars-d-learn wrote: > void main(){ > int myfilter(int a){ > static int[] b; That static variable makes thi

Re: arrays in DMD V2

2014-04-19 Thread steven kladitis via Digitalmars-d-learn
Fantastic I am getting a much clearer picture of arrays I appreciate all of the help I see many examples of D online, but most do not compile under 2.065. I am wondering if there is a D manual or book just for D V2.0.

Re: Template method and type resolution of return type

2014-04-19 Thread matovitch via Digitalmars-d-learn
On Saturday, 19 April 2014 at 18:46:28 UTC, matovitch wrote: On Saturday, 19 April 2014 at 17:49:54 UTC, bearophile wrote: matovitch: struct Test { immutable (ubyte)[] data; T get(T)() { return *(cast(T*)(&(data[0]))); } It's better to return

Re: Template method and type resolution of return type

2014-04-19 Thread matovitch via Digitalmars-d-learn
On Saturday, 19 April 2014 at 17:49:54 UTC, bearophile wrote: matovitch: struct Test { immutable (ubyte)[] data; T get(T)() { return *(cast(T*)(&(data[0]))); } It's better to return const/immutable data. Otherwise the program gives undefined r

Re: Template method and type resolution of return type

2014-04-19 Thread matovitch via Digitalmars-d-learn
On Saturday, 19 April 2014 at 18:46:28 UTC, matovitch wrote: On Saturday, 19 April 2014 at 17:49:54 UTC, bearophile wrote: matovitch: I did'nt know that one. This last sentence is misleading, let be clear : I know almost nothing when it comes to D...but I'm willing to learn ! ;-)

Re: Template method and type resolution of return type

2014-04-19 Thread bearophile via Digitalmars-d-learn
matovitch: struct Test { immutable (ubyte)[] data; T get(T)() { return *(cast(T*)(&(data[0]))); } It's better to return const/immutable data. Otherwise the program gives undefined results. In alternative use mutable ubytes for data. Also &da

Re: Reducing an array

2014-04-19 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 18 April 2014 at 22:11:17 UTC, bearophile wrote: This preserves ordering and it's in-place. Not tested much: void main() { import std.stdio, std.traits; auto data = [2, 1, 1, 3, 2, 3]; bool[ForeachType!(typeof(data))] seen; size_t pos = 0; foreach (immutable i; 0

Template method and type resolution of return type

2014-04-19 Thread matovitch via Digitalmars-d-learn
Hi everyone ! Let's say I have a struct : struct Test { immutable (ubyte)[] data; T get(T)() { return *(cast(T*)(&(data[0]))); } } This code will work : import std.stdio; void main() { Test t; t.data = [152, 32, 64, 28, 95];

Re: Reducing an array

2014-04-19 Thread Ali Çehreli via Digitalmars-d-learn
On 04/19/2014 09:55 AM, MattCoder wrote: > On Friday, 18 April 2014 at 20:02:41 UTC, Tim Holzschuh via > Digitalmars-d-learn wrote: > void main(){ > int myfilter(int a){ > static int[] b; That static variable makes this solution non-reentrant. To see an effect of this, replace the

Re: arrays in DMD V2

2014-04-19 Thread Ali Çehreli via Digitalmars-d-learn
On 04/19/2014 09:14 AM, steven kladitis wrote: > // does not work I think you are thinking in terms of C's syntax. Unlike C, array definition syntax is consistent in D. I inserted spaces below to make it stand out: int[] [string] arr; The definition above is in the following form:

Re: Reducing an array

2014-04-19 Thread MattCoder via Digitalmars-d-learn
On Friday, 18 April 2014 at 20:02:41 UTC, Tim Holzschuh via Digitalmars-d-learn wrote: Hi there, I try to remove all equal elements of an array, thus [2,2] --> [2]. I thought this maybe would be possible with std.algorithm.reduce, but at least the way I tried it doesn't work: arr.reduce(

Re: arrays in DMD V2

2014-04-19 Thread Jesse Phillips via Digitalmars-d-learn
On Saturday, 19 April 2014 at 16:14:45 UTC, steven kladitis wrote: void main() { //inta0[]; int[] a0; //inta1[][]; int[][] a1; //string a2[][]; string[][] a2; //string a3[][string]; string[string] a3; // string[][string] a3; // possibly should be above for a3 //string a4[][string][strin

Re: arrays in DMD V2

2014-04-19 Thread steven kladitis via Digitalmars-d-learn
void main() { //inta0[]; int[] a0; //inta1[][]; int[][] a1; //string a2[][]; string[][] a2; //string a3[][string]; string[string] a3; // string[][string] a3; // possibly should be above for a3 //string a4[][string][string]; string[][string][string] a4; //string a4[string][string][string]

Re: Get and set terminal size

2014-04-19 Thread Mike Parker via Digitalmars-d-learn
On 4/19/2014 9:06 PM, FreeSlave wrote: Note that you don't need to apply toStringz to string literals since they implicitly cast to const char*. And, more importantly, are nul terminated.

Re: Struct size

2014-04-19 Thread Lars T. Kyllingstad via Digitalmars-d-learn
On Saturday, 19 April 2014 at 12:26:16 UTC, Andrej Mitrovic via Digitalmars-d-learn wrote: On 4/19/14, Lars T. Kyllingstad via Digitalmars-d-learn wrote: Say I have two structs, defined like this: struct A { /* could contain whatever */ } struct B { A a; } My question is, is it now

Re: Reducing an array

2014-04-19 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 18 April 2014 at 22:11:17 UTC, bearophile wrote: monarch_dodra: Out of curiosity, if the requirement was to *also* preserve ordering (eg: remove all non-first elements), how would you go at it? [2, 1, 1, 3, 2, 3] => [2, 1, 3]; Maybe std.algorithm's `makeIndex` would help here? B

Re: Struct size

2014-04-19 Thread Andrej Mitrovic via Digitalmars-d-learn
On 4/19/14, Lars T. Kyllingstad via Digitalmars-d-learn wrote: > Say I have two structs, defined like this: > > struct A { /* could contain whatever */ } > > struct B { A a; } > > My question is, is it now guaranteed that A.sizeof==B.sizeof? The best thing to do is add a static assert a

Struct size

2014-04-19 Thread Lars T. Kyllingstad via Digitalmars-d-learn
Say I have two structs, defined like this: struct A { /* could contain whatever */ } struct B { A a; } My question is, is it now guaranteed that A.sizeof==B.sizeof, regardless of how A is defined (member variable types, alignment, etc.)? More to the point, say I have a function foo()

Re: Get and set terminal size

2014-04-19 Thread FreeSlave via Digitalmars-d-learn
I use ldc2 main.d -L-lcurses or dmd main.d -L-lcurses and following source code: import std.stdio; extern(C) int tgetnum(const(char) *id); int main() { writeln(tgetnum("li")); return 0; } Note that you don't need to apply toStringz to string literals since they implicitly cast to con

Re: Get and set terminal size

2014-04-19 Thread FreeSlave via Digitalmars-d-learn
What are compiler and platform do you use? Probably you are trying to link with 64-bit library while being on 32-bit OS (or vice versa) It works fine on my 32-bit Debian with ldc2 and dmd.

Re: Get and set terminal size

2014-04-19 Thread Denis Mezhov via Digitalmars-d-learn
On Saturday, 19 April 2014 at 10:39:43 UTC, Adam D. Ruppe wrote: Blargh, I don't know teh ldc switch for it :( Try adding pragma(lib, "curses"); (or ncurses) to your file with main in it, i think ldc supports that. pragma(lib, "curses"); extern(C): int tgetnum(const(char) *capname); hmm

Re: Get and set terminal size

2014-04-19 Thread Adam D. Ruppe via Digitalmars-d-learn
Blargh, I don't know teh ldc switch for it :( Try adding pragma(lib, "curses"); (or ncurses) to your file with main in it, i think ldc supports that.

Re: *** GMX Spamverdacht *** Re: Reducing an array

2014-04-19 Thread Tim Holzschuh via Digitalmars-d-learn
Am 18.04.2014 22:27, schrieb Steven Schveighoffer via Digitalmars-d-learn: arr.sort(); arr = arr.uniq.array(); -Steve Thanks, also for the explanation! - Tim

Re: Get and set terminal size

2014-04-19 Thread Denis Mezhov via Digitalmars-d-learn
On Saturday, 19 April 2014 at 09:59:39 UTC, Adam D. Ruppe wrote: Try adding -lncurses or -lcurses to the command line. tgetnum is found in the curses library which isn't linked in automatically by default. Error: unrecognized switch '-lcurses' Error: unrecognized switch '-lncurses' :((

Re: Get and set terminal size

2014-04-19 Thread Adam D. Ruppe via Digitalmars-d-learn
Try adding -lncurses or -lcurses to the command line. tgetnum is found in the curses library which isn't linked in automatically by default.

Get and set terminal size

2014-04-19 Thread Denis Mezhov via Digitalmars-d-learn
I want use tgetnum for get terminal size http://www.mkssoftware.com/docs/man3/curs_termcap.3.asp extern(C): int tgetnum(const(char) *capname); calling a method from main writeln(tgetnum(toStringz("li"))); I receive an error message Undefined symbols for architecture x86_64: "_tgetnum", refer