[Implemented] TypeOf() compiler directive

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
pcfreak
User
User
Posts: 75
Joined: Sat May 22, 2004 1:38 am

[Implemented] TypeOf() compiler directive

Post by pcfreak »

Well, as I was working with macros, made some nice LinkedLists,
HashTables and the like quite easy usable for any type I realized that
there could be a better way to make template functions. By implementing
a compiler directive - whatever name it shall be called, e.g. TypeOf() -
you could check by CompilerIf and the like whether a variable is of a
specific type or not. As such thing is defined _before_ runtime it can be
used as compiler directive.
A small example for usage could this:

Code: Select all

Macro MakeFunction(type)
 Procedure.type Add#type(a.type, b.type)
  CompilerIf TypeOf(a, #PB_COMPILER_TYPE_STRING)
   ProcedureReturn Str(Val(a) + Val(b))
  CompilerElse
   ProcedureReturn a + b
  CompilerEndIf
 EndProcedure
EndMacro

Macro Add(a, b)
 Add#TypeOf(a)(a, b)
EndMacro

MakeFunction(l)
MakeFunction(s)

Debug Add(5, 6)
Debug Add("5", "6")
Possible Syntax for TypeOf() is TypeOf(Variable [, Type]) returning
#True or #False if two parameters are passed or the Type of the Variable
if only one was passed.
byo
Enthusiast
Enthusiast
Posts: 635
Joined: Mon Apr 02, 2007 1:43 am
Location: Brazil

Post by byo »

But how would you know a chunk of memory is of a given type? I never saw a language where you could retrieve the variable type in memory. It's the programmer's task to do that. If it was that easy the compiler would say "Hey, this is data from a variable of type long and not a string" when you misused PeekS().
Proud registered Purebasic user.
Because programming should be fun.
User avatar
pcfreak
User
User
Posts: 75
Joined: Sat May 22, 2004 1:38 am

Post by pcfreak »

You don't need to know this, 'cause you won't use this macro for memory
but for defined variables. It's not that you will do something like
TypeOf(@var), you will always do something like TypeOf(var), where
"var" is a well defined variable already known to the compiler.
I don't see there any problem with this compiler directive at all.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

I see a problem.
implementing this would need a second compiler pass.
would need to totally change the compiler's architecture.

note that SizeOf() by that reason only works with Structs, not with Variables.

I think, it's to much efford compared to the effect.

PureBasic is a Programming Language, not a Template Tool Kit.
thus, improvements to the functions and the compiler are of much higher priority
than adding fancy meta-functions to make templating easier.

if you really need such, what about trying to write yourself a preparser?
oh... and have a nice day.
Fred
Administrator
Administrator
Posts: 18161
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Should be no problem to add this.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

I'd like this one too, been playing with some constructions in the past and they were variable type dependend, would be great to have this.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply