Thread overview
array function
Aug 31, 2015
Namal
Aug 31, 2015
Rikki Cattermole
Aug 31, 2015
Namal
Aug 31, 2015
bearophile
Aug 31, 2015
Namal
Aug 31, 2015
Namal
Aug 31, 2015
cym13
Aug 31, 2015
cym13
August 31, 2015
Hello,

can someone explain to me please what I am doing wrong by passing an integer to this function and then just creating a static array? The error I get is:

Error: variable N cannot be read at compile time

int[] foo(int N){


	int[N] v;
	//do something with it
	int[] s;
	return s;
}

void main(){

	int N = 12;
	int[] A;
	A=prim_numbers(N);

}
August 31, 2015
On 31/08/15 11:24 PM, Namal wrote:
> Hello,
>
> can someone explain to me please what I am doing wrong by passing an
> integer to this function and then just creating a static array? The
> error I get is:
>
> Error: variable N cannot be read at compile time
>
> int[] foo(int N){
>
>
>      int[N] v;
>      //do something with it
>      int[] s;
>      return s;
> }
>
> void main(){
>
>      int N = 12;
>      int[] A;
>      A=prim_numbers(N);
>
> }

You cannot define static arrays using runtime information.
You must use dynamic arrays.

int[] foo(int N) {
	int[] v;
	v.length = N;
	// do something with it
	int[] 2;
	return s;
}


Of course you can pass it in via a template argument. But this of course does not work at runtime like you are wanting.
August 31, 2015
On Monday, 31 August 2015 at 11:27:20 UTC, Rikki Cattermole wrote:

> You cannot define static arrays using runtime information.
> You must use dynamic arrays.
>
> int[] foo(int N) {
> 	int[] v;
> 	v.length = N;
> 	// do something with it
> 	int[] 2;
> 	return s;
> }
>
>
> Of course you can pass it in via a template argument. But this of course does not work at runtime like you are wanting.


Hmm, this has never been a problem for me in C++

#include <vector>


std::vector<int> foo(int N){

	std::vector<int> V(N);
	int some_array[N];

	std::vector<int> other_V;

	return other_V;
	
}

int main(){

std::vector<int> V = foo(12);


}

compiles

August 31, 2015
Namal:

> std::vector<int> foo(int N){
>
> 	std::vector<int> V(N);
> 	int some_array[N];

VLAs are not present in D.

Bye,
bearophile
August 31, 2015
On Monday, 31 August 2015 at 12:00:26 UTC, bearophile wrote:
> Namal:
>
>> std::vector<int> foo(int N){
>>
>> 	std::vector<int> V(N);
>> 	int some_array[N];
>
> VLAs are not present in D.
>
> Bye,
> bearophile

Yah, I guess I have been damaged with them when I started to learn programming in C++ >(
August 31, 2015
Hey guys, since I am learning D arrays here, can you tell me the best way to remove an element at the end of an array or at some index i?
August 31, 2015
On Monday, 31 August 2015 at 13:00:49 UTC, Namal wrote:
> Hey guys, since I am learning D arrays here, can you tell me the best way to remove an element at the end of an array or at some index i?

import std.algorithm;

T[] arr;
arr = arr.remove(index);

http://p0nce.github.io/d-idioms/#Adding-or-removing-an-element-from-arrays
August 31, 2015
On Monday, 31 August 2015 at 13:17:30 UTC, cym13 wrote:
> On Monday, 31 August 2015 at 13:00:49 UTC, Namal wrote:
>> Hey guys, since I am learning D arrays here, can you tell me the best way to remove an element at the end of an array or at some index i?
>
> import std.algorithm;
>
> T[] arr;
> arr = arr.remove(index);
>
> http://p0nce.github.io/d-idioms/#Adding-or-removing-an-element-from-arrays

Also, to remove the last element you can use slices:

T[] arr;
arr = arr[0..$-1];