Page 1 of 1
Directly assigning a value via AddElement()
Posted: Thu Aug 04, 2022 1:17 pm
by jacdelad
I would like to be able to directly assign a value to a linked list when using AddElement().
Before:
Code: Select all
NewList MyList.i()
AddElement(MyList())
MyList()=123456
After:
Code: Select all
NewList MyList.i()
AddElement(MyList(),123456)
And of course this being optional and limited to simple variables.
Re: Directly assigning a value via AddElement()
Posted: Thu Aug 04, 2022 4:04 pm
by Mijikai
Hmm...
Unsafe Macro (dont do this!):
Code: Select all
Macro AddInteger(_lst_,_int_)
AddElement(_lst_):_lst_ = _int_
EndMacro
This would be safe and correct but slower:
Code: Select all
Procedure.i AddInteger(List MyList(),Value.i)
If AddElement(MyList())
MyList() = Value
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
there is no nice way to do it, other then writing it directly.
This means there is no good way to do it without creating an entirely new function and compiler switch
since you would otherwise force an additional check on AddElement() which will make it slower for all other tasks.
Imho. its just one line so i dont see the issue tbh.
Re: Directly assigning a value via AddElement()
Posted: Thu Aug 04, 2022 4:12 pm
by jacdelad
Almost every time I add an element I want to assign a value immediately afterwards, so yes, it only saves one line, but for me it feels more "natural".
Re: Directly assigning a value via AddElement()
Posted: Thu Aug 04, 2022 4:40 pm
by Rinzwind
Yup, obvious missing optional parameter.
+1
Re: Directly assigning a value via AddElement()
Posted: Thu Aug 04, 2022 5:11 pm
by STARGĂ…TE
If you really think about such a simplification, then the addition of multiple elements would be a complete solution.
Otherwise you have to add another wish next time
Code: Select all
AddElement(MyList(), 1)
AddElement(MyList(), 2)
AddElement(MyList(), 3)
; simplifies to
AddElement(MyList(), 1, 2, 3)
However, keep in mind, such wishes, which can be easily implemented by the programer without disadvantages in speed, usually have low priority to be realized.
Re: Directly assigning a value via AddElement()
Posted: Thu Aug 04, 2022 5:28 pm
by Marc56us
Personally, I put the two commands on one line,
Code: Select all
AddElement(MyList()) : MyList() = ""
Then I press
CTRL + D as many times as necessary
Then I put values.
It doesn't exactly answer the question, but it goes very fast to initialize a list.

Re: Directly assigning a value via AddElement()
Posted: Thu Aug 04, 2022 5:30 pm
by StarBootics
Mijikai wrote: Thu Aug 04, 2022 4:04 pm
Hmm...
Unsafe Macro (dont do this!):
Code: Select all
Macro AddInteger(_lst_,_int_)
AddElement(_lst_):_lst_ = _int_
EndMacro
Can you explain why this is unsafe. Because I'm using such macro for the last 10 years at least without any problem at all.
Best regards
StarBootics
Re: Directly assigning a value via AddElement()
Posted: Thu Aug 04, 2022 5:33 pm
by jacdelad
@STARGATE: Sure, but it is often used in loops where it is called only once.
I have no problem with using macros and such, but I find it a bit annoying to always include them at the beginning of a code and not having them autoinserted when they are placed in a certain folder or file (this was discussed somewhere else too).
Re: Directly assigning a value via AddElement()
Posted: Thu Aug 04, 2022 5:58 pm
by StarBootics
jacdelad wrote: Thu Aug 04, 2022 5:33 pm
@STARGATE: Sure, but it is often used in loops where it is called only once.
I have no problem with using macros and such, but I find it a bit annoying to always include them at the beginning of a code and not having them autoinserted when they are placed in a certain folder or file (this was discussed somewhere else too).
What about compiling a Resident file with your macros ?
Best regards
StarBootics
Re: Directly assigning a value via AddElement()
Posted: Thu Aug 04, 2022 6:04 pm
by Mijikai
Its unsafe because you assume that enough memory is available to expand the list.
If there is not enough memory AddElement() will fail, now List() will still point at the last active entry and overrides it.
Im sure you check the return of AllocateMemory().
Re: Directly assigning a value via AddElement()
Posted: Thu Aug 04, 2022 6:05 pm
by jacdelad
StarBootics wrote: Thu Aug 04, 2022 5:58 pm
jacdelad wrote: Thu Aug 04, 2022 5:33 pm
@STARGATE: Sure, but it is often used in loops where it is called only once.
I have no problem with using macros and such, but I find it a bit annoying to always include them at the beginning of a code and not having them autoinserted when they are placed in a certain folder or file (this was discussed somewhere else too).
What about compiling a Resident file with your macros ?
Best regards
StarBootics
I don't know how that works. Is there a good introduction other than the helpfile?
Re: Directly assigning a value via AddElement()
Posted: Fri Aug 05, 2022 4:59 pm
by Rinzwind
Btw this should go hand in hand with one-time-use structure declaration and initialization.
Guess we will never see that too. Also welcome when handling procedures with structure parameters.
Surprise me

Re: Directly assigning a value via AddElement()
Posted: Mon Aug 08, 2022 3:38 am
by DeanH
+1 from me, too. I used linked lists a great deal instead of arrays. Also, assigning a value to a mapped list element is a single line like MappedVar( a$) = 12435 . I once made up a set of procedures to add an element in one line.
Code: Select all
;Procedure to add a string to a linked string list ... example without error checking
Procedure ListAdd( List temp$(), value$)
AddElement( temp$() )
temp$() = value$
endprocedure
Use: ListAdd( myList$(), myValue$ )
Re: Directly assigning a value via AddElement()
Posted: Mon Aug 08, 2022 5:59 am
by AZJIO
DeanH wrote: Mon Aug 08, 2022 3:38 am
Code: Select all
;Procedure to add a string to a linked string list ... example without error checking
Procedure ListAdd( List temp$(), value$)
AddElement( temp$() )
temp$() = value$
endprocedure
Will this be optimized to run in a loop? You increase the number of events. If you make it a macro, then it will be expanded during compilation, without adding extra events.
jacdelad wrote: Thu Aug 04, 2022 6:05 pmI don't know how that works. Is there a good introduction other than the helpfile?
This is some kind of permanent include file. (took from
here)
Code: Select all
IncludePath #PB_Compiler_Home+"Include\"
XIncludeFile "File1.pb"
XIncludeFile "File2.pb"
Re: Directly assigning a value via AddElement()
Posted: Wed Aug 10, 2022 3:08 am
by Rinzwind
If only pb had an inline command so you know the procedure is always uhm inlined/inserted... macro's cannot replace this functionality because of their obvious limitations. Well I'm sure there exists a feature request for that already for a decade. Sigh. The language itself is not evolving at all. Stop rant, go back to work ;