44
55sealed abstract class Type (val code : Char , val prim : String , val ref : String ) {
66 def boxed : String = ref
7+ def unbox (expr : String ) = {
8+ s " scala.runtime.BoxesRunTime.unboxTo ${prim.head.toUpper + prim.tail}( $expr) "
9+ }
10+ def box (expr : String ) = {
11+ s " scala.runtime.BoxesRunTime.boxTo ${ref.head.toUpper + ref.tail}( $expr) "
12+ }
713}
814object Type {
915 case object Boolean extends Type ('Z' , " boolean" , " Boolean" )
@@ -218,15 +224,15 @@ object CodeGen {
218224 val suffix = specializedSuffix(tparamNames, tps)
219225 val List (r) = tps
220226 val applyMethodBody = if (r == Type .Void ) s " apply $suffix(); return scala.runtime.BoxedUnit.UNIT; "
221- else s " return ( ${r.ref} ) apply $suffix(); "
227+ else s " return ${r.box( s " apply $suffix() " )} ; "
222228 val code = s """
223229 | $copyright
224230 |
225231 | $packaging
226232 |
227233 |@FunctionalInterface
228234 |public interface JFunction0 $suffix extends JFunction0 {
229- | abstract ${r.prim} apply $suffix();
235+ | ${r.prim} apply $suffix();
230236 |
231237 | default Object apply() { $applyMethodBody }
232238 |}
@@ -239,16 +245,16 @@ object CodeGen {
239245 val tparamNames = function1Spec.map(_._1)
240246 val suffix = specializedSuffix(tparamNames, tps)
241247 val List (t, r) = tps
242- val applyMethodBody = if (r == Type .Void ) s " apply $suffix(( ${t.ref} ) t ); return scala.runtime.BoxedUnit.UNIT;"
243- else s " return ( ${r.ref} ) apply $suffix(( ${t.ref} ) t) ;"
248+ val applyMethodBody = if (r == Type .Void ) s " apply $suffix( ${t.unbox( " t " )} ); return scala.runtime.BoxedUnit.UNIT; "
249+ else s " return ${r.box( s " apply $suffix( ${t.unbox( " t " )} ) " )} ; "
244250 val code = s """
245251 | $copyright
246252 |
247253 | $packaging
248254 |
249255 |@FunctionalInterface
250256 |public interface JFunction1 $suffix extends JFunction1 {
251- | abstract ${r.prim} apply $suffix( ${t.prim} v1);
257+ | ${r.prim} apply $suffix( ${t.prim} v1);
252258 |
253259 | default Object apply(Object t) { $applyMethodBody }
254260 |}
@@ -260,16 +266,16 @@ object CodeGen {
260266 val tparamNames = function2Spec.map(_._1)
261267 val suffix = specializedSuffix(tparamNames, tps)
262268 val List (t1, t2, r) = tps
263- val applyMethodBody = if (r == Type .Void ) s " apply $suffix(( ${t1.ref} ) v1, ( ${t2.ref} ) v2 ); return scala.runtime.BoxedUnit.UNIT;"
264- else s " return ( ${r.ref} ) apply $suffix(( ${t1.ref} ) v1, ( ${t2.ref} ) v2 );"
269+ val applyMethodBody = if (r == Type .Void ) s " apply $suffix( ${t1.unbox( " v1 " )} , ${t2.unbox( " v2 " )} ); return scala.runtime.BoxedUnit.UNIT; "
270+ else s " return ${r.box( s " apply $suffix( ${t1.unbox( " v1 " )} , ${t2.unbox( " v2 " )} " )} ); "
265271 val code = s """
266272 | $copyright
267273 |
268274 | $packaging
269275 |
270276 |@FunctionalInterface
271277 |public interface JFunction2 $suffix extends JFunction2 {
272- | abstract ${r.prim} apply $suffix( ${t1.prim} v1, ${t2.prim} v2);
278+ | ${r.prim} apply $suffix( ${t1.prim} v1, ${t2.prim} v2);
273279 |
274280 | default Object apply(Object v1, Object v2) { $applyMethodBody }
275281 |}
@@ -288,11 +294,11 @@ object CodeGen {
288294 private def function0SpecMethods = {
289295 val apply = specialized(" apply" , function0Spec) {
290296 case (name, List (r)) =>
291- val applyCall = s " apply(); "
292- def body = if (r == Type .Void ) applyCall else s " return ( ${r.ref} ) $ applyCall"
297+ val applyCall = s " apply() "
298+ def body = if (r == Type .Void ) applyCall else s " return ${r.unbox( applyCall)} "
293299 s """
294300 |default ${r.prim} $name() {
295- | $body
301+ | $body;
296302 |}
297303 | """ .stripMargin.trim
298304 }
@@ -317,11 +323,11 @@ object CodeGen {
317323 private def function1SpecMethods = {
318324 val apply = specialized(" apply" , function1Spec) {
319325 case (name, List (t1, r)) =>
320- val applyCall = s " apply((T1) (( ${t1.ref} ) v1)); "
321- def body = if (r == Type .Void ) applyCall else s " return ( ${r.ref} ) $ applyCall"
326+ val applyCall = s " apply((T1) ${t1.box( " v1 " )} ) "
327+ def body = if (r == Type .Void ) applyCall else s " return ${r.unbox( applyCall)} "
322328 s """
323329 |default ${r.prim} $name( ${t1.prim} v1) {
324- | $body
330+ | $body;
325331 |}
326332 | """ .stripMargin.trim
327333 }
@@ -348,12 +354,12 @@ object CodeGen {
348354 private def function2SpecMethods = {
349355 val apply = specialized(" apply" , function2Spec) {
350356 case (name, List (t1, t2, r)) =>
351- val applyCall = s " apply((T1) (( ${t1.ref} ) v1) , (T2) (( ${t2.ref} ) v2)); "
352- def body = if (r == Type .Void ) applyCall else s " return ( ${r.ref} ) $ applyCall"
357+ val applyCall = s " apply((T1) ${t1.box( " v1 " )} , (T2) ${t2.box( " v2 " )} ) "
358+ def body = if (r == Type .Void ) applyCall else s " return ${r.unbox( applyCall)} "
353359
354360 s """
355361 |default ${r.prim} $name( ${t1.prim} v1, ${t2.prim} v2) {
356- | $body
362+ | $body;
357363 |}
358364 | """ .stripMargin.trim
359365 }
0 commit comments