diff --git a/collects/honu/examples/BoundedStack.honu b/collects/honu/examples/BoundedStack.honu index 6ce10c7cb8..6b7bb75363 100644 --- a/collects/honu/examples/BoundedStack.honu +++ b/collects/honu/examples/BoundedStack.honu @@ -16,6 +16,7 @@ type Stack { Stack push(Any x); pop(); + void foreach(Any -> void); } class ListStackC(List list) : Stack impl Stack { @@ -28,7 +29,9 @@ class ListStackC(List list) : Stack impl Stack { return (new ListStackC(list = list.rest()), list.first()); } - export Stack : push, pop; + void foreach(Any -> void f) { list.foreach(f); } + + export Stack : push, pop, foreach; } Stack emptyStack() { @@ -75,7 +78,9 @@ class BoundedStackC(Stack stack, int space) : BoundedStack impl BoundedStack { return (new BoundedStackC(stack = s, space = space + 1), obj); } - export BoundedStack : push, pop, isFull, spaceRemaining; + void foreach(Any -> void f) { stack.foreach(f); } + + export BoundedStack : push, pop, isFull, spaceRemaining, foreach; } BoundedStack emptyBoundedStack(int n) { @@ -139,6 +144,7 @@ type List { Any foldl( -> Any, Any); Any foldr( -> Any, Any); List filter(Any -> bool); + void foreach(Any -> void); } @@ -190,12 +196,14 @@ class MTList() : List impl List { List filter(Any -> bool f) { return (this : List); } + void foreach(Any -> void f) { return(); } + export List : add as addToFront, add as addToEnd, no_elt as first, no_elts as atIndex, no_elt as last, rest, drop, take, reverse, ret_other as appendToEnd, ret_other as appendToFront, length, empty, - map, fold as foldl, fold as foldr, filter; + map, fold as foldl, fold as foldr, filter, foreach; } // Since init slots get translated to init fields by need, we can put @@ -289,9 +297,14 @@ class ConsList(Any car, List cdr) : List impl List { }; } + void foreach(Any -> void f) { + f(car); + cdr.foreach(f); + } + export List : addToFront, addToEnd, first, atIndex, last, reverse, rest, drop, take, appendToEnd, appendToFront, length, empty, - map, foldl, foldr, filter; + map, foldl, foldr, filter, foreach; } @@ -318,6 +331,15 @@ Stack s3 = s2.push(new IntegerC(value = 10)); Stack s4 = s3.push(new IntegerC(value = 20)); Stack s5 = s4.push(new IntegerC(value = 40)); +// use foreach + don't care(about binding) syntax + +_ = s5.foreach(void fun(Any x) { + cond { + x isa Integer => printLine(intToString((x : Integer).value)); + else printLine("Unknown type of value"); + }; + }); + // Now try adding something to s5! Stack s6 = s5.push(new IntegerC(value = 50)); \ No newline at end of file diff --git a/collects/honu/examples/List.honu b/collects/honu/examples/List.honu index 00efa766b0..dcc3639166 100644 --- a/collects/honu/examples/List.honu +++ b/collects/honu/examples/List.honu @@ -23,6 +23,7 @@ type List { Any foldl( -> Any, Any); Any foldr( -> Any, Any); List filter(Any -> bool); + void foreach(Any -> void); } @@ -74,12 +75,14 @@ class MTList() : List impl List { List filter(Any -> bool f) { return (this : List); } + void foreach(Any -> void f) { return(); } + export List : add as addToFront, add as addToEnd, no_elt as first, no_elts as atIndex, no_elt as last, rest, drop, take, reverse, ret_other as appendToEnd, ret_other as appendToFront, length, empty, - map, fold as foldl, fold as foldr, filter; + map, fold as foldl, fold as foldr, filter, foreach; } // Since init slots get translated to init fields by need, we can put @@ -171,7 +174,12 @@ class ConsList(Any car, List cdr) : List impl List { }; } + void foreach(Any -> void f) { + f(car); + cdr.foreach(f); + } + export List : addToFront, addToEnd, first, atIndex, last, reverse, rest, drop, take, appendToEnd, appendToFront, length, empty, - map, foldl, foldr, filter; + map, foldl, foldr, filter, foreach; } diff --git a/collects/honu/private/compiler/translate-unwanted-types.ss b/collects/honu/private/compiler/translate-unwanted-types.ss index fbd3a1c20b..098320f13a 100644 --- a/collects/honu/private/compiler/translate-unwanted-types.ss +++ b/collects/honu/private/compiler/translate-unwanted-types.ss @@ -1,6 +1,7 @@ (module translate-unwanted-types mzscheme - (require (lib "plt-match.ss") + (require (lib "list.ss" "srfi" "1") + (lib "plt-match.ss") "../../ast.ss" "translate-utils.ss") @@ -14,7 +15,8 @@ (match defn [(struct honu:bind-top (_ _ types value)) (cons (build-unwanted-type-syntax-expression value) - (map translate-type-for-syntax types))] + ;; remember to filter out the top types used whenever _ appears + (map translate-type-for-syntax (filter (lambda (t) (not (honu:type-top? t))) types)))] [(struct honu:function (_ _ type formals body)) (list (translate-type-for-syntax type) (build-unwanted-type-syntax-expression body) @@ -88,7 +90,10 @@ (build-unwanted-type-syntax-expression body))] [(struct honu:let (_ bindings body)) (list (map (lambda (b) - (list (map translate-type-for-syntax (honu:binding-types b)) + ;; again, make sure to remove types corresponding to _ + (list (map translate-type-for-syntax (filter (lambda (t) + (not (honu:type-top? t))) + (honu:binding-types b))) (build-unwanted-type-syntax-expression (honu:binding-value b)))) bindings) (build-unwanted-type-syntax-expression body))]