Page 1 of 1

[Implemented] TypeOf() compiler directive

Posted: Wed Jul 30, 2008 10:19 pm
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.

Posted: Thu Jul 31, 2008 1:05 am
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().

Posted: Thu Jul 31, 2008 1:32 am
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.

Posted: Thu Jul 31, 2008 10:44 am
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?

Posted: Thu Jul 31, 2008 11:33 am
by Fred
Should be no problem to add this.

Posted: Thu Jul 31, 2008 5:35 pm
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.