#2109 Dynamic invoke and arity of method

bedla Wed 13 Mar 2013

Hi,

I have found some (for me) strange behaviour which I dont understand. I have following code. I am expectiong UnknownSlotErr while dynamic invocation of doIt, but nothing is thrown.

If I compare result of fits calls of "slot methods" and "local funcs" they are different. I thing that I am missing something. Could you explain it to me? :]

Thx

Ivos

class Main
{
  static Void main()
  {
      m := Main()
      m->doIt(123)

      echo("---")

      echo(Main#testM)
      echo(Main#testM.func)
      echo(Main#testM.signature)
      echo(Main#testM.typeof)
      echo(Main#testM.func.typeof)

      echo("---")

      echo(Main#doIt)
      echo(Main#doIt.func)
      echo(Main#doIt.signature)
      echo(Main#doIt.typeof)
      echo(Main#doIt.func.typeof)

      echo("--- method")
      echo(Main#testM.typeof.fits(Main#doIt.typeof)) // true
      echo(Main#doIt.typeof.fits(Main#testM.typeof)) // true

      echo("--- func")
      echo(Main#testM.func.typeof.fits(Main#doIt.func.typeof)) // true
      echo(Main#doIt.func.typeof.fits(Main#testM.func.typeof)) // true

      echo("--- method 2")
      echo(Main#testM.typeof.fits(Main#testM2.typeof)) // true
      echo(Main#testM2.typeof.fits(Main#testM.typeof)) // true

      echo("--- func 2")
      echo(Main#testM.func.typeof.fits(Main#testM2.func.typeof)) // true
      echo(Main#testM2.func.typeof.fits(Main#testM.func.typeof)) // true

      f1 := |Int a| { echo("f1($a)")}
      f2 := |Int a, Int b| { echo("f2($a, $b)")}
      echo("--- local func")
      echo(f1.typeof.fits(f2.typeof)) // true
      echo(f2.typeof.fits(f1.typeof)) // false
  }

  Void testM2(Int num, Int num2) { echo("Why $num, $num2") }
  Void testM(Int num) { echo("Why $num") }

  Void doIt() { echo("I did it, Again") }

}

brian Wed 13 Mar 2013

You can pass more parameters to a function than its arity. So if a callback provides the value and index, then you can choose to ignore the index:

list := [1, 2, 3]
list.each |val| { .... }
list.each |val, index| { .... } 

Also see http://fantom.org/doc/docLang/Functions.html#arityCompatibility

bedla Wed 13 Mar 2013

Thx, I understand this.

But what I dont understand is difference between fits on local funcs and slot funcs.

At following example I have two local functions with arity 1 and 2. Result of fits method call on local funcs is true, false - so right answer when considering arity. But when I get method's funcs with same arity result is true, true.

Thx for explanation :]

class SlotArity
{
  static Void main()
  {
      f1 := |Int a| { echo("f1($a)")}
      f2 := |Int a, Int b| { echo("f2($a, $b)")}
      echo("--- local func")
      echo(f1.typeof.fits(f2.typeof)) // true
      echo(f2.typeof.fits(f1.typeof)) // false
      m1 := #testM1.func
      m2 := #testM2.func
      echo("--- slots func")
      echo(m1.typeof.fits(m2.typeof)) // true
      echo(m2.typeof.fits(m1.typeof)) // true
  }
  Void testM2(Int num, Int num2) { echo("Why $num, $num2") }
  Void testM1(Int num) { echo("Why $num") }
}

And sorry for confusion in last test case O:]

katox Wed 13 Mar 2013

I think I can see the point of bedla's post:

s1 := Str#contains.func
s2 := Str#all.func

s1.typeof.fits(s2.typeof) // -> true (sys::Func fits sys::Func)
s2.typeof.fits(s1.typeof) // -> true (sys::Func fits sys::Func)

echo (s1.params) // -> [sys::Str this, sys::Str s]
echo (s2.params) // -> [sys::Str this, |sys::Int,sys::Int->sys::Bool| c]

That is that the method conversion via func call effectively clears the type info for fits.

Localy defined functions work differently (no type info is lost)

f2.typeof.fits(f1.typeof)) // -> false (|Int| doesn't implement |Int,Int|)

brian Wed 13 Mar 2013

Yeah, all the Method.func can return is just a generic Func type because otherwise each instance of Method would be required to have a different return type.

bedla Wed 13 Mar 2013

Ok, thx for explanation :]

SlimerDude Tue 10 Oct 2017

Note this behaviour has been updated / fixed in Fantom 1.0.70.

See topic Method.func.typeof is not parameterized for details.

Login or Signup to reply.