Skip to content

Instantly share code, notes, and snippets.

View kmizu's full-sized avatar

Kota Mizushima kmizu

View GitHub Profile
@kmizu
kmizu / DiamondOperators.java
Created July 31, 2011 16:10
By using diamond operator, type inference is done correctly.
import java.util.*;
public class DiamondOperators {
public static void main(String[] args) {
Collection<? extends Number> collection = null;
List<? super String> list = new ArrayList<>(collection);
}
}
@kmizu
kmizu / mirage-scala-improved.scala
Created August 8, 2011 14:22
improvement mirage-scala API
val result = sqlManager.iterate(classOf[Book], Sql("SELECT BOOK_ID, BOOK_NAME, AUTHOR, PRICE FROM BOOK")){book =>
sum = sum + book.price
sum
}
@kmizu
kmizu / GJava.java
Created August 9, 2011 11:19
Using Scala generics from Java.
public class GJava {
public static void main(String[] args) {
GScala<String> s = new GScala<String>("FOO");
System.out.println(s.value());
}
}
trait Association {
type Item <: AbstractItem
trait AbstractItem {
def memberOf(): Group
}
type Group <: AbstractGroup
trait AbstractGroup {
def addItem(item: Item)
def removeItem(item: Item)
def items(): List[Item]
@kmizu
kmizu / ReturnFromAnonymousFunction.scala
Created August 14, 2011 02:47
Confirm compiled code that uses return expression inside anonymous function.
object ReturnFromAnonymousFunction {
def main(args: Array[String]) {
(1 to 10).foreach{i =>
println(i)
if(i < 5) return
}
}
}
@kmizu
kmizu / GenTypeConstraints.scala
Created August 14, 2011 04:17
Hacking generalized type constraints.
object GenTypeConstraints {
type ||[A, B] = Either[A, B]
implicit def any2Left[A, B](l: B): Either[B, A] = Left(l)
implicit def any2Right[A, B](r: B): Either[A, B] = Right(r)
def main(args: Array[String]) {
println(f(1))
println(f("FOO"))
// compilation error println(f(1.0D))
}
def f[T](v: T)(implicit ev: T => (String || Int)): String = {
@kmizu
kmizu / SqlManager.scala.diff
Created August 15, 2011 17:07
Result of diff -u applied to SqlManager.scala in mirage-scala.
--- SqlManager.scala.orig 2011-08-16 01:40:50.603000000 +0900
+++ SqlManager.scala 2011-08-16 02:04:16.069118600 +0900
@@ -8,10 +8,7 @@
/**
* SqlManager wrapper for Scala.
*/
-class SqlManager(sqlManager: jp.sf.amateras.mirage.SqlManagerImpl) {
-
- // TODO もう少しいい初期化の方法はないかなぁ
- BeanDescFactoryInitializer.initialize()
@kmizu
kmizu / link.scala
Created August 26, 2011 14:39
Emulating separate compilation in Scala
Welcome to Scala version 2.9.0.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26).
Type in expressions to have them evaluated.
Type :help for more information.
scala> trait scope {
| class A extends AnyRef with Z {
| def aaa() = zzz2()
| def aaa2() = 100
| }
@kmizu
kmizu / VisitorMain.scala
Created August 31, 2011 22:40
A Visitor pattern example, which solved "Expression Problem" related to Visitor.
trait VisitorsBase {self =>
trait Node {
def accept(v: V)
}
case class Add(l: Node, r: Node) extends Node {
def accept(v: V) {
v.visit(this)
}
}
case class Sub(l: Node, r: Node) extends Node {