Deprecation

2019-01-18 Thread Ali via Digitalmars-d-learn
Hello. I am having an issue with the code below. the out put 
after compiling is this :


Deprecation: foreach: loop index implicitly converted from size_t 
to uint


the code is :

auto available = new int[cast(uint) max - min];
foreach (uint i, ref a; available)
a = min + i;

Any help would be highly appreciated.


Re: depreciated function delete

2019-01-18 Thread Ali via Digitalmars-d-learn
On Friday, 18 January 2019 at 21:37:38 UTC, Steven Schveighoffer 
wrote:

On 1/18/19 4:32 PM, Ali wrote:
On Friday, 18 January 2019 at 21:13:34 UTC, Steven 
Schveighoffer wrote:

On 1/18/19 3:48 PM, alik wrote:
Hi there. as you know delete function is depreciated. so I 
tried to use the __delete function for the code below:


if (this.parse_buffer.length > this.parse_size)
 {
 __delete(this.parse_buffer);
 this.parse_buffer.length = this.parse_size;
 }


but in return after I compile code I get the error like : 
Undefined identifier.
when I use destroy instead I don't get any errors. but I 
want to use __delete because of the garbage collector as it 
frees up the memory


thanks in advance.


import core.memory: __delete;



thank you for your quick answer. I did it for my own files but 
should I change something in here :


/root/.dub/packages/undead-1.0.9/undead/src/undead/regexp.d(370,17): 
Deprecation: The delete keyword has been deprecated.  Use object.destroy() (and 
core.memory.GC.free() if applicable) instead.



Yeah that should be updated to use __delete, feel free to 
submit a PR, or raise an issue.


-Steve


did it but now it shows me another deprecated functions:


 Deprecation: Symbol object.string is not visible from module 
math because it is privately imported in module string


 Deprecation: foreach: loop index implicitly converted from 
size_t to int


/root/.dub/packages/undead-1.0.9/undead/src/undead/socketstream.d(124,32): 
Deprecation: undead.socketstream.SocketStream.seek cannot be annotated with 
@disable because it is overriding a function in the base class


instead of @disable I dont have any idea what to use.


Re: depreciated function delete

2019-01-18 Thread Ali via Digitalmars-d-learn
On Friday, 18 January 2019 at 21:13:34 UTC, Steven Schveighoffer 
wrote:

On 1/18/19 3:48 PM, alik wrote:
Hi there. as you know delete function is depreciated. so I 
tried to use the __delete function for the code below:


if (this.parse_buffer.length > this.parse_size)
     {
     __delete(this.parse_buffer);
     this.parse_buffer.length = this.parse_size;
     }


but in return after I compile code I get the error like : 
Undefined identifier.
when I use destroy instead I don't get any errors. but I want 
to use __delete because of the garbage collector as it frees 
up the memory


thanks in advance.


import core.memory: __delete;

-Steve


thank you for your quick answer. I did it for my own files but 
should I change something in here :


/root/.dub/packages/undead-1.0.9/undead/src/undead/regexp.d(370,17): 
Deprecation: The delete keyword has been deprecated.  Use object.destroy() (and 
core.memory.GC.free() if applicable) instead.



Re: c2 classes

2018-04-06 Thread Ali via Digitalmars-d-learn

On Friday, 6 April 2018 at 14:31:49 UTC, Alex wrote:

On Friday, 6 April 2018 at 13:41:50 UTC, aerto wrote:

its possible to make this work ??

import std.stdio;


class UUsers
{
public:
int age;
}


class users
{
public:
int[int] uid;

}


void main() {

users newuser = new users();
newuser.uid[0].age = 23;
writeln(newuser.uid[0].age);

}


It depends on what you want to achieve...
This is runnable:
```
import std.stdio;


class UUsers
{
this(int val)
{
age = val;
}
public:
int age;
}


class users
{
public:
UUsers[int] uid;

}


void main() {

users newuser = new users();
newuser.uid[0] = new UUsers(23);
writeln(newuser.uid[0].age);

}
```


A question from me, since I am also still learning D
what is the difference between those following two declarations

UUsers[int] uid; UUsers[] uid;




Re: c2 classes

2018-04-06 Thread Ali via Digitalmars-d-learn

On Friday, 6 April 2018 at 13:41:50 UTC, aerto wrote:

its possible to make this work ??

import std.stdio;


class UUsers
{
public:
int age;
}


class users
{
public:
int[int] uid;

}


void main() {

users newuser = new users();
newuser.uid[0].age = 23;
writeln(newuser.uid[0].age);

}


This will work
import std.stdio;

class UUsers
{
public:
int age;

}


class users
{
public:
UUsers[] uid;

}


void main() {

users userList = new users();

userList.uid ~=  new UUsers();

userList.uid[0].age = 24 ;

writeln(userList.uid[0].age);
}

Let me try to explain why
Basically, what you wanted to do is have two classes
one will hold user's information, and another holding a user list

For the second class to hold user list
this line

int[int] uid;


became this line

UUsers[] uid;


In you code, there was no relation between the two classes
Then in your main

You instantiate your users list object

users userList = new users();


Then you instantiate a user object and append it to the list (~= 
is the append operator)



userList.uid ~=  new UUsers();


Finally you assign a value to your user object age and print it

userList.uid[0].age = 24 ;
writeln(userList.uid[0].age);


Re: Declare and Define Before Use? [rant]

2018-04-04 Thread Ali via Digitalmars-d-learn

On Wednesday, 4 April 2018 at 19:51:27 UTC, kdevel wrote:

On Wednesday, 4 April 2018 at 19:19:30 UTC, Ali wrote:
BTW: You can't write

   void main ()
   {
  x.writeln;
  int x;
   }
   import std.stdio;



This is because x is not module scope
you can do this

   void main ()
   {
  x.writeln;

   }

   import std.stdio;
   int x;



Re: Declare and Define Before Use? [rant]

2018-04-04 Thread Ali via Digitalmars-d-learn

On Wednesday, 4 April 2018 at 18:57:27 UTC, kdevel wrote:

Why are people writing

   import std.stdio;

   void main ()
   {



   }

   struct S {



   }

but not

   void main ()
   {



   }

   struct S {



   }

   import std.stdio;

?



Personally i found it weird and inconsistent that
you can import and define anywhere at module scope
but for any other scope you have to define and import before use

I think the rules should have been the same everywhere
and if there was an exception to be made, it could be made for 
the main function

since the main function is special anyway

Personally I would either do

import



declare and define



main


or

main



import



declare and define


the later, is because main is special, so it is as if i am saying
this is what i want to do (main) and to do it (import) this stuff 
and (declare and define this stuff) ... putting main first would 
be just to highlight it and attract attention to it


importing and declaring anywhere at module scope is just too 
random for my taste





Re: Learning D - modules packages and the package.d

2018-04-03 Thread Ali via Digitalmars-d-learn

On Wednesday, 4 April 2018 at 04:54:50 UTC, Ali wrote:
at first i though package.d is special name, as in i must call 
the file package.d or this trick or idiom to work


the trick was to have one module that public import all the 
modules you import as group in other modules


so instead of importing a dozen or so modules, you only import 
one module that imports everything you need


Learning D - modules packages and the package.d

2018-04-03 Thread Ali via Digitalmars-d-learn

I am going through the Learning D book by Michael Parker
So every now and then I will make post about the book
either critics of the book, book content or questions


First critic
chapter 2 - the special package module

this small section, suggest an idiom to create a package which 
can have any name

the book suggest somepack
and inside it add a module package.d , so you will end up 
somepack/package.d

inside the file add


module somepack; //notice this named after the folder
//then public import std.stdio, somepack.one, somepack.two; 
//etc


at first i though package.d is special name, as in i must call 
the file package.d or this trick or idiom to work
also it felt weird that the module name, is named after the 
folder name, which previously was referred to as the package name


anyway, i started playing with this, and renaming everything
and my conclusion is

i not really sure, if D really support or have packages
as in D is aware that some modules are together in a package

it seems to me at this time D only support modules
modules can have names with . in them
you can use the . in module names to make it seem as if its 
packages



import io = long.module.name


also seem as a trick that complements having modules with long 
names with dots in them


and it seems you really need to match file and module names

my critic of the chapter 2 so far, the book doesn't really help 
clarify this situation
i am relieved though to learn that D doesnt treat special 
file/module names as special


