B4J Question Java version and Lambda Expressions

Roycefer

Well-Known Member
Licensed User
Longtime User
Using lambda expressions in inline Java code results in the following compile-time error:
B4X:
B4J version: 3.02
Parsing code.    (0.00s)
Compiling code.    (0.02s)
Compiling generated Java code.    Error
B4J line: 33
End Sub
javac 1.8.0_40
src\b4a\example\pbtest\main.java:125: error: lambda expressions are not supported in -source 1.5
    Calc c1 = (a,b)->{System.out.println(a + "+" + b); return a+b;};
                  ^
  (use -source 8 or higher to enable lambda expressions)
1 error

As you can see, I'm using Java 8 Update 40. Why is inline Java code being compiled using "-source 1.5"?
 

wl

Well-Known Member
Licensed User
Longtime User
Which brings me to the following question ...

why not implement Lambda expressions directly into B4X ?
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
As far as I know, Android is still stuck at the Java 7 language level, which doesn't support lambda expressions. Objective C uses blocks, instead of lambdas. It wouldn't be possible to just simply provide a wrapper for Java 8-style lambdas. I suspect Erel would have to build in lambda support at the B4X language level and then implement it separately for each of the three parsers. This sounds like a huge project for such a specialized function. I suspect inheritance is probably a much higher priority in B4X language development and even that's probably decades away.

However, the precursor to lambda expressions, function objects, could be hacked together by B4X programmers today with the language as it now stands. The CallSub() function gives us a lot of power.
 
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
Hi,

Indeed I agree in such a situation it would need lots of work.

The CallSub() is indeed powerful but as it is not strongly typed may introduce a lot of mayhem... I would rather like to have real "function pointers" / delegates or whatever you would call them.
 
Upvote 0
Top