Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Thoughts on pointers/references in Lua++:
- I spent a lot of time thinking about this last night and so far, the conclusion I've reached is that it simply does not make sense to have pointers or references in this language. At least not in the traditional sense.
- I can imagine *at least* one way to implement a pointer in this language: as a function. It's not ideal but bear with me...
- Say I want to write something like this:
- double :: (int& a) {
- *a *= 2;
- }
- b : int = 42;
- double(&b);
- That could translate into this (potentially):
- function double(a)
- a(a() * 2)
- end
- local b = 42
- double(function(x)
- if x then b = x end
- return b
- end)
- It's a bit .. shitty, having to use a closure to implement a pointer like this but it seems like the simplest way to do it to me, if not the "only" way. I mean the alternative would be, I guess, storing everything in invisible arrays that I can pass around... But then *all* data would take a performance hit. The advantage of this approach is that its performance hit is actually relatively minimal. In the case of a struct/array reference/pointer, a closure wouldn't even be needed since they're already reference types, whereas, if it were being used as a value type, extra code would be needed to implement the copying.
- This is all fine and dandy but then, what about pointer arithmetic? The only case where it would seem to even make sense is in the case of a pointer to an array element. In all other cases the behavior would likely just be undefined? I mean.. eh..
- Even if it makes sense though, how? It would require a different implementation...
- One theoretical implemention may be to use a function that takes in an expression at runtime and generates the pointer closure from that... But like... that's kind of insane and would be pretty damn slow... idk.
- What I meant by that:
- a : []int = [ 0, 2, 6, 4, 9, 5 ];
- b := &a[0];
- c := &a[5];
- for b != c {
- *b *= 4;
- b++;
- }
- And in Lua:
- local a = { 0, 2, 6, 4, 9, 5 }
- local b = function(x)
- if x then a[0] = x end
- return a[0]
- end
- local c = function(x)
- if x then a[5] = x end
- return a[5]
- end
- (*at this point, I realize that a pointer would actually necessarily be a table with overridden __eq and __call so that comparing pointers would be possible; maybe a table/metatable-based approach would simplify things and be better, but it would also be a further performance hit*)
- function __ptr_expr_*unique-id*(*something*) -- obviously *unique-id* is some expr identifier from the copiler
- -- so... uh... shit. maybe i need table-based pointers to be able to do this at all? er... hmmh... yeah.............. (dotdotdot)
- end
- while b ~= c do -- pretend that comparison will work; see just above
- b(b() * 4)
- b = __ptr_expr_*unique-id*(*something?*)
- end
- Yeah idk where exactly I was going with that... meh. Seems pointless to try since I really don't think pointer arithmetic makes sense in this language... but I'll be very eager to hear your thoughts...
- And if we don't do pointer arithmetic, why do pointers? Seems like the only use-cases would be modifying function parameters and having references to array elements and struct members...
- But like.. eh.. is that even that useful? Idk
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement