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

Scala example source code file (constrained-types.check)

This example Scala source code file (constrained-types.check) 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

a, a, annot, annot, b, companions, int, int, npe, string, string, stuff, type, where

The Scala constrained-types.check source code

Type in expressions to have them evaluated.
Type :help for more information.

scala> 

scala> 

scala> class Annot(obj: Any) extends annotation.Annotation with annotation.TypeConstraint
defined class Annot

scala> 

scala> class A {
  val x = "hello"
  val y: Int @Annot(x) = 10
  override def toString = "an A"
} 
defined class A

scala> 

scala> val a = new A
a: A = an A

scala> val y = a.y   // should rewrite "this.x" to "a.x" 
y: Int @Annot(a.x) = 10

scala> var a2 = new A
a2: A = an A

scala> val y2 = a2.y   // should drop the annotation
y2: Int = 10

scala> 

scala> object Stuff {
  val x = "hello"
  val y : Int @Annot(x) = 10
}
defined module Stuff

scala> 

scala> val y = Stuff.y // should rewrite the annotation
y: Int @Annot(Stuff.x) = 10

scala> 

scala> class B {
  val y: Int @Annot(Stuff.x) = 10
  override def toString = "a B"
}
defined class B

scala> 

scala> val b = new B
b: B = a B

scala> val y = b.y  // should keep the annotation
y: Int @Annot(Stuff.x) = 10

scala> def m(x: String): String @Annot(x) = x
m: (x: String)String @Annot(x)

scala> 

scala> val three = "three"
three: java.lang.String = three

scala> val three2 = m(three:three.type)  // should change x to three
three2: String @Annot(three) = three

scala> var four = "four"
four: java.lang.String = four

scala> val four2 = m(four) // should have an existential bound
four2: java.lang.String @Annot(x) forSome { val x: java.lang.String } = four

scala> val four3 = four2   // should have the same type as four2
four3: java.lang.String @Annot(x) forSome { val x: java.lang.String } = four

scala> val stuff = m("stuff") // should not crash
stuff: String @Annot("stuff") = stuff

scala> 

scala> class peer extends annotation.Annotation // should not crash
defined class peer

scala> 

scala> class NPE[T <: NPE[T] @peer] // should not crash
defined class NPE

scala> 

scala> def m = {
  val x = "three"
  val y : String @Annot(x) = x
  y
} // x should not escape the local scope with a narrow type
m: java.lang.String @Annot(x) forSome { val x: java.lang.String }

scala> 

scala> def n(y: String) = {
  def m(x: String) : String @Annot(x) = {
    (if (x == "")
      m("default")
    else
      x)
  }
  m("stuff".stripMargin)
} // x should be existentially bound
n: (y: String)java.lang.String @Annot(x) forSome { val x: String }

scala> 

scala> class rep extends annotation.Annotation { }
defined class rep

scala> 

scala> object A { val x = "hello" : String @ rep }
defined module A
warning: previously defined class A is not a companion to object A.
Companions must be defined together; you may wish to use :paste mode for this.

scala> 

scala> val y = a.x // should drop the annotation
y: java.lang.String = hello

scala> 

scala> val x = 3 : Int @Annot(e+f+g+h) // should have a graceful error message
<console>:8: error: not found: value e
       val x = 3 : Int @Annot(e+f+g+h) // should have a graceful error message
                              ^

scala> 

scala> class Where(condition: Boolean) extends annotation.Annotation
defined class Where

scala> 

scala> val x : Int @Where(self > 0 && self < 100) = 3
x: Int @Where(self.>(0).&&(self.<(100))) = 3

scala> 

scala> 

Other Scala examples (source code examples)

Here is a short list of links related to this Scala constrained-types.check source code file:

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.