Complete externalization of field and constant lifting mechanism
This is a pretty big change, but has the positive consequences :
1) A user can inject support for arbitrary field type (like JodaTime) by implementing a few implicit "factory".
2) TypeArithmetic has much less code
3) user defined types can be made to behave like Long, Int, etc. with regards to math and agreggate operations, an alternate date object can behave like Date, i.e. between operator, etc....
https://groups.google.com/forum/#!topic/squeryl/sKEHbqzT8aI
See the prototype below to see how this works (still a few glitches to iron out...)
1) A user can inject support for arbitrary field type (like JodaTime) by implementing a few implicit "factory".
2) TypeArithmetic has much less code
3) user defined types can be made to behave like Long, Int, etc. with regards to math and agreggate operations, an alternate date object can behave like Date, i.e. between operator, etc....
https://groups.google.com/forum/#!topic/squeryl/sKEHbqzT8aI
See the prototype below to see how this works (still a few glitches to iron out...)
Leave a comment
on 2011-05-26 19:08 *
By maxime.levesque
Description changed from This is a pretty big change... to This is a pretty big change...
on 2011-05-26 19:09 *
By maxime.levesque
Prototype :
package zaza
trait TARRoot
trait TAROptionBigDecimal extends TARRoot
trait TARBigDecimal extends TAROptionBigDecimal with TARNonOption
trait TAROptionDouble extends TAROptionBigDecimal
trait TARDouble extends TAROptionDouble with TARBigDecimal with TARNonOption
trait TAROptionLong extends TAROptionDouble
trait TARLong extends TAROptionLong with TARDouble with TARNonOption
trait TAROptionFloat extends TAROptionDouble
trait TARFloat extends TAROptionFloat with TARDouble with TARNonOption
trait TAROptionInt extends TAROptionLong with TAROptionFloat
trait TARInt extends TAROptionInt with TARLong with TARNonOption with TARFloat
trait TAROptionByte extends TAROptionInt
trait TARByte extends TAROptionByte with TARInt with TARNonOption
trait TAROption extends TAROptionByte with TAROptionInt with TAROptionFloat with TAROptionLong with TAROptionDouble with TAROptionBigDecimal
trait TARNonOption
trait TARF extends TAROptionFloat with TARDouble with TAROptionDouble with TAROptionBigDecimal with TARBigDecimal
object Impls {
implicit def fi2b(f: Byte) = s1.create(f)
implicit def fi2b(f: Int) = s3.create(f)
implicit def fi2b(f: Long) = s4.create(f)
implicit def fi2b(f: Float) = s5.create(f)
implicit def fi2b(f: Double) = s2.create(f)
implicit def fi2b1(f: Option[Byte]) = s6.create(f)
implicit def fi2b2(f: Option[Int]) = s8.create(f)
implicit def fi2b3(f: Option[Long]) = s9.create(f)
implicit def fi2b4(f: Option[Float]) = s10.create(f)
implicit def fi2b5(f: Option[Double]) = s7.create(f)
implicit def fi2b6(f: Option[BigDecimal]) = s11.create(f)
implicit def fi2b7(f: BigDecimal) = s12.create(f)
// implicit def v2b[A,B](a: A)(implicit bf: BasketFactory[A,B]): Basket[A,B] = bf.create(a)
implicit val s1 = new BasketFactory[Byte,TARByte] {
def create(v: Byte) = new ConstantBasket[Byte,TARByte](v)
def convert(v: Basket[_,_]) = new BasketConversion[Byte,TARByte](v,this)
def sample = 1: Byte
}
implicit val s2 = new BasketFactory[Double,TARDouble] {
def create(v: Double) = new ConstantBasket[Double,TARDouble](v)
def convert(v: Basket[_,_]) = new BasketConversion[Double,TARDouble](v,this)
def sample = 1D
}
implicit val s3 = new BasketFactory[Int,TARInt] {
def create(v: Int) = new ConstantBasket[Int,TARInt](v)
def convert(v: Basket[_,_]) = new BasketConversion[Int,TARInt](v,this)
def sample = 1
}
implicit val s4 = new BasketFactory[Long,TARLong] {
def create(v: Long) = new ConstantBasket[Long,TARLong](v)
def convert(v: Basket[_,_]) = new BasketConversion[Long,TARLong](v,this)
def sample = 1L
}
implicit val s5 = new BasketFactory[Float,TARFloat] {
def create(v: Float) = new ConstantBasket[Float,TARFloat](v)
def convert(v: Basket[_,_]) = new BasketConversion[Float,TARFloat](v,this)
def sample = 1F
}
implicit val s6 = new OptionBasketFactory[Byte,TARByte,TAROptionByte](s1) {
def create(v: Option[Byte]) = new ConstantBasket[Option[Byte],TAROptionByte](v)
def convert(v: Basket[_,_]) = new BasketConversion[Option[Byte],TAROptionByte](v,this)
}
implicit val s7 = new OptionBasketFactory[Double,TARDouble,TAROptionDouble](s2) {
def create(v: Option[Double]) = new ConstantBasket[Option[Double],TAROptionDouble](v)
def convert(v: Basket[_,_]) = new BasketConversion[Option[Double],TAROptionDouble](v,this)
}
implicit val s8 = new OptionBasketFactory[Int,TARInt,TAROptionInt](s3) {
def create(v: Option[Int]) = new ConstantBasket[Option[Int],TAROptionInt](v)
def convert(v: Basket[_,_]) = new BasketConversion[Option[Int],TAROptionInt](v,this)
}
implicit val s9 = new OptionBasketFactory[Long,TARLong,TAROptionLong](s4) {
def create(v: Option[Long]) = new ConstantBasket[Option[Long],TAROptionLong](v)
def convert(v: Basket[_,_]) = new BasketConversion[Option[Long],TAROptionLong](v,this)
}
implicit val s10 = new OptionBasketFactory[Float,TARFloat,TAROptionFloat](s5) {
def create(v: Option[Float]) = new ConstantBasket[Option[Float],TAROptionFloat](v)
def convert(v: Basket[_,_]) = new BasketConversion[Option[Float],TAROptionFloat](v,this)
}
implicit val s12 = new BasketFactory[BigDecimal,TARBigDecimal] {
def create(v: BigDecimal) = new ConstantBasket[BigDecimal,TARBigDecimal](v)
def convert(v: Basket[_,_]) = new BasketConversion[BigDecimal,TARBigDecimal](v,this)
def sample = BigDecimal(1)
}
implicit val s11 = new OptionBasketFactory[BigDecimal,TARBigDecimal,TAROptionBigDecimal](s12) {
def create(v: Option[BigDecimal]) = new ConstantBasket[Option[BigDecimal],TAROptionBigDecimal](v)
def convert(v: Basket[_,_]) = new BasketConversion[Option[BigDecimal],TAROptionBigDecimal](v,this)
}
def max[G >: TAROption, E <: G, B1, B2]
(b: Basket[B1,E])
(implicit bs: BasketFactory[B2,G]) = bs.convert(b)
def avg[G >: TAROptionFloat, E <: G, B1, B2]
(b: Basket[B1,E])
(implicit bs: BasketFactory[B2,G]) = bs.convert(b)
def nvl[A1 <: Option[_],
A2,
A3,
E4 <: TARNonOption,
E1 >: TAROption,
E3 >: E1,
E2 <: E3]
(a: Basket[A1,E1],
b: Basket[A2,E2])
(implicit bs: OptionBasketFactory[A3,E4,E3]): Basket[A3,E4] = bs.related.convert(a)
}
import Impls._
import scala.Some
import scala.Some
import scala.Some
import scala.Some
trait Basket[A,F1] {
def add[F2 >: F1 <: TARRoot, E <: F2, B1, B2]
(b: Basket[B1,E])
(implicit bs: BasketFactory[B2,F2]): Basket[B2,F2] = bs.convert(b)
// def addZ[F2 >: F1 <: TARRoot, E <: F2, B1, B2]
// (b: Basket[B1,E])
// (implicit bs: BasketFactory[B2,F2]): Basket[B2,F2] = add(b)(bs)
def div[G >: TARFloat,
E <: G,
F1 <: G,
B1, B2]
(b: Basket[B1,E])
(implicit bs: BasketFactory[B2,G]) = bs.convert(b)
// def div[F2 >: F1 <: TARRoot, E <: F2, B1, B2]
// (b: Basket[B1,E])
// (implicit
// bs0: BasketFactory[Float,TARFloat],
// bs: BasketFactory[B2,F2]) =
// bs0.sampleB.add(bs.convert(b))(bs0)
def s: A
}
class ConstantBasket[A,B](val a: A) extends Basket[A,B] {
def s = a
}
class BasketConversion[A,B](val b: Basket[_,_], bf: BasketFactory[A,B]) extends Basket[A,B] {
def s = bf.sample
}
trait ToFloatifier[F,G]
trait BasketFactory[F,G] {
def create(f: F) : Basket[F,G]
def convert(v: Basket[_,_]): BasketConversion[F,G]
def sample: F
def sampleB = create(sample)
}
abstract class OptionBasketFactory[F,G1,G2](val related: BasketFactory[F,G1]) extends BasketFactory[Option[F],G2] {
def sample = Some(related.sample)
}
object Main {
val combs = Map(
(1:Byte) ->(8,false,"(1:Byte)"),
(1:Int) ->(32,false,"(1:Int)"),
(1:Long) ->(64,false,"(1:Long)"),
(1:Float) ->(32,true,"(1:Float)"),
(1:Double) ->(64,true,"(1:Double)"),
(BigDecimal(1)) ->(1000,true,"(BigDecimal(1))")
)
def getTest = {
for(c1 <- combs; c2 <- combs) yield {
val isFloat = c1._2._2 || c2._2._2
val maxSize: Int =
if(c1._2._1 > c2._2._1)
c1._2._1
else
c2._2._1
(isFloat,maxSize) match {
case (true,8) => 1
}
}
}
def main(a: Array[String]) = {
1L.s
1.s
1.0F.s
1.0D.s
val bLong = s4.sampleB
(bLong add 3).s : Long
(1L add 3).s : Long
(1L add 2L).s : Long
(bLong add 3.5F).s : Double
(1L add 3.5D).s : Double
(1 add 3).s : Int
(bLong add Some(3)).s : Option[Long]
(1L add Some(3.4F)).s : Option[Double]
(Some(1L) add 2L).s : Option[Long]
(Some(1L) add 3.5F).s : Option[Double]
(Some(1L) add 3.5D).s : Option[Double]
(Some(1) add 3).s : Option[Int]
(1 div 2L ).s : Double
((1:Int) div(2:Int)).s : Float
(1 div(2 :Byte)).s : Float
(1 div(2 :Long)).s : Double
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// (Some(1L) div(2 :Int)).s : Option[Double] // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
(1L div(2 :Int)).s : Double
// (Some(1L) div(Some(1 :Int))).s : Option[Double]
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
max(bLong).s : Option[Long]
max(1).s : Option[Int]
max(2L).s : Option[Long]
max(3.5D).s : Option[Double]
max(3.0F).s : Option[Float]
(bLong add 3).s : Long
(1L add 3).s : Long
(1L add 2L).s : Long
max(bLong add 3.5F).s : Option[Double]
max(1L add 3.5D).s : Option[Double]
max(1 add 3).s : Option[Int]
max(1 div 2L ).s : Option[Double]
max((1:Int) div(2:Int)).s : Option[Float]
max(1 div(2 :Byte)).s : Option[Float]
max(1 div(2 :Long)).s : Option[Double]
avg(bLong add 3.5F).s : Option[Double]
avg(1L add 3.5D).s : Option[Double]
avg(1 add 3).s : Option[Float]
avg(1 add 2L ).s : Option[Double]
avg((1:Int) add(2:Int)).s : Option[Float]
avg(1 add(2 :Byte)).s : Option[Float]
avg(1 add(2 :Long)).s : Option[Double]
avg(BigDecimal(1) add(2 :Long)).s : Option[BigDecimal]
avg(3.0D).s : Option[Double]
avg(Some(3.2D)).s : Option[Double]
avg(3L).s : Option[Double]
avg(Some(3L)).s : Option[Double]
avg(1).s : Option[Float]
avg(Some(1)).s : Option[Float]
avg(1F).s : Option[Float]
avg(Some(1F)).s : Option[Float]
avg(1: Byte).s : Option[Float]
avg(Some(1: Byte)).s : Option[Float]
avg(BigDecimal(1)).s : Option[BigDecimal]
avg(Some(BigDecimal(1))).s : Option[BigDecimal]
max(BigDecimal(1) add(2 :Long)).s : Option[BigDecimal]
(BigDecimal(1) add(2 :Long)).s: BigDecimal
(BigDecimal(1) add(2 :Byte)).s: BigDecimal
nvl(Some(1.0F), 0).s : Float
nvl(Some(1.0F), 0 : Long).s : Double
}
}
on 2011-05-26 19:10 *
By maxime.levesque
Description changed from This is a pretty big change... to This is a pretty big change...
on 2011-12-21 19:24 *
By maxime.levesque
Closed, since duplicate of #72