Pages: « 1 2
  Print  
Author Topic: Object member functons  (Read 23953 times)
Offline (Female) IsmAvatar
Reply #15 Posted on: January 07, 2011, 07:00:49 pm

LateralGM Developer
LGM Developer
Location: Pennsylvania/USA
Joined: Apr 2008
Posts: 877

View Profile Email
Suppose f1 and f2 return 0, and obj2.array[0,0] = 0.

The statement would be equivalent to the following:
obj1.a = 0;

As you can see, I'm totally lost, which is why someone needs to explain this one to me.
Logged
Offline (Male) RetroX
Reply #16 Posted on: January 07, 2011, 08:54:16 pm

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
It would be expansion hell.

Now, that makes sense, I guess.

with() will do what it will, I suppose.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) RetroX
Reply #17 Posted on: January 07, 2011, 08:55:20 pm

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Suppose f1 and f2 return 0, and obj2.array[0,0] = 0.

The statement would be equivalent to the following:
obj1.a = 0;

As you can see, I'm totally lost, which is why someone needs to explain this one to me.
f1() is performed for all objects, f2() is performed for objects, and set to a for all objects.  It would be expansion hell, and really, in the end, the last one executed would actually be stored.  That's why it's a bad idea to make one variable operate on multiple objects.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) polygone
Reply #18 Posted on: January 08, 2011, 07:02:47 am

Contributor
Location: England
Joined: Mar 2009
Posts: 794

View Profile
Suppose f1 and f2 return 0, and obj2.array[0,0] = 0.

The statement would be equivalent to the following:
obj1.a = 0;

As you can see, I'm totally lost, which is why someone needs to explain this one to me.
As RetroX has just said, because what if there are multiple instances of obj1 and obj2? You can't run things so straight then. f1 and f2 might return different values depending on the instance it was executed with.

RetroX has stated as a solution:
Quote
in the end, the last one executed would actually be stored
But to me this is really not nice and I don't think it is a viable solution. Which is why I mentioned previously:

Code: (EDL) [Select]
obj.a = obj.f1();People would naturally expect this to work like:

Code: (EDL) [Select]
with (obj)
{
  a = f1();
}
But it would instead work like:

Code: (EDL) [Select]
global var ______ENIGMATEMP;
with (obj)
{
  ______ENIGMATEMP = f1();
}
with (obj)
{
  a = ______ENIGMATEMP;
}
« Last Edit: January 08, 2011, 07:11:31 am by polygone » Logged
I honestly don't know wtf I'm talking about but hopefully I can muddle my way through.
Offline (Unknown gender) r9k
Reply #19 Posted on: January 08, 2011, 08:57:17 am
Member
Joined: Aug 2010
Posts: 25

View Profile
Suppose f1 and f2 return 0, and obj2.array[0,0] = 0.

The statement would be equivalent to the following:
obj1.a = 0;

As you can see, I'm totally lost, which is why someone needs to explain this one to me.
As RetroX has just said, because what if there are multiple instances of obj1 and obj2? You can't run things so straight then. f1 and f2 might return different values depending on the instance it was executed with.

RetroX has stated as a solution:
Quote
in the end, the last one executed would actually be stored
But to me this is really not nice and I don't think it is a viable solution. Which is why I mentioned previously:

Code: (EDL) [Select]
obj.a = obj.f1();People would naturally expect this to work like:

Code: (EDL) [Select]
with (obj)
{
  a = f1();
}
But it would instead work like:

Code: (EDL) [Select]
global var ______ENIGMATEMP;
with (obj)
{
  ______ENIGMATEMP = f1();
}
with (obj)
{
  a = ______ENIGMATEMP;
}

There are no ______ENIGMATEMP. The parser will just convert "obj." to enigma_varaccess and everything will be as fast as a c++ member function call and assignation.
Logged
Offline (Male) Josh @ Dreamland
Reply #20 Posted on: January 08, 2011, 11:22:38 am

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
Unfortunately, r9k, it's not that simple. It's not as simple as polygone's suggestion, either.
The only way to implement this cleanly is too flawed to implement:
Code: (GML) [Select]
obj1.func()
becomes
Code: (C++) [Select]
(enigma::with_iter(obj1), func())
When the expression starts, it will be as though it was executed for obj1. When the expression ends, the with_iter will destruct, and the code will be back in its original scope. However, this will fail catastrophically:
Code: (GML) [Select]
obj1.func(obj2.func())
Both will be executed for obj2. This is because at the beginning of the expression, obj1 is pushed as the current instance, then obj2 is as well.

I haven't yet found a way around this.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) RetroX
Reply #21 Posted on: January 08, 2011, 12:51:20 pm

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Polygone, I wasn't suggesting any solution.  I was mentioning what would actually happen.

I think that, like obj.x, obj.func() should only operate on one instance.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Unknown gender) luiscubal
Reply #22 Posted on: January 08, 2011, 01:36:01 pm
Member
Joined: Jun 2009
Posts: 452

View Profile Email
Perhaps you should have with_iter use some sort of stack?

(enigma::push_with(obj1), func(enigma::push_with(obj2), func(), enigma::pop_with()), enigma::pop_with());
Logged
Offline (Male) RetroX
Reply #23 Posted on: January 08, 2011, 05:33:38 pm

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
That was actually what he mentioned doing, I believe.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Josh @ Dreamland
Reply #24 Posted on: January 09, 2011, 12:41:29 am

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
That's how with_iter works, luis. Issue is, you just passed func() the return value of enigma::pop_with().
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Unknown gender) luiscubal
Reply #25 Posted on: January 09, 2011, 08:09:54 am
Member
Joined: Jun 2009
Posts: 452

View Profile Email
I see.
What about:
Code: [Select]
(enigma::push_with(obj1), enigma::pop_with((enigma::push_with(obj2), enigma::pop_with(func()))))where pop_with(K) returns K.

If with_iter is already a stack, then I don't see how "obj1.func(obj2.func())" would fail. After all:
Code: [Select]
(enigma::with_iter(obj1), func((enigma::with_iter(obj2), func())))The inner func is called after obj2 is pushed to the stack, and before it is poped, so it is applied to obj2 alone, the top() of the stack(assuming a STL stack)
The outer func is called after obj1 and obj2 are pushed to the stack, but obj2 was already poped, so obj1 is the top() of the stack and therefore func() is applied to obj1 alone.

So I think it would work. That is, unless C++ sequence points are playing tricks on me.
Logged
Offline (Male) Josh @ Dreamland
Reply #26 Posted on: January 09, 2011, 01:12:24 pm

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
The stack is pushed twice at the beginning of the expression in my example, then popped twice right after.

pop_with(K) was what I was considering. But it wasn't until you posted it again that I realized I was thinking about it incorrectly. My first instinct was that there'd be no way to instantiate a template because the return type of func() can't be gathered from &func. But now I see that a simple template function would have done it.

Code: (C++) [Select]
inline template<typename any> any& pop_with(any &r) { return r; }

So thank you, Luis; the simplicity of the matter escaped me.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Pages: « 1 2
  Print