www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Add Element to list not Working

reply "Chris Pons" <cmpons gmail.com> writes:
I'm trying to add an element to a list with insert but that 
doesn't seem to do anything at all. If I try using ~= it says 
that "Error: cannot append type Node to type SList!(Node). I'm 
pretty confused about using ~= because it works fine for arrays 
but apperantly not for lists.

How do I add an element to a list?
Apr 01 2012
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/01/2012 10:45 PM, Chris Pons wrote:
 I'm trying to add an element to a list with insert but that doesn't seem
 to do anything at all. If I try using ~= it says that "Error: cannot
 append type Node to type SList!(Node). I'm pretty confused about using
 ~= because it works fine for arrays but apperantly not for lists.

 How do I add an element to a list?

import std.stdio; import std.container; void main() { auto l = SList!int(); l.insert(42); // inserts front l.insert(43); // this too assert(l == SList!int(43, 42)); // inserts after the specified range (l[] is the entire list) l.insertAfter(l[], 44); assert(l == SList!int(43, 42, 44)); // This doesn't work because SList.Range doesn't define opOpAssign!"~" // l[] ~= 45; } Ali
Apr 01 2012
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/01/2012 11:18 PM, Chris Pons wrote:
 Thanks. I tried doing this and the list didn't update:

 void AddToList( SList!int list, int i )
 {
 list.insert( i );
 }

Oh, that has nothing to do with SList. SList is a struct and as a fundamental rule of D, structs are copied to functions. SList happens to be a struct. The list parameter of AddToList and the list variable that is passed to the function as an argument are two different variables.
 SList!int intList;

 AddToList( intList, 42 );

 but when I switched to this, it worked:

 SList!int intList;

 void AddToList( int i )
 {
 intList.insert( i );
 }

Global variables are not a good solution of course. :( The solution here is to pass the argument by-reference by the 'ref' keyword: void AddToList( ref SList!int list, int i ) { // ... } Ali P.S. There is this chapter that covers 'ref' and most (but not all) types of function parameters: http://ddili.org/ders/d.en/function_parameters.html
Apr 02 2012
prev sibling next sibling parent James Miller <james aatch.net> writes:
On 2 April 2012 17:45, Chris Pons <cmpons gmail.com> wrote:
 I'm trying to add an element to a list with insert but that doesn't seem to
 do anything at all. If I try using ~= it says that "Error: cannot append
 type Node to type SList!(Node). I'm pretty confused about using ~= because
 it works fine for arrays but apperantly not for lists.

 How do I add an element to a list?

opAppend (or whatever it is) isn't defined for alot of types that it probably should be. There should be an append method that you can use though. -- James Miller
Apr 01 2012
prev sibling next sibling parent "Chris Pons" <cmpons gmail.com> writes:
Thanks. I tried doing this and the list didn't update:

void AddToList( SList!int list, int i )
{
     list.insert( i );
}

SList!int intList;

AddToList( intList, 42 );

but when I switched to this, it worked:

SList!int intList;

void AddToList( int i )
{
     intList.insert( i );
}

AddToList( 42 );

The first method didn't give an error it just didn't update the 
list as I thought. Any idea?

On Monday, 2 April 2012 at 06:07:40 UTC, Ali Çehreli wrote:
 On 04/01/2012 10:45 PM, Chris Pons wrote:
 I'm trying to add an element to a list with insert but that 
 doesn't seem
 to do anything at all. If I try using ~= it says that "Error: 
 cannot
 append type Node to type SList!(Node). I'm pretty confused 
 about using
 ~= because it works fine for arrays but apperantly not for 
 lists.

 How do I add an element to a list?

import std.stdio; import std.container; void main() { auto l = SList!int(); l.insert(42); // inserts front l.insert(43); // this too assert(l == SList!int(43, 42)); // inserts after the specified range (l[] is the entire list) l.insertAfter(l[], 44); assert(l == SList!int(43, 42, 44)); // This doesn't work because SList.Range doesn't define opOpAssign!"~" // l[] ~= 45; } Ali

Apr 01 2012
prev sibling parent "Chris Pons" <cmpons gmail.com> writes:
Ah, thank you. I didn't realize taht SList is a struct and that 
it used value semantics. That clears this up.

On Monday, 2 April 2012 at 14:22:25 UTC, Ali Çehreli wrote:
 On 04/01/2012 11:18 PM, Chris Pons wrote:
 Thanks. I tried doing this and the list didn't update:

 void AddToList( SList!int list, int i )
 {
 list.insert( i );
 }

Oh, that has nothing to do with SList. SList is a struct and as a fundamental rule of D, structs are copied to functions. SList happens to be a struct. The list parameter of AddToList and the list variable that is passed to the function as an argument are two different variables.
 SList!int intList;

 AddToList( intList, 42 );

 but when I switched to this, it worked:

 SList!int intList;

 void AddToList( int i )
 {
 intList.insert( i );
 }

Global variables are not a good solution of course. :( The solution here is to pass the argument by-reference by the 'ref' keyword: void AddToList( ref SList!int list, int i ) { // ... } Ali P.S. There is this chapter that covers 'ref' and most (but not all) types of function parameters: http://ddili.org/ders/d.en/function_parameters.html

Apr 02 2012