Skip to content

Instantly share code, notes, and snippets.

View kmizu's full-sized avatar

Kota Mizushima kmizu

View GitHub Profile
class MyArray[A:ClassManifest:Nothing_=!=](size: Int) {
val array = new Array[A](size)
}
trait Nothing_=!=[A]
object MyArray {
implicit val anyIsNotNothing: Nothing_=!=[Any] = null
implicit def anyValIsNotNothing: Nothing_=!=[AnyVal] = null
implicit def refTypeIsNotNothing[A >: Null]: Nothing_=!=[A] = null
implicit def byteIsNotNothing: Nothing_=!=[Byte] = null
implicit def shortIsNotNothing: Nothing_=!=[Short] = null
object Factorial {
def fact(n: Int) = (1 to n).foldLeft(1)(_*_)
def main(args: Array[String]) = {
val n = if(args.length == 0) 1 else args(0).toInt
1 to n foreach {i => println(i + "! = " + fact(i))}
}
}
package object stack {
trait StackSignature {
type Stack[T]
def empty[T]: Stack[T]
def push[T](s: Stack[T], e: T): Stack[T]
def peek[T](s: Stack[T]): T
def pop[T](s: Stack[T]): Stack[T]
def isEmpty[T](s: Stack[T]): Boolean
}
val StackModule: StackSignature = new StackSignature {
//scalac -P:continuations:enable
import scala.util.continuations._
import scala.collection._
object Generator {
type susp = cps[Any]
def make[A](body: (A => Unit @susp) => Unit @susp): Iterator[A] = new Iterator[A] {
val yields: A => Unit @susp = {v =>
shift{k: (Unit => Any) => (k, v):Any }
}
package example;
public class Outer {
public class Inner1 {}
public static class Inner2 {}
}
@kmizu
kmizu / K.scala
Created September 27, 2010 12:48
object K {
def main(args: Array[String]) {
println(X.N)
}
}
class RichInt(n: Int) {
def times(f: Int => Unit): Int = {
0 until n foreach f
n
}
}
implicit def enrichInt(n: Int) = new RichInt(n)
3.times{i =>
println(i)
}
trait A {
def method(){}
}
class B extends A {
def method(){} // Error
//A.scala:6: error: overriding method method in trait A of type ()Unit;
// method method needs `override' modifier
// def method(){} // Error
}
import java.util.ArrayList
trait MyArrayList[T] extends ArrayList[T] {
override def add(e: T): Boolean = {
println("added: " + e)
super.add(e)
}
}
val list = new MyArrayList[String]{}
list.add("A")
list.add("B")
import java.util.{ArrayList => JavaArrayList, List => JavaList}
trait LoggableList[T] extends JavaList[T] {
abstract override def add(e: T): Boolean = {
println("added: " + e)
super.add(e)
}
}
val list = new JavaArrayList[String] with LoggableList[String]
list.add("A")
list.add("B")