Jump to content


[AHK_L] [1.1.5.3] nonexistent class function is no error


  • Please log in to reply
4 replies to this topic

#1 nfl

nfl
  • Guests

Posted 01 December 2011 - 12:41 AM

Hi,

I just wondered, why a not defined function in a class does not come up with a MsgBox like "Call to nonexistent function."

Example code:
#Warn
t := new test
t.func1()
t.func2() ; no error
func1()
func2() ; error

class test {
	func1() {
		msgbox bla
	}
}

func1() {
	msgbox bli
}
Is this intented behavior or is there something wrong?

Regards, nfl

#2 infogulch

infogulch
  • Moderators
  • 717 posts

Posted 01 December 2011 - 12:59 AM

This is because objects' methods are references to functions and can be manipulated dynamically, making error detection nearly impossible at load time.

E.g. modify your code:

#Warn
t := new test
t.func1(1)

t.func2 := t.func1
t.func2(2) ;this calls correctly

t.func1 := ""
t.func1(3) ;this doesn't get called at all

class test {
   func1(x) {
      msgbox %x%
   }
}
Shows "1" then "2" but not "3"

Also don't forget about the __call meta function, which would complicate error detection further.

iirc there has been talk about run-time errors when calling nonexistent methods (possibly including exceptions), but I don't remember the outcome of the conversation.

#3 jethrow

jethrow
  • Fellows
  • 2549 posts

Posted 01 December 2011 - 01:36 AM

Also don't forget about the __call meta function ...


Plus, I would argue that objects should build their own error handling:
#Warn
t := new test
t.func1()
t.func2() ; user defined runtime error



class Warn {
   __Call(p) {
      MsgBox, 262420, , % "Call to NonExistent Method`n`n  Class:`t" this.__Class 
                     .    "`n  Func:`t" p "`n  Line:`t" Exception("",-1).line
                     .    "`n`nContinue Script?"
      IfMsgBox, No
         ExitApp
   }
}

class test extends Warn {
   func1() {
      msgbox bla
   }
}

func1() {
   msgbox bli
}

EDIT - corrected MsgBox logic - thanks nimda.

#4 nimda

nimda
  • Members
  • 4301 posts

Posted 01 December 2011 - 01:39 AM

That's evil :twisted: :p

Continue Script?
> Yes
> > Script exits



#5 nfl

nfl
  • Guests

Posted 01 December 2011 - 09:26 AM

Hmm I haven't thought about the references thing. That makes sense, of course.
Added jethrows __Call function to my classes for now, thanks :)