Re: Optional parameters?

2018-04-01 Thread Ali via Digitalmars-d-learn
On Sunday, 1 April 2018 at 15:54:16 UTC, Steven Schveighoffer 
wrote:
I currently have a situation where I want to have a function 
that accepts a parameter optionally.


why not simply use function overloading?




Re: The first example in the Learning D book, wont compile

2018-03-25 Thread Ali via Digitalmars-d-learn

On Sunday, 25 March 2018 at 20:52:29 UTC, Ali wrote:

On Sunday, 25 March 2018 at 20:45:58 UTC, Ali wrote:
I now see my typo, should be retro, not range


We need better IDEs, this would have been easily highlighted by a 
good ide


Re: The first example in the Learning D book, wont compile

2018-03-25 Thread Ali via Digitalmars-d-learn

On Sunday, 25 March 2018 at 20:45:58 UTC, Ali wrote:

Hi

The first example in the Learning D book

import core.thread;
import std.stdio;

void main() {
import std.range: iota, range;
write("Greeting in, ");
foreach(num; iota(1, 4).range) {
writef("%s...", num);
stdout.flush();
Thread.sleep(1.seconds);
}
writeln();
writeln("Hello, World");
}


wont compile and give this error

hello.d(5): Error: module `std.range` import range not found

this should be an easy issue to fix, except that googling this 
error doesnt return anything useful



I now see my typo, should be retro, not range




The first example in the Learning D book, wont compile

2018-03-25 Thread Ali via Digitalmars-d-learn

Hi

The first example in the Learning D book

import core.thread;
import std.stdio;

void main() {
import std.range: iota, range;
write("Greeting in, ");
foreach(num; iota(1, 4).range) {
writef("%s...", num);
stdout.flush();
Thread.sleep(1.seconds);
}
writeln();
writeln("Hello, World");
}


wont compile and give this error

hello.d(5): Error: module `std.range` import range not found

this should be an easy issue to fix, except that googling this 
error doesnt return anything useful


Re: dmd download sig file, how do I use it

2018-03-25 Thread Ali via Digitalmars-d-learn

On Sunday, 25 March 2018 at 04:01:28 UTC, Seb wrote:
gpg --verify --keyring ~/dlang/d-keyring.gpg 
--no-default-keyring dmd.2.079.0.linux.tar.xz.sig 
dmd.2.079.0.linux.tar.xz


Thanks, I guess this kinda works
I am now getting

 gpg: Signature made Fri 02 Mar 2018 01:47:57 PM EST
 gpg:using RSA key B273811612BB1939
 gpg: Good signature from "Martin Nowak 
" [expired]
 gpg: aka "Martin Nowak (dawg) " 
[expired]

 gpg: aka "Martin Nowak " [expired]
 gpg: Note: This key has expired!
 Primary key fingerprint: AFC7 DB45 693D 62BB 472B  F27B AB8F 
E924 C2F7 E724
  Subkey fingerprint: A734 4DAD 3C34 1EA1 2D13  C4E6 B273 
8116 12BB 1939


The command is a bit tricky, originally i kept trying the command 
with only the keyring file name, which didnt work, it needed the 
path


