Re: arrays in DMD V2

2014-04-19 Thread Ali Çehreli via Digitalmars-d-learn
On 04/18/2014 08:47 PM, steven kladitis wrote: Thanks, I am trying to understand what I am doing. The docs seem unclear to me on how to initialize these. I think there are three ways. with brackets , the other with foreach or direct assignments. -- here is a longer version -- uncomment out

Re: arrays in DMD V2

2014-04-19 Thread Jesse Phillips via Digitalmars-d-learn
On Saturday, 19 April 2014 at 03:51:02 UTC, steven kladitis wrote: import std.stdio; void main() { inta0[]; inta1[][]; string a2[][]; string a3[][string]; string a4[][string][string]; //string a4[string][string][string]; is this the same as above int

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,

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: *** 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 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: 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 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 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

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

Re: Struct size

2014-04-19 Thread Andrej Mitrovic via Digitalmars-d-learn
On 4/19/14, Lars T. Kyllingstad via Digitalmars-d-learn digitalmars-d-learn@puremagic.com 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

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?

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 digitalmars-d-learn@puremagic.com wrote: Say I have two structs, defined like this: struct A { /* could contain whatever */ } struct B {

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: 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

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

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: 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

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 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;

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

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

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: 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: 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 this

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 digitalmars-d-learn@puremagic.com 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

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

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

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: 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

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!

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 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