Why no Procedures in Residents

For everything that's not in any way related to PureBasic. General chat etc...
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Why no Procedures in Residents

Post by jacdelad »

Hello,
in the light of my last question, AddElement() with an optional parameter to also assign a value to the new element, someone suggested to use residents for this. I now read what residents do and a bit how they work.
My question is, why can't I add procedures? It would be a good way to auto-add new functions without the use of include files.
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
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Why no Procedures in Residents

Post by BarryG »

I haven't tried Procedures, but you can definitely have macros in Residents files; I've done this before. So if your procedures are short non-complicated things, maybe try using them as resident Macros instead?
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Why no Procedures in Residents

Post by jacdelad »

The help explicitely states that procedures or dynamic code is not possible.
E.g. I want to include the Windows registry functions from ts-soft as native commands without having to always include the pbi-file. I thought I could do that with a resident, but that doesn't seem possible.
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
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Why no Procedures in Residents

Post by BarryG »

Dynamic code isn't possible? What do you mean? You can definitely use macros with passed parameters as resident, but maybe you don't mean that?

For example, this works just fine in a Resident file:

Code: Select all

Macro Msg(text)
  MessageRequester("Title",text)
EndMacro
Then I can just run the following code without any IncludeFile, just as though Msg() was a native command:

Code: Select all

text$=InputRequester("Test","Enter some text:","")
Msg("Text was: "+text$) ; Text was dynamic, yes?
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Why no Procedures in Residents

Post by jacdelad »

I don't know, I just quoted the help file.
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
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Why no Procedures in Residents

Post by skywalk »

Dynamic code means it must be compiled.
Macros are text replacement only before compilation.
For procedures and prototypes, you need to consider dll. Static libs are not officially supported yet. Those would be cool.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Caronte3D
Addict
Addict
Posts: 1027
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Why no Procedures in Residents

Post by Caronte3D »

I'm curious what "residents" are :?
AZJIO
Addict
Addict
Posts: 1313
Joined: Sun May 14, 2017 1:48 am

Re: Why no Procedures in Residents

Post by AZJIO »

Caronte3D wrote: Thu Aug 11, 2022 9:22 am I'm curious what "residents" are :?
https://www.purebasic.com/documentation ... dents.html
User avatar
Caronte3D
Addict
Addict
Posts: 1027
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Why no Procedures in Residents

Post by Caronte3D »

Thanks! seems useful :wink:
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Why no Procedures in Residents

Post by jacdelad »

Ok, so I tried to create a resident file from a function (ToFraction), which created a resident file, but id doesn't work. So the help is right, I cannot use procedures within residents. This is an unfortunate limitation, but due to my poor knowledge about this topic I can't complain. Maybe Fred can help out, it would be extra cool to be able to enhance the list of functions by useful functions that are always available without includes.
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
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Why no Procedures in Residents

Post by BarryG »

What's the function look like? I want to try it as a macro.
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Why no Procedures in Residents

Post by jacdelad »

Taken from this (http://forums.purebasic.com/german/view ... 77#p358136) thread and slightly changed:

Code: Select all

Procedure.s ToFraction (Double.d, MaxRelativeError.d=1e-6)
   Protected.q LowNumerator = 0, LowDenominator = 1, HighNumerator = 1, HighDenominator, Numerator = 1, Denominator, Fraction, OldFraction, Factor, Iteration
   Protected sign$
   
   If IsNAN(Double) Or IsInfinity(Double)
      ProcedureReturn StrD(double)         ; NaN, +Infinity or -Infinity
   EndIf
   
   If Double < 0
      Double = Abs(Double)
      sign$ = "-"
   EndIf
   
   If Double > 1e16
      ProcedureReturn "Overflow"
   ElseIf Double < 1e-16
      ProcedureReturn "Underflow"
   EndIf
   
   HighDenominator = Round(1/Double, #PB_Round_Down) - 1
   Denominator = HighNumerator + HighDenominator
   Fraction = Infinity()
   
   Repeat
      Iteration + 1
      OldFraction = Fraction
      Fraction = 1 - Double * Denominator / Numerator
      ; Debug "Iteration "+RSet(Str(Iteration),3)+":  " + Str(Numerator) + " / " + Str(Denominator) + "   ( δ = "+Fraction+" )"
      Factor = Abs(Fraction) / (Abs(OldFraction) - Abs(Fraction))
      If Factor < 1 Or Fraction > 0 Or OldFraction > 0
         Factor = 1
      EndIf
      If Fraction < -MaxRelativeError          ; zu klein
         LowNumerator   = Numerator
         LowDenominator = Denominator
         Numerator   + Factor*HighNumerator
         Denominator + Factor*HighDenominator
      ElseIf Fraction > MaxRelativeError       ; zu groß
         HighNumerator   = Numerator
         HighDenominator = Denominator
         Numerator   + Factor*LowNumerator
         Denominator + Factor*LowDenominator
      Else
         ProcedureReturn sign$ + Str(Numerator) + "/" + Str(Denominator)
      EndIf
   ForEver
EndProcedure
This can't be changed into a macro. Also, even if, this wouldn't solve my problem (or say, do what I want to do).
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
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Why no Procedures in Residents

Post by BarryG »

Yeah, that's definitely not short and non-complicated.

It's not that hard to put a quick IncludeFile line in your code though? Looks like you're creating a lot of grief over a non-issue?
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Why no Procedures in Residents

Post by jacdelad »

This may be. I have found a lot of useful, optimized and well working code found here and in some other places. I save them into pbi-files, to include it, like the Windows-Registry-include, my ListIconGadget-enhancements etc. Of course, I can include them into every project I want to, it would be just much more convenient to automatically have the functions available. It was just a thought, I know it can become complicated if one function calls another not-predefined function and so on. That's why I wanted to discuss and understand it.
I guess xincluding it isn't so inconvenient at all. I'm just lazy.
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
AZJIO
Addict
Addict
Posts: 1313
Joined: Sun May 14, 2017 1:48 am

Re: Why no Procedures in Residents

Post by AZJIO »

When I wanted to solve this problem, the first thing that came to my mind was to use alternative auto-completion. That is, you use an abbreviation to insert "xinclude ...".
Post Reply