[SOLVED] How do I override base methods in a class ?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
julka
Posts: 14
Joined: 06 Feb 2014, 02:31

[SOLVED] How do I override base methods in a class ?

11 Feb 2014, 06:04

What I want to achieve, is extending the functionality of base method.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

A := new product("cheese")
B := new purchase(A)
B.price:=1

A.msgbox()
B.msgbox()
exitapp

class product
{	
	title:=""
	
	__New(title:="")
	{	
		this.title:=title
		return this
	}
	
	info()
	{
		temp:= this.title ;%
		return temp
	}
	msgbox()
		msgbox % this.info() ;%
}

class purchase extends product
{
	price:=0
	__New(r_product:="")
	{
		if IsObject(r_product)
			this:=r_product
		return this
	}
        info() ; override base.info()
;I want to extend the funtionality of this method, so it would include the data, which exists only in purchase
        {
                temp:=base.info()
                temp:= temp . "`n" . this.price ;%
                return temp
        }
        msgbox() ; override base.msgbox()
                msgbox this.info()
}
How can I override a base method in a class ?

I tried the example in manual Here but its not exactly what I need and did not work.
I also tried looking for examples/tutorials on the Net but I found zero results regarding this topic.

Some other things I tried:
Tut about using objects couldn't help :(.

Any help would be greatly appreciated,
Thank you
lexikos
Posts: 9587
Joined: 30 Sep 2013, 04:07
Contact:

Re: How do I override base methods in a class ?

11 Feb 2014, 20:14

julka wrote:I tried the example in manual Here but its not exactly what I need and did not work.
That's not a manual! It's a preliminary discussion from before class syntax was implemented.

But actually, it does mention the solution: call base.Method().

Your main problem appears to be that your msgbox method definitions are invalid - they are missing braces around the method body.
julka
Posts: 14
Joined: 06 Feb 2014, 02:31

Re: How do I override base methods in a class ?

12 Feb 2014, 01:06

How

Code: Select all

base.method()
is used though? In docs u reffered me to it's only mentioned that it won't work, or can't I read?

I want to override base methods like in c# or c++.
When I call

Code: Select all

A.msgbox()
from my original post, the program should "print" cheese - only title, however, when

Code: Select all

B.msgbox()
is called, it should "print" cheese{newline}1 - title and price.

Sorry for no comments in the code itself to explain that point.

Thank you for your help!
julka
Posts: 14
Joined: 06 Feb 2014, 02:31

Re: How do I override base methods in a class ?

12 Feb 2014, 04:13

After some chatting on IRC channel with TLM, I understood, where the problem laid.

My methods were overriden twice: once in class definition and another time with line

Code: Select all

this:=r_product
that copied not only the values of the vars, but also the methods.

Good old spaghetti code prevails:

Code: Select all

A := new product("cheese")
B := new purchase(A)
B.price:=1

A.msgbox()
B.msgbox()
exitapp

class product
{	
	title:=""
	
	__New(title:="")
	{	
		this.title:=title
		return this
	}
	
	info()
	{
		temp:= this.title ;%
		return temp
	}
	msgbox()
	{
		msgbox % this.info() ;%
	}
}

class purchase extends product
{
	price:=0
	__New(r_product:="")
	{
		if IsObject(r_product)
		{
			this.title:=r_product.title
                        ;other stuff to copy
		}
		return this
	}
    info()
    {
            temp:=base.info()
            temp:= temp . "`n" . this.price ;%
            return temp
    }
    
	msgbox()
    {
        msgbox % this.info() ;%
    }
}
I also missed a percent sign in my original post :oops:
Also, if you noticed I'm using base.method() even in original post.
Last edited by julka on 12 Feb 2014, 04:18, edited 1 time in total.
User avatar
trismarck
Posts: 506
Joined: 30 Sep 2013, 01:48
Location: Poland

Re: How do I override base methods in a class ?

12 Feb 2014, 04:15

Not sure if this is how it is supposed to work:

Code: Select all

	A := new product("cheese")
	B := new purchase(A)
		; not sure how should this work
		; 
	B.price:=1

	A.msgbox()
		; print info about object A
		; 
	B.msgbox()
		; makes little sense, because B points to the same
		; object A points to
		; 
	C := new purchase("onion")
	C.msgbox()
	exitapp
return

class product
{   
   __New(title:="") 
   {   
	  this.title:=title
	  return this
   }
	info()
   {
	  temp:= this.title ;%
	  return temp
   }
   msgbox()
   {
	  msgbox % this.info() ;%
  }
}