(Note: the individual keys in the keyring are currently expired 
and we are working on rolling out a new keyring, but that 
doesn't affect yverifying the existing signatures.)


while you are at it, also add a sha1 or a sh256 checksum, i think 
it will work better to verify the download


dmd download sig file, how do I use it

2018-03-24 Thread Ali via Digitalmars-d-learn

Hi All,

The DMD download is accompanied with a sig file
How exactly do I use this sig file
I am assuming I can use it in place of checksum to verify the 
download


And to be honest, I have almost zero knowledge for gpg and 
encryption
I googled a little but, didnt exactly find what I was hoping to 
find


I tried the following command

gpg --verify dmd_2.079.0-0_amd64.deb.sig dmd_2.079.0-0_amd64.deb


which returns

 gpg: Signature made Fri 02 Mar 2018 01:47:57 PM EST
 gpg:using RSA key B273811612BB1939
 gpg: Can't check signature: No public key


I guess this means, the file is not verified
So how can I do a complete verification?

I also downloaded the keryring file, from link in the download 
page

but also couldnt figure out how to use it

Any explanation of how this gpg sig key works and how to use it 
to verify the download

would be appreciated

thanks


Re: Testing D database calls code for regression

2018-03-19 Thread Ali via Digitalmars-d-learn

On Friday, 16 March 2018 at 20:17:49 UTC, aberba wrote:
How will you test D code which makes calls to database to 
detect bugs and regression. Unlike where you can inject data 
like assert (2+1 == 3), database interfacing code will be 
crazy... Or there's some mocking available for such cases. 
Especially when more features are developed on top.


Well, I am not really sure what you are looking for
but to test database code, there are frameworks for this

Check for example tsqlt ( http://tsqlt.org/ )
this framework is ms sql specific and is the one I have 
experience with


There is also dbfit ( http://dbfit.github.io/dbfit/ ) which seem 
to support more database management frameworks


tsqlt is very good for unit testing, sql procedures or statements
at a high this is how it was setup to be used

1. you prepare sql procedure to create your tables that will be 
used in testing
2. you prepare sql procedure with insert statements to create the 
data sample you want to be used for testing
3. you write a script execute the two procedures from the first 
two step then executed the procedure or statement  you want to 
test and then at the end of this script executed some assert 
statements that is basically your unit test


how to setup used those scripts
1. the setup started a transaction
2. the setup dropped everything in the database
3. the setup executed the scripts from point 3 above to create 
your database, followed by the insert statements scripts or data 
creation script, followed by executing the statement to be tested 
and finally executing the assert statements

4. finally the setup rolled back everything

this setup was support by the tsqlt framework, but honestly i 
dont know how much of this was specific to the environment where 
i worked


but you can use tsqlt to have this

D is not a database language, D is not sql
You should clearly separate testing the D code that call the sql 
statements and testing the sql statements themselves


The above frameworks, will help you test the sql code in isolation



Re: sort, .array and folding on immutable data (finding most common character in column of matrix)

2016-12-19 Thread Ali via Digitalmars-d-learn

On Monday, 19 December 2016 at 14:09:47 UTC, pineapple wrote:
This is a shortcoming of Phobos - here is a package of sorting 
algorithms including some that do not require their inputs to 
be mutable, random access, and/or finite:


https://github.com/pineapplemachine/mach.d/tree/master/mach/range/sort


Oh nice! Will take a looksies

It's worth noting that giving up eagerness, random access, etc. 
often comes with a speed penalty. It may be more efficient just 
to copy the lazy things into memory first and then sort 
in-place, as you have been doing.


True dat!




Re: sort, .array and folding on immutable data (finding most common character in column of matrix)

2016-12-19 Thread Ali via Digitalmars-d-learn
On Monday, 19 December 2016 at 12:45:48 UTC, Nicholas Wilson 
wrote:


Ah, oh well. It was nice in theory.



Indeed. Thank you for trying Nicholas :)



auto word = data.map!(reduce!max).array.map!"a[1]".array;

you want

auto word = data.map!"a[1]".map!(reduce!max).array;



Problem max has to performed on the frequency part of the tuple 
and "word" has to result in a concatenation of all the character 
parts of the highest frequencied letters. So that up there will 
result in "word" = 364782 // or something.




Re: sort, .array and folding on immutable data (finding most common character in column of matrix)

2016-12-19 Thread Ali via Digitalmars-d-learn
On Monday, 19 December 2016 at 10:03:34 UTC, Nicholas Wilson 
wrote:

On Monday, 19 December 2016 at 09:24:38 UTC, Ali wrote:
On Monday, 19 December 2016 at 00:11:49 UTC, Nicholas Wilson 
wrote:

[...]


Ok so laziness stops as soon as sort is required on a range 
then?


No. Because a lazy range is not random access, and therefore 
does not meet sorts requirement.



Ahh, because in place algorithms?


Yes


Are there any plans in
D to make is to that you can output copies to collections so 
that you could do something like filter.transpose.sort and 
just have it output a modified copy?


None that I know.



[...]


Hmm, for the other problem you could do

chain(only(T(dchar.max, uint.max)),range)

and still use the seedless version.


Thanks for input so far!


The seedless version without the typeof(a)(b[0], b[1]) hack (with 
default inited T) seems to crap out with:


"Unable to deduce an acceptable seed type for __lambda2 with 
element type immutable(Tuple!(dchar, uint))."



BTW: Your chain fix does indeed make the T(dchar.init, uint.init) 
seeded fold work on a seedless version of fold :D. Only problem 
is the need for the typeof hack again though.



Some more info: The following alternate version of this program 
works great.


// Swapping the tuple elements that are produced by "group" 
around is necessary for reduce!max to work.
auto data = 
import("data_06.txt").split("\n").transposed.map!`a.array.sort().group.map!"tuple(a[1], a[0])".array`.array;

void main() {
auto word = data.map!(reduce!max).array.map!"a[1]".array;
word.writeln;
}

But this stops working as soon as you take the "word" variable 
outside the scope of main and in to global scope. If you make 
everything static immutable then you get:


"Error: static assert  "Unable to deduce an acceptable seed type 
for std.algorithm.comparison.max with element type 
immutable(Tuple!(uint, dchar))."


Re: Pointer to private structure

2016-12-19 Thread Ali via Digitalmars-d-learn

On Monday, 19 December 2016 at 06:42:27 UTC, Nikhil Jacob wrote:

On Monday, 19 December 2016 at 06:21:10 UTC, ketmar wrote:
i bet that just trying this with D compiler will take less 
time than writing forum post.


I did try but it seems to give compilation failure... Let me 
try once more and I will get back with more details.


What're you trying to do here?

Forward declarations in C++ are used to solve a few different 
things:

1. Reduce build times (unneeded in D AFAIK)
2. Break cyclic references (unneeded in D again?)
3. Give APIs visibility (D's modules and Access layers solve this)
4. Maintain binary compatibility while allowing internal data 
changes (aka pimlp idiom) <-- This I believe you cannot do in D - 
https://wiki.dlang.org/Access_specifiers_and_visibility (someone 
correct me if I'm wrong)


I've seen something about .di files in D. But they seem flakey a 
bit.




Re: sort, .array and folding on immutable data (finding most common character in column of matrix)

2016-12-19 Thread Ali via Digitalmars-d-learn
On Monday, 19 December 2016 at 00:11:49 UTC, Nicholas Wilson 
wrote:

On Sunday, 18 December 2016 at 22:26:50 UTC, Ali wrote:


1. The first line with the splitting, I need to use .array 
three times. The last one I understand is because on "line 2" 
I alias T as the type of the data, and if I don't call .array 
then it's a MapResult type which has no opIndex. Yes?


2. On "line 1" I have to call .array.sort(). Why can't I just 
call .sort() on the transposed rows?




Because sort requires a random access range.


Ok so laziness stops as soon as sort is required on a range then? 
Ahh, because in place algorithms? Are there any plans in D to 
make is to that you can output copies to collections so that you 
could do something like filter.transpose.sort and just have it 
output a modified copy?


Unfortunately arrays have a builtin property sort that for some 
reason takes precedence of std.algorithm.sort when called 
without (). The compiler error is probably because .sort is a 
mutating operation and you're working with immutable data.


Ah ok. I think it's just that array.sort doesn't have CTFE 
capabilities then: "Error: _adSort cannot be interpreted at 
compile time, because it has no available source code"




Try
 })(ImmutableOf!T.init);
or use the seedless version of fold.
may need to import std.traits(?)


Same error message :(



does `data.map!fold!"a[1] > b[1] ? a : b".map!"a[0]".array;` 
work?


Actually I guess what's stopping this is my problem above maybe? 
Since I can't jut return b because of that error. I can't use the 
seedless version of fold either because Another version of the 
problem requires the seed be T.init(dchar.max, uint.max).


Thanks for input so far!




sort, .array and folding on immutable data (finding most common character in column of matrix)

2016-12-18 Thread Ali via Digitalmars-d-learn
Hey, so I have this data file that has a list of a string of 
characters separated by new lines. The task is to find the most 
common letter in each column. Ie if file is:


abc
axy
cxc

Then the letters are a (column 1), x and c.

I've written the code to do this at compile time. But I have a 
few questions about sorting, immutablity casting and the need to 
use .array. The code follows:


==
import std.stdio, std.range, std.algorithm;

// Line 1
static immutable data = 
import("data_06.txt").split("\n").transposed.map!"a.array.sort().group.array".array;


// Line 2
alias T = typeof(data[0][0]);

auto redux(T[] charData) {
return charData.fold!((a, b) {
return a[1] > b[1] ? a : typeof(a)(b[0], b[1]);
})(T.init);
}

// Line 3
static immutable word = data.map!redux.array.map!"a[0]".array;

void main() {
word.writeln;
}
==

Well mostly I'm looking for how to simplify this even further. So 
any hints there would be awesome.


And, there're a few questions I have about the code (I guess 
mainly because of my of my lack of understanding of the type 
system)


1. The first line with the splitting, I need to use .array three 
times. The last one I understand is because on "line 2" I alias T 
as the type of the data, and if I don't call .array then it's a 
MapResult type which has no opIndex. Yes?


2. On "line 1" I have to call .array.sort(). Why can't I just 
call .sort() on the transposed rows?


3. If I call .sort (without parenthesis) I get a compiler error. 
What up with that?


4. On "line 3" I call map with the free function "redux". In this 
function, if I return just "a", it's all good. If I return "b", 
then I get "Incompatible function/seed/element: 
__lambda2/Tuple!(dchar, uint)/immutable(Tuple!(dchar, uint))". So 
my seed is not immutable, but the elements of "data" are. I can 
understand that. But how do I work around it without having to do 
"typeof(a)(b[0], b[1])" ?


5. Is there anyway to get rid of the the "alias" I have in the 
code and just use an inline lambda in my map call on "line 3"?


Re: Accessing members through pointers to structs (also, CTFE associative arrays)

2016-12-17 Thread Ali via Digitalmars-d-learn

On Friday, 16 December 2016 at 01:48:59 UTC, Ali Çehreli wrote:

On 12/15/2016 05:30 PM, Stefan Koch wrote:
On Thursday, 15 December 2016 at 19:30:08 UTC, Ali Çehreli 
wrote:


Yeah, I think the compiler is confused because the function 
is called
in a non-const context during the initialization of an 
immutable object.


I would open an issue:

  https://issues.dlang.org/enter_bug.cgi?product=D

Ali


You cannot Assign Associative Arrays at compile-time.
Because those are defined by druntime have no stable ABI.


Thanks Stefan but at least there should be a better diagnostic 
instead of the confusing current situation that Ali experienced.


Ali


Ah kay. Confusing :)
Thanks again!


