alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (bug3175.scala)

This example Scala source code file (bug3175.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

any, anyref, array, array, byte, int, int, long, long, nil, string, string, test, unit

The Scala bug3175.scala source code

/** A bit down the road this test will examine
 *  the bytecode.
 */
object Test {
  def len(x:{ def length: Int }) = x.length
  def f1(x:{ def apply(x: Int): Long }) = x(0)
  def f2(x:{ def apply(x: Int): Byte }) = x(0)
  def f3(x:{ def apply(x: Int): String }) = x(0).length
  
  def f4(x:{ def update(x: Int, y: Long): Unit }, y: Long) = x(0) = y
  def f5(x:{ def update(x: Int, y: Byte): Unit }, y: Byte) = x(0) = y
  def f6(x:{ def update(x: Int, y: String): Unit }, y: String) = x(0) = y
  
  def f7(x: { def length: Any }) = x.length
  
  def f8(x: { def apply(x: Int): Any }) = x(0)
  def f9(x: { def apply(x: Int): Int }) = x(0)
  def f10(x: { def apply(x: Int): Long }) = x(0)
  
  // update has some interesting special cases
  def f11(x:{ def update(x: Int, y: Long): Any }, y: Long) = x(0) = y
  def f12(x:{ def update(x: Int, y: String): AnyVal }, y: String) = x(0) = y  
  def f13(x:{ def update(x: Int, y: String): AnyRef }, y: String) = x(0) = y
    
  // doesn't work yet, see #3197
  // def fclone(x:{ def clone(): AnyRef }) = x.clone()
  
  def main(args: Array[String]): Unit = {
    val longs = Array(5L)
    val bytes = Array(5: Byte)
    val strs = Array("abcde", "fghjij")
    
    println(len(Array(1,2,3)) + len(Array(4.0,5.0f)) + len(Array("abc", 5)) + len("bop"))
    println(f1(longs) + f2(bytes) + f3(strs))
    
    f4(longs, 1)
    f5(bytes, 1)
    f6(strs, "a")
    
    println(f1(longs) + f2(bytes) + f3(strs))
    
    println(f7(Array(1,2,3)))
    println(f7("def"))
    
    println(f8(Array(5)))
    println(f9(Array(5)))
    println(f10(Array(5)))
    
    f11(longs, 100L)
    f12(strs, "jabooboo")
    println(longs(0))
    println(strs(0))
    f13(new { def update(x: Int, y: String): List[Int] = { println("hi mom") ; Nil } }, "irrelevant")
  }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala bug3175.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.