class purchase extends product
{
   price:=0
   __New(r_product:="")
   {
		if(r_product.__Class = "product")
		{
			; add all necessary keys from the overriding class 
			; to the object? (~price := 0)
			; 
			for key, val in this
				r_product[key] := val
			
			; change the base of the object
			; 
			r_product.base := purchase
			
			; return the object with 'overridden methods'
			; 
			return r_product
		}
		else
		{
			; call the constructor of the to be overridden class
			; 
			base.__New.(this, r_product)
				; r_product is the title
				; 
		}
   }
	info() ; override base.info()
	;I want to extend the funtionality of this method, so it would 
	; include the data, which exists only in purchase
	{
		;temp:=base.info()
		temp:=base.info.(this)
		temp:= temp . "`n" . this.price ;%
		return temp
	}
	msgbox() { ; override base.msgbox()
			;msgbox, this.info()
			msgbox, % this.info()
	}
}
julka
Posts: 14
Joined: 06 Feb 2014, 02:31

Re: [SOLVED] How do I override base methods in a class ?

12 Feb 2014, 05:16

trismarc, your code edited the variable being passed to constructor (that's it, A) - it got the properties of purchase.

It's due to undocumented fact that variables reffering to objects are only pointers, I think.

You gave me idea how to get rid of spaghetti code though:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

A := new product("cheese")
B := new purchase(A)
B.price:=1

A.msgbox()
B.msgbox()

C := new purchase("onion")
C.msgbox()
exitapp

class product
{  
   __New(title:="")
   {  
      this.title:=title
      return this
   }
    info()
   {
      temp:= this.title ;%
      return temp
   }
   msgbox()
   {
      msgbox % this.info() ;%
  }
}

class purchase extends product
{
   price:=0
   __New(r_product:="")
   {
        if(r_product.__Class = "product")
        {
			r_purchase:=r_product.clone() ; << ensures that original r_product isn't changed
            ; add all necessary keys from the overriding class
            ; to the object? (~price := 0)
            ;
            for key, val in this
                r_purchase[key] := val
           
            ; change the base of the object
            ;
            r_purchase.base := purchase
           
            ; return the object with 'overridden methods'
            ;
            return r_purchase
        }
        else
        {
            ; call the constructor of the to be overridden class
            ;
            base.__New.(this, r_product)
                ; r_product is the title
                ;
        }
   }
    info()
    {
        ;temp:=base.info()
        temp:=base.info.(this)
        temp:= temp . "`n" . this.price ;%
        return temp
    }
    msgbox() { ; override base.msgbox()
            ;msgbox, this.info()
            msgbox, % this.info()
    }
}
User avatar
trismarck
Posts: 506
Joined: 30 Sep 2013, 01:48
Location: Poland

Re: [SOLVED] How do I override base methods in a class ?

12 Feb 2014, 05:40

julka wrote:trismarc, your code edited the variable being passed to constructor (that's it, A) - it got the properties of purchase.
Yes, I've did that on purpose, because I didn't know if the goal was to clone the object or to just 'assign a new base' to the existing object and thus override methods of that existing object.

Note also, that the .clone() method creates a _shallow_ (vs deep) copy of the object. I.e. if A has values that store references other _objects_, cloned values of B will reference to those objects as well, unless a deep clone method is used.

Also, because we're operating on the instance object of the _product_ class, those:

Code: Select all

base.__New.(this, r_product)
temp:=base.info.(this)
can be changed to:

Code: Select all

base.__New(r_product)
temp:=base.info()
This doesn't actually matter as base always passes this, not this.base.base[.base]*.
lexikos
Posts: 9587
Joined: 30 Sep 2013, 04:07
Contact:

Re: How do I override base methods in a class ?

12 Feb 2014, 20:33

julka wrote:How base.method() is used though? In docs u reffered me to it's only mentioned that it won't work, or can't I read?
The docs say no such thing. If the name of the method is "method" and there are no parameters, it is used as-is.
I want to override base methods like in c# or c++.
That is funny, since C# is the same: base.method(). That's no coincidence. C++ is something like BaseClassName::method() - the closest AutoHotkey equivalent would be BaseClassName.method.(this).
julka wrote:... this:=r_product that copied not only the values of the vars, but also the methods.
It copies neither. It makes this refer to the same object as r_product, and the original object which this referred to is unaffected. However, since you return a value from __new, the original object is discarded.
julka wrote:It's due to undocumented fact that variables reffering to objects are only pointers, I think.
Not undocumented.
An object reference is a pointer or "handle" to a particular object. Like strings and numbers, object references can be stored in variables, passed to or returned from functions and stored in objects. After copying a reference from one variable to another as in x := y, both variables refer to the same object.

Source: near the top of http://ahkscript.org/docs/Objects.htm

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5 and 292 guests