Re: Accessing members through pointers to structs (also, CTFE associative arrays)

2016-12-14 Thread Ali via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 23:29:31 UTC, Ali Çehreli wrote:

On 12/13/2016 01:36 PM, Ali wrote:


Now about that second part of my problem 


I'm not entirely sure whether this should work but I think the 
problem is with mutating the 'frequencies' member of an 
immutable element of 'rooms'. The error message means that 
those non-const expressions cannot be shared by a member of an 
immutable AA.


I'm not sure I fully follow here. Because the variable in the 
parse function is not immutable and the frequencies member is not 
"created" yet, so to say. So after I create the frequencies 
object, I assign it to the member of a Room object. The following 
illustrates this more clearly I think:


struct A { int x; }
struct B { int[char] x; }
auto f1() {
  int x;
  x = 3;
  return A(x);
}

auto f2() {
  int[char] x;
  x['A'] = 2;
  return B(x);
}

static immutable a = f1();
static immutable b = f2();

pragma(msg, a);
pragma(msg, b);

This fails to compile with "Error: non-constant expression 
['A':2]". But, the pragma(msg) prints out the correct information 
for both a and b.


What's the deal with static immutable associative arrays and D 
anyway? Is there some kind of roadmap somewhere or bugs that need 
to be fixed before they work properly in D? I'm confused because 
it seems like the data actually gets inside the struct object 
that has an AA as a member, but then the compiler just decides 
"nah, just kidding, this ain't actually ok. bye".




