Scala programming mystery -
i expecting below program print each character of string "this test" on seperate line , when run snippet on scalafiddle.io , not print , can please me find why ?
abstract class absiterator{ type t def hasnext:boolean def next():t } class stringiterator(s:string) extends absiterator{ type t = char private var = 0 def hasnext = < s.length def next()={ val ch = s charat += 1 ch } } trait richiterator extends absiterator{ def foreach (f:t => unit):unit = while(hasnext) f(next()) } object stringiteratortest extends app{ class iter extends stringiterator ("this test") richiterator val iter = new iter iter foreach println }
since said tried on scalafiddle, not executing inside main method.
println("as script - executed") object x extends app { println("as main app - not executed") }
you need run script below, working scalafiddle code here - https://scalafiddle.io/sf/3czsxsp/0
abstract class absiterator{ type t def hasnext:boolean def next():t } class stringiterator(s:string) extends absiterator{ type t = char private var = 0 def hasnext = < s.length def next(): t = { val ch = s charat += 1 ch } } trait richiterator extends absiterator{ def foreach (f:t => unit):unit = while(hasnext) f(next()) } class iter extends stringiterator ("this test") richiterator val iter = new iter iter foreach println
output
t h s s t e s t
code - https://scastie.scala-lang.org/prayagupd/upw6piqtrpenlxor4wdlsw
Comments
Post a Comment