Directly assigning a value via AddElement()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Directly assigning a value via AddElement()

Post 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.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Directly assigning a value via AddElement()

Post 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.
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Directly assigning a value via AddElement()

Post 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".
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Rinzwind
Enthusiast
Enthusiast
Posts: 636
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Directly assigning a value via AddElement()

Post by Rinzwind »

Yup, obvious missing optional parameter.
+1
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Directly assigning a value via AddElement()

Post 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 :lol:

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.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Directly assigning a value via AddElement()

Post 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.
8)
Last edited by Marc56us on Thu Aug 04, 2022 5:53 pm, edited 3 times in total.
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Directly assigning a value via AddElement()

Post 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
The Stone Age did not end due to a shortage of stones !
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Directly assigning a value via AddElement()

Post 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).
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Directly assigning a value via AddElement()

Post 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
The Stone Age did not end due to a shortage of stones !
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Directly assigning a value via AddElement()

Post 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().
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Directly assigning a value via AddElement()

Post 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?
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Rinzwind
Enthusiast
Enthusiast
Posts: 636
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Directly assigning a value via AddElement()

Post 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 ;)
User avatar
DeanH
Enthusiast
Enthusiast
Posts: 223
Joined: Wed May 07, 2008 4:57 am
Location: Adelaide, South Australia
Contact:

Re: Directly assigning a value via AddElement()

Post 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$ )
AZJIO
Addict
Addict
Posts: 1315
Joined: Sun May 14, 2017 1:48 am

Re: Directly assigning a value via AddElement()

Post 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"
Rinzwind
Enthusiast
Enthusiast
Posts: 636
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Directly assigning a value via AddElement()

Post 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 ;
Post Reply