1. Type Class!
- Posted by Mike The Spike <mtsreborn at yahoo.com> Mar 17, 2001
- 528 views
Here's a little trick I fished up just now... You like VB huh? You like it when you can create a property, and set and get the status of a variable... Confused? No problem, Eu can do that too! Here it is... If you didn't know you could do this (people are afraid of using 'type' nowadays), you'll think how you could miss this cool feature of Eu's. Ok... What if you'd want a variable, that when assigned a value, it autmatically processes that data using your own specifc code? An example? Here ya go. type type_writer(sequence s) puts(1,s) return 1 end type Now simply say; type_writer echo echo = "Hello World!" What happens? "Hello World!" is printed to the screen. Also usefull for doing Memory Dumping. Keep track of each value assigned to a variable, by writing them to disk. Wouldn't it be cool to do 'classes' in Eu, FOR REAL? Well, you can! Like this; type class(sequence func) call_proc(func,func[2]) return 1 end type Now you can do this!; class myclass myclass = {"MyPuts","Hello World!"} After this assignement, routine 'MyPuts' is called with "Hello World!", thus writing this string to the screen (assuming that's what MyPuts does). That ain't all folks... There *LOADS* of things you can do with types... Go ahead! Try it! Classes, properties, debugging, resource tracking, ByRef passing (routine_id works on types aswell!!), etc... There's no limits, just your own imagination. So go ahead! Types won't bite! Mike The Spike PS. Don't forget to use 'without type_check' for extra speed boosts!
2. Re: Type Class!
- Posted by David Cuny <dcuny at LANSET.COM> Mar 18, 2001
- 464 views
tacitus wrote: > i was interested in types as well - but > soon came to the conclusion that they > are really just functions disguised in > a slightly different syntax. Yes, for *emulating classes* they are essentially worthless. First, you can't query an object to see what class it belongs to. For example: type this( integer i ) if i > 5 then return 1 else return 0 end if end type type that( integer i ) if i > 10 then return 1 else return 0 end if end type Now, declare an object: that instanceOfThat if this( instanceOfThat ) then puts( "class = this\n") end if if that( instanceOfThat ) then puts( "class = that\n") end if Lo and behold, it belongs to both types! The other major problem is that you lose the 'type' when it's passed through a 'generic' type. For example: mySpecialType myInstance procedure foo( object bar ) end procedure If you call: foo( myInstance ) and myInstance is essentially cast into an object. -- David Cuny
3. Re: Type Class!
- Posted by Mike The Spike <mtsreborn at yahoo.com> Mar 18, 2001
- 487 views
> Yes, for *emulating classes* they are essentially > worthless. Explain... > First, you can't query an object to see what class > it belongs to. For > example: > > type this( integer i ) > if i > 5 then return 1 else return 0 end if > end type > > type that( integer i ) > if i > 10 then return 1 else return 0 end if > end type > > Now, declare an object: > > that instanceOfThat > > if this( instanceOfThat ) then > puts( "class = this\n") > end if > > if that( instanceOfThat ) then > puts( "class = that\n") > end if > > Lo and behold, it belongs to both types! Yep, but there are ways around this, complex time-based ways, but hey, who needs that anyways? You should know what a class' type is, CLASS! :p > The other major problem is that you lose the 'type' > when it's passed through > a 'generic' type. For example: > > mySpecialType myInstance > > procedure foo( object bar ) > end procedure > > If you call: > > foo( myInstance ) > > and myInstance is essentially cast into an object. Use foo( mySpecialType ) instead. What? Those other Euphoria class libraries don't do all this stuff eighter. Atleast using types is somewhat more *real* instead of haing to call 'member(MyClass,"myMember",{args})', you waste loads of memory using integer handles to classes, and it's all slow too for they have to be looked up in a sequence, etc... > -- David Cuny But hey, I only gave an example... You can do other cool things with types too... Here's a couple of more things that you didn't know Eu could do, all having to do with datatypes somehow; Did You Know... -*-*-*-*-*-*-*- - That you can get the address, in memory, of a function or procedure? And peek at the machine code the function or procedure embeds in memory? And call them from inline ASM or Machine Code? That's right! You do it like this; atom pointer pointer = call_back(routine_id("wait_key")) call(pointer) -- call() is used to call machine code So essentially, you can 'jmp' to a Eu routine in ASM, 'call' one in ASM, and use call() to call it as machine code. Machine COde freaks can even try to keep peeking at Euphoria's internal routines starting from the address returned by call_back() untill you encounter a RET machine code. This way, you can get the Machine Code source of Euphoria runtime-routines! - That you can re-write Euphoria's built-in routines by providing alternatives of your own, written in C? To do this, you must read the previous explenation, and build upon it. If call_back(routine_id("pixel")) returns an adress, simply poke() new machine code of your own to that address, and voila! Euphoria now uses your own code! How to get the machine code if you don't know ASM of Machine code? Simply get a C compiler, write the replacement routine in C, compile it, and then get the machine code of that routine and paste it into your euphoria program, to be poked into memory! You can even do it easier. Write a DLL wich contains the replacement routines you want to use. Get the address of those routines, peek() all the machine code, and then poke() them back into memory at the offsets of Euphoria routines. Remember, MTS is a certified Hacker/Cracker, so if you don't know what you're doing, don't. - You can pass a sequence by address, even to C/C++ programs as C arrays? How do you do this? Simple. Poke each member of a sequence into memory, whetever type it may have, as an integer wich represents the adress of that member. Example; sequence s s = {"Hello!",10,3.14} atom seq, foo seq = allocate(length(s)*4) for i = 1 to length(seq) do if sequence(seq[i]) then poke4(seq+i,allocate_string(seq[i])) elsif atom(seq[i]) then poke4(seq+i,float32_to_bytes(seq[i])) elsif integer(seq[i]) then poke4(seq+i,seq[i]) end if end for Now 'seq' is a pointer to a sequence!!! >From a C program, you do this to receive a sequence from a Euphoria program; void *seq; seq = malloc(LENGTH_OF_SEQUENCE_IN_BYTES); seq = get_a_sequence_from_euphoria(); /* Now to access/update the sequence; */ puts((char*)seq+4); printf("%f",(float*)seq+8); printf("%d",(int*)seq+12); Well, maybe I'm explaining this sloppy, but you should be able to get the prinicples. The C code wasn't tested, for example, but any C coder now knows how to use/create/pass/store/refference/exchange Euphoria sequences. Robert should build-in this technique in Euphoria though.. like a peek/poke_sequence() or something... See? No reason for C code not being able to use Eu, and Eu not being able to use C code wich uses Sequences.... Mike The Spike