Page 1 of 2
Why no Procedures in Residents
Posted: Thu Aug 11, 2022 2:57 am
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.
Re: Why no Procedures in Residents
Posted: Thu Aug 11, 2022 3:11 am
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?
Re: Why no Procedures in Residents
Posted: Thu Aug 11, 2022 3:58 am
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.
Re: Why no Procedures in Residents
Posted: Thu Aug 11, 2022 4:36 am
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?
Re: Why no Procedures in Residents
Posted: Thu Aug 11, 2022 5:18 am
by jacdelad
I don't know, I just quoted the help file.
Re: Why no Procedures in Residents
Posted: Thu Aug 11, 2022 6:42 am
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.
Re: Why no Procedures in Residents
Posted: Thu Aug 11, 2022 9:22 am
by Caronte3D
I'm curious what "residents" are

Re: Why no Procedures in Residents
Posted: Thu Aug 11, 2022 4:52 pm
by AZJIO
Caronte3D wrote: Thu Aug 11, 2022 9:22 am
I'm curious what "residents" are
https://www.purebasic.com/documentation ... dents.html
Re: Why no Procedures in Residents
Posted: Thu Aug 11, 2022 5:08 pm
by Caronte3D
Thanks! seems useful

Re: Why no Procedures in Residents
Posted: Thu Aug 11, 2022 9:17 pm
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.
Re: Why no Procedures in Residents
Posted: Fri Aug 12, 2022 1:04 am
by BarryG
What's the function look like? I want to try it as a macro.
Re: Why no Procedures in Residents
Posted: Fri Aug 12, 2022 1:30 am
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).
Re: Why no Procedures in Residents
Posted: Fri Aug 12, 2022 1:42 am
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?
Re: Why no Procedures in Residents
Posted: Fri Aug 12, 2022 1:55 am
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.
Re: Why no Procedures in Residents
Posted: Fri Aug 12, 2022 2:24 am
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 ...".