I moved the frequencies to 'data' and hte following worked.


Yeah that is indeed the workaround I mentioned and hence the need 
for the pointer to room problem I was having :p This does work. 
But I guess I'm just looking for a more ideal solution.



Thanks for all the help so far! :)



Re: Accessing members through pointers to structs (also, CTFE associative arrays)

2016-12-13 Thread Ali via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 21:33:11 UTC, Ali Çehreli wrote:

On 12/13/2016 12:30 PM, Ali wrote:

> foreach (i, room; rooms) {
> data[i].room = &room;

That is the address of the local variable room. You need to use 
'ref' there:


foreach (i, ref room; rooms) {

> - Ali



Ahh true!! Cheers. Now about that second part of my problem 



Ali
"the real one :o)"


Haha :)


Re: Alias variable from another class

2016-12-13 Thread Ali via Digitalmars-d-learn
I guess it's because you're accessing a member of _1 from inside 
a scope of _2 where nothing has been constructed yet? Someone 
more familiar with D would probably have a better answer on this 
part. I'm quite new :)


But a work around would be

class _2 {
  _1 instance;

  auto twelve() @property {
  return instance.i;
  }

  this() {
  instance = new _1(12);
  }
  }




Re: Accessing members through pointers to structs (also, CTFE associative arrays)

2016-12-13 Thread Ali via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 21:08:31 UTC, drug007 wrote:

(*d.room).name


Oh yeah, tried that too. That at least compiles but gives a 
runtime exception (bad address).





Re: reading from file

2016-12-13 Thread Ali via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 16:59:17 UTC, Namal wrote:

On Tuesday, 13 December 2016 at 16:57:40 UTC, Namal wrote:
Sorry if I wasn't clear. The array should be two demensional 
and each line in text line should be a row in that 2x2 array.


Also, it should be saved as an integer.


And extending Ali's solution you can actually get the data in to 
a two dimentional array at compile time and have it in static 
memory with a small adjustment:


static immutable matrix = import("data.txt")
.split("\n")
.map!(a => a.split(",").map!(to!int).array)
.array;

void main() {
writeln(matrix);
}


Accessing members through pointers to structs (also, CTFE associative arrays)

2016-12-13 Thread Ali via Digitalmars-d-learn
Hi, Long time watcher and recently started playing with D a bit 
more. Ran in to a couple of snags that I'll combine in one post. 
It involves a data set that contains a list of strings. Each 
string represents a Room name. What I'm trying to do is pluck out 
the room names and also calculate the frequency each letter 
occurs in a name, per room.


First problem is to do with pointers to structs. Here's the code:

static immutable rooms = 
import("data.txt").split("\n").map!parse.array;


static Tuple!(const(Room*), "room", int[char], 
"frequencies")[rooms.length] data;

static this() {
foreach (i, room; rooms) {
data[i].room = &room;
// Also calculate frequencies, but that's not important 
yet.

}
}

void main() {
foreach (d; data) {
d.room.name.writeln; // <-- How do I access name here??
}
}

I've tried d.(*room).name but that didn't work. There's no arrow 
operator. I've tried making my tuple a ref Room instead, but 
that's a no go as well. I can copy the Room object directly in to 
the tuple, but since it's already there in static immutable data 
I'd rather just have a pointer to it.


Is there a way to do that?

Second problem is to do with associative arrays. At first the 
Room object had a frequencies object in it (ie: int[char] <- 
number of times a character appears in the name).


In my parse function, if I add a foreach loop that loops through 
the letters in the room's name, and adds populates an associative 
array like so:


Room parse(string line) {
immutable name = // blah
int[char] frequencies;
foreach (letter; name) {
frequencies[letter] += 1
}
return Room(name, frequencies);
}

pragma(msg, rooms); // <- this works!

In the above case the pragma actually prints out all the Room 
objects, with their respective frequencies calculated correctly. 
But *after* it has printed it out, then a whole list of 
compilation errors that all look like this:


Error: non-constant expression ['d':1, 'r':3, 'x':1, 'e':1, 
'v':2, 'k':2, 'z':1, 't':1, 'u':1, 'p':2, 'c':1, 's':1, 'f':2, 
'i':2]


But it seems that it was calculated correctly, it just can't be 
assigned to the actual variable.


My current workaround includes taking frequencies out of the Room 
struct and calculating them inside a module constructor (hence 
the first question on the Tuple and Room *)


Are there other workarounds?

Cheers, and thanks for any help!
- Ali