#909 Problem compiling script

fury Wed 13 Jan 2010

I tried to compile some code available on this site, specifically:

class Foo
{ 
  static Int add(Int a, Int b) { return a + b }
  static Void main() { m := &add(3); echo(m(4)) }
}

The compilation fails with this message:

x.fan(4,29): Expected expression, not &

The output of fan -version is:

Fantom Launcher Copyright (c) 2006-2009, Brian Frank and Andy Frank Licensed under the Academic Free License version 3.0

Java Runtime:

java.version:    1.6.0_17
java.vm.name:    Java HotSpot(TM) Client VM
java.vm.vendor:  Sun Microsystems Inc.
java.vm.version: 14.3-b01
java.home:       C:\Program Files\Java\jre6
fan.platform:    win32-x86
fan.version:     1.0.48

And on a separate note, is it possible to create a function that takes another function as a parameter? I couldn't find anything about this on the documentation.

DanielFath Wed 13 Jan 2010

There is nothing wrong with your enviroment, that feature just got removed at 1.0.47 ( Curry removal) but Docs haven't been updated yet. Do you have link to that part of the doc so they can fix it up?

What m: = add(3) was to bind one parameter making m |Int a->Int| { return a+3}.

So your code in Fantom 1.0.48 should look.

class Foo
{ 
  static Int add(Int a, Int b) { return a + b }
  static Void main() 
  {
     m := |Int a->Int| {add(a,3)}
     echo(m(4)) 
  }
}

Second question. Yeah. Keep in mind Method is just a wrapper for Func. Anything Method can do, so can Func.

class FuncTest
{
  static Int add(Int a, Int b) { return a + b }

  public static Void main(Str[] args)
  {
    //Function result becomes paramter
    func := |Int a, Int b-> Int | {return a+b}
    sub := |Int a->Int| {return a}
    result := func(sub(2),3) 
    echo(result)

    //Function as parameter
    print := | |Int->Int| printfunc, Int what | {echo(printfunc(what))}
    print(sub,3)
  }
}

#Output:
   5
   3

brian Thu 14 Jan 2010

Sometimes I miss the & operator. Although I think we can live without for 1.0.

I've been using more the Scala style _ to do partial application in SkyFoundry's query language, and I'm really digging that. That might be a better way to handle it Fantom eventually.

fury Thu 14 Jan 2010

After I saw the answers to my callback question, would it be safe to assume that # is the new & operator?

DanielFath Thu 14 Jan 2010

No. Str# yields type Sys::Str and you it was reflection katox used to find the func() of a Method (see sys::Method.func()). Tompalmer solution is the most Fantom like.

Though to be honest the whole it-, with- blocks confuse the crap out of me.

brian Thu 14 Jan 2010

After I saw the answers to my callback question, would it be safe to assume that # is the new & operator?

I think in a lot of cases #slot.func is used to replace the old & operator.

Although, it-blocks are also a good easy to write easy callbacks.

Login or Signup to reply.