September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,61 +1,39 @@
|
|||
include FMS-SI.f
|
||||
include FMS-SILib.f
|
||||
|
||||
: (where) ( class-xt where-dfa -- flag )
|
||||
swap >body { where-dfa class-dfa }
|
||||
begin
|
||||
class-dfa ['] object >body <>
|
||||
while
|
||||
class-dfa where-dfa = if true exit then
|
||||
class-dfa sfa @ to class-dfa
|
||||
repeat false ;
|
||||
|
||||
: where ( class-xt "classname" -- flag )
|
||||
' >body state @
|
||||
if postpone literal postpone (where)
|
||||
else (where)
|
||||
then ; immediate
|
||||
|
||||
:class Eatable
|
||||
:m eat cr ." successful eat" ;m
|
||||
:m eat ." successful eat " ;m
|
||||
;class
|
||||
|
||||
\ FoodBox is defined without using eat in any way.
|
||||
\ FoodBox is defined without inspecting for the eat message
|
||||
:class FoodBox
|
||||
object-list eatable-types
|
||||
:m fill: { n class-xt -- }
|
||||
class-xt where Eatable
|
||||
if n 0 do class-xt eatable-types xtadd: loop
|
||||
else ." not an eatable type "
|
||||
:m init: eatable-types init: ;m
|
||||
:m add: ( obj -- )
|
||||
dup is-kindOf Eatable
|
||||
if eatable-types add:
|
||||
else drop ." not an eatable type "
|
||||
then ;m
|
||||
:m get ( -- obj ) eatable-types ;m
|
||||
:m test
|
||||
begin eatable-types each:
|
||||
while eat
|
||||
repeat ;m
|
||||
;class
|
||||
|
||||
: test ( obj -- ) \ send the eat message to each object in the object-list
|
||||
begin dup each:
|
||||
while eat
|
||||
repeat drop ;
|
||||
FoodBox aFoodBox
|
||||
Eatable aEatable
|
||||
aEatable aFoodBox add: \ add the e1 object to the object-list
|
||||
aFoodBox test \ => successful eat
|
||||
|
||||
FoodBox fb
|
||||
3 ' Eatable fb fill: \ fill the object-list with 3 objects of class Eatable
|
||||
fb get test
|
||||
successful eat
|
||||
successful eat
|
||||
successful eat
|
||||
:class brick
|
||||
:m eat cr ." successful eat " ;m
|
||||
;class
|
||||
|
||||
FoodBox fb1
|
||||
5 ' object fb1 fill: \ => not an eatable type
|
||||
brick abrick \ create an object that is not eatable
|
||||
abrick aFoodBox add: \ => not an eatable type
|
||||
|
||||
:class apple <super Eatable
|
||||
;class
|
||||
|
||||
:class green-apple <super apple
|
||||
;class
|
||||
|
||||
5 ' green-apple fb1 fill:
|
||||
fb1 get test
|
||||
successful eat
|
||||
successful eat
|
||||
successful eat
|
||||
successful eat
|
||||
successful eat
|
||||
apple anapple
|
||||
anapple aFoodBox add:
|
||||
aFoodBox test \ => successful eat successful eat
|
||||
|
|
|
|||
|
|
@ -0,0 +1,51 @@
|
|||
// version 1.0.6
|
||||
|
||||
interface Eatable {
|
||||
fun eat()
|
||||
}
|
||||
|
||||
class Cheese(val name: String) : Eatable {
|
||||
override fun eat() {
|
||||
println("Eating $name")
|
||||
}
|
||||
|
||||
override fun toString() = name
|
||||
}
|
||||
|
||||
class Meat(val name: String) : Eatable {
|
||||
override fun eat() {
|
||||
println("Eating $name")
|
||||
}
|
||||
|
||||
override fun toString() = name
|
||||
}
|
||||
|
||||
class FoodBox<T: Eatable> {
|
||||
private val foodList = mutableListOf<T>()
|
||||
|
||||
fun add(food: T) {
|
||||
foodList.add(food)
|
||||
}
|
||||
|
||||
override fun toString() = foodList.toString()
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val cheddar = Cheese("cheddar")
|
||||
val feta = Cheese("feta")
|
||||
val cheeseBox = FoodBox<Cheese>()
|
||||
cheeseBox.add(cheddar)
|
||||
cheeseBox.add(feta)
|
||||
println("CheeseBox contains : $cheeseBox")
|
||||
|
||||
val beef = Meat("beef")
|
||||
val ham = Meat("ham")
|
||||
val meatBox = FoodBox<Meat>()
|
||||
meatBox.add(beef)
|
||||
meatBox.add(ham)
|
||||
println("MeatBox contains : $meatBox")
|
||||
|
||||
cheddar.eat()
|
||||
beef.eat()
|
||||
println("Full now!")
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
call dinnerTime "yogurt"
|
||||
call dinnerTime .pizza~new
|
||||
call dinnerTime .broccoli~new
|
||||
|
||||
|
||||
-- a mixin class that defines the interface for being "food", and
|
||||
-- thus expected to implement an "eat" method
|
||||
::class food mixinclass object
|
||||
::method eat abstract
|
||||
|
||||
::class pizza subclass food
|
||||
::method eat
|
||||
Say "mmmmmmmm, pizza".
|
||||
|
||||
-- mixin classes can also be used for multiple inheritance
|
||||
::class broccoli inherit food
|
||||
::method eat
|
||||
Say "ugh, do I have to?".
|
||||
|
||||
::routine dinnerTime
|
||||
use arg dish
|
||||
-- ooRexx arguments are typeless, so tests for constrained
|
||||
-- types must be peformed at run time. The isA method will
|
||||
-- check if an object is of the required type
|
||||
if \dish~isA(.food) then do
|
||||
say "I can't eat that!"
|
||||
return
|
||||
end
|
||||
else dish~eat
|
||||
49
Task/Constrained-genericity/Rust/constrained-genericity.rust
Normal file
49
Task/Constrained-genericity/Rust/constrained-genericity.rust
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
// This declares the "Eatable" constraint. It could contain no function.
|
||||
trait Eatable {
|
||||
fn eat();
|
||||
}
|
||||
|
||||
// This declares the generic "FoodBox" type,
|
||||
// whose parameter must satisfy the "Eatable" constraint.
|
||||
// The objects of this type contain a vector of eatable objects.
|
||||
struct FoodBox<T: Eatable> {
|
||||
_data: Vec<T>,
|
||||
}
|
||||
|
||||
// This implements the functions associated with the "FoodBox" type.
|
||||
// This statement is not required, but here it is used
|
||||
// to declare a handy "new" constructor.
|
||||
impl<T: Eatable> FoodBox<T> {
|
||||
fn new() -> FoodBox<T> {
|
||||
FoodBox::<T> { _data: Vec::<T>::new() }
|
||||
}
|
||||
}
|
||||
|
||||
// This declares a simple type.
|
||||
struct Banana {}
|
||||
|
||||
// This makes the "Banana" type satisfy the "Eatable" constraint.
|
||||
// For that, every declaration inside the declaration of "Eatable"
|
||||
// must be implemented here.
|
||||
impl Eatable for Banana {
|
||||
fn eat() {}
|
||||
}
|
||||
|
||||
// This makes also the primitive "char" type satisfy the "Eatable" constraint.
|
||||
impl Eatable for char {
|
||||
fn eat() {}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// This instantiate a "FoodBox" parameterized by the "Banana" type.
|
||||
// It is allowed as "Banana" implements "Eatable".
|
||||
let _fb1 = FoodBox::<Banana>::new();
|
||||
|
||||
// This instantiate a "FoodBox" parameterized by the "char" type.
|
||||
// It is allowed, as "char" implements "Eatable".
|
||||
let _fb2 = FoodBox::<char>::new();
|
||||
|
||||
// This instantiate a "FoodBox" parameterized by the "bool" type.
|
||||
// It is NOT allowed, as "bool" does not implement "Eatable".
|
||||
//let _fb3 = FoodBox::<bool>::new();
|
||||
}
|
||||
10
Task/Constrained-genericity/Zkl/constrained-genericity-1.zkl
Normal file
10
Task/Constrained-genericity/Zkl/constrained-genericity-1.zkl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
class Eatable{ var v;
|
||||
fcn eat{ println("munching ",self.topdog.name); }
|
||||
}
|
||||
class FoodBox{
|
||||
fcn init(food1,food2,etc){
|
||||
editable,garbage:=vm.arglist.filter22("isChildOf",Eatable);
|
||||
var contents=editable;
|
||||
if(garbage) println("Rejecting: ",garbage);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
class Apple(Eatable){} class Nuts(Eatable){} class Foo{}
|
||||
FoodBox(Apple,"boogers",Nuts,Foo).contents.apply2("eat");
|
||||
Loading…
Add table
Add a link
Reference in a new issue