Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Let's Learn D!

Name: Anonymous 2012-02-08 13:42

www.d-programming-language.org/

Hey /prog/! Today we're going to learn about the D programming language. Today's topic is array slices.


// main.d
void main() // Ok in D. The runtime handles main's return
{
    int[10] arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    int[2] slice = arr[3..5];
    writeln(slice) // "[4, 5]"
}


Array slices in D capture a [bold]view[/bold] of the original array. They DO NOT create a copy.

In D, since strings are arrays of characters, the exact same syntax works with strings.


// main.d
int main() // int main() is fine as well
{          // but you must supply a return value
    string s = "Yo prog, I herd you liek programming";
    // $ is shorthand for the end index of the array
    string sslice = s[10..$];
    writeln(sslice); // "I herd you liek programming";

    sslice = "I herd you liek jews";
    writeln(sslice); // "I herd you liek jews"
    writeln(s); // "Yo prog, I herd you liek jews"

    return 0;
}


So as you can see, modifying a slice of an array also modifies the original array. Good luck coding!

Name: Anonymous 2012-02-08 13:45

D is fucking shit.

Name: Anonymous 2012-02-08 14:13

You are putting it as if this was something new. Also, when ``The runtime handles main's return'', why are you still handling it yourself in the second example?

Name: Anonymous 2012-02-08 14:31

Array slices in D capture a [bold]view[/bold] of the original array. They DO NOT create a copy.
Oh wow this is such a difficult concept, thanks for explaining it to me!

On a more serious note, fuck off back to /g/ you stupid piece of shit.

Name: Anonymous 2012-02-08 14:54

no d ever

Name: Anonymous 2012-02-08 16:58

C++ or die

Name: Anonymous 2012-02-09 8:38

>>3
I'm just illustrating a few different ways to do things. Defining main() as void main() tells the runtime to handle returning a value. Defining int main() tells it that you're going to do it yourself (and you'd better mean it, because if you define int main() and don't return a value, your code won't compile).

Name: Anonymous 2012-02-09 8:42

>>3
This IS new, to the C/C++/Java crowd. None of these languages support built-in array slicing.

>>4
Not everyone is a super 1337 coder. The point is to make people aware of some of D's important features.

Name: Anonymous 2012-02-09 8:45

>>8
Nobody on /prog/ needs you tell them the most basic shit about programming. Fuck off back to /g/ you stupid piece of shit.

Name: Anonymous 2012-02-09 8:53

>>9
From what I've seen, nobody on /prog/ can actually program.

Name: Anonymous 2012-02-09 8:55

>>10
That's because you only look at the posts that are from retards like yourself.

Name: Anonymous 2012-02-09 8:55

>>10
I'm an EXPERT BBCODE PROGRAMMER.

Name: Anonymous 2012-02-09 9:01

writeln

get the fuck out

Name: Anonymous 2012-02-09 9:13

>>13
Inb4 writeln works exactly like printf.

Name: Anonymous 2012-02-09 12:10

D is heaps better than C++. Its like programming real future C, not this crap they shove into the Concept C++/c11x

Name: Anonymous 2012-02-09 16:35

>>15
heaps better than C++
Not saying much.

Don't change these.
Name: Email:
Entire Thread Thread List