Bug? String interpolation and continuations

The following gives me weird results:
(Typed into IRB. The results are similar in ruby but because of the simple example you get an infinite loop)

···

=================================
# Allow a continuation to escape
store_cont = proc {|cont| $cont=cont; nil}

puts "[#{callcc &store_cont}]"
# prints '[]', as expected

$cont.call("hello")
# prints '[]hello]'
# expected '[hello]'

$cont.call("world")
# prints '[]hello]world]'
# expected '[world]'

I tested this with "ruby 1.8.2 (2004-07-16)".

Sam

[Sam McCall <tunah.usenet@tunah.net>, 2004-07-22 08.52 CEST]

The following gives me weird results:
(Typed into IRB. The results are similar in ruby but because of the
simple example you get an infinite loop)

=================================
# Allow a continuation to escape
store_cont = proc {|cont| $cont=cont; nil}

puts "[#{callcc &store_cont}]"
# prints '', as expected

$cont.call("hello")
# prints 'hello]'
# expected '[hello]'

My guess is, the line with the puts and the interpolated string is
internally translated to something like:

s = "["
s << callcc &store_cont
s << "]"

puts s

···

--

Hi,

The following gives me weird results:
(Typed into IRB. The results are similar in ruby but because of the
simple example you get an infinite loop)

It's not a bug. It's a feature.

# Allow a continuation to escape
store_cont = proc {|cont| $cont=cont; nil}

puts "[#{callcc &store_cont}]"
# prints '', as expected

saves the continuation to $cont, then evaluate "[#{nil}]".

$cont.call("hello")
# prints 'hello]hello]hello]...'
# expected '[hello]'

By calling the continuation the execution goes back to the saved
point, but no other states are restored, so that the value of
generating string (referenced from the local variable) remains the
last generated value after the jump. Then execution concatenate
"hello", then "]". Repeat forever.

That's what happened.

              matz.

···

In message "Bug? String interpolation and continuations" on 04/07/22, Sam McCall <tunah.usenet@tunah.net> writes:

Carlos <angus@quovadis.com.ar> wrote in message news:<20040722102042.GA1575@quovadis.com.ar>...

[Sam McCall <tunah.usenet@tunah.net>, 2004-07-22 08.52 CEST]
> The following gives me weird results:
> (Typed into IRB. The results are similar in ruby but because of the
> simple example you get an infinite loop)
>
> =================================
> # Allow a continuation to escape
> store_cont = proc {|cont| $cont=cont; nil}
>
> puts "[#{callcc &store_cont}]"
> # prints '', as expected
>
> $cont.call("hello")
> # prints 'hello]'
> # expected '[hello]'

My guess is, the line with the puts and the interpolated string is
internally translated to something like:

s = "["
s << callcc &store_cont
s << "]"

puts s

--

This doesnt address your problem fully, but you can do:
puts "[#{callcc &store_cont}" + "]"
$cont.call("hello")
# [hello]
# => nil

Yukihiro Matsumoto wrote:

Hi,

>The following gives me weird results:
>(Typed into IRB. The results are similar in ruby but because of the >simple example you get an infinite loop)

It's not a bug. It's a feature.

Just to be clear, I did expect the infinite loop :slight_smile: But I assumed that it would reinterpolate the string rather than continue appending.

>$cont.call("hello")
># prints 'hello]hello]hello]...'
># expected '[hello]'

By calling the continuation the execution goes back to the saved
point, but no other states are restored, so that the value of
generating string (referenced from the local variable) remains the
last generated value after the jump. Then execution concatenate
"hello", then "]". Repeat forever.

That's what happened.

Thanks, that makes sense, although it is surprising until I think about implementation details. (I haven't read the spec yet... not a native C speaker :wink:

I expected
s="#{foo()}-#{callcc &store_cont}-#{bar()}"
to have the interpolated values evaluated first (left to right), and then have the string assembled afterwards (by analogy with method calls having arguments evaluated first). foo() would be called once, but bar() would be called every time the continuation was resumed.

On the other hand even if this was better behaviour I guess it isn't work the performance costs, just for continuations.

Sam

···

In message "Bug? String interpolation and continuations" > on 04/07/22, Sam McCall <tunah.usenet@tunah.net> writes:

This doesnt address your problem fully, but you can do:
puts "[#{callcc &store_cont}" + "]"

Well, this is probably this that he want

  puts "["+ callcc(&store_cont) + "]"

but, perhaps, the best is to have the real example

Guy Decoux

This doesnt address your problem fully, but you can do:
puts "[#{callcc &store_cont}" + "]"

Yes :slight_smile:

Well, this is probably this that he want

  puts "["+ callcc(&store_cont) + "]"

but, perhaps, the best is to have the real example

Actually, I was just playing around with continuations when I hit this. Obviously it's easily worked around and it's never going to be a major problem, but I thought I should report it.

Sam