Skip to content

Feature Request: Allow state-declared variables to be instantiated/initialized to non-zero signed integers #458

Open
@jubilatious1

Description

@jubilatious1

REFERENCE:

https://docs.raku.org/syntax/state

Presently, state-declared variables can be instantiated within blocks and -ne / -pe one-liners. They provide a way of initializing a variable within a block only once, for easier readability, etc. Here's a typical usage:

Count number of lines with length condition

~$ raku -ne 'state $i; $i++ if .chars <= 300; END say $i // 0;' file.txt

REQUEST:

What would be really nice would being able to initialize the state-declared value to a signed integer. Let's say you want to count the days remaining until the end of the month. You instantiate a state-declared and initialize it to the total number of days in the month (e.g. state $December = 31, and then $December-- until New Year's Eve:

~$ raku -ne 'state $Dec = 31; 
             $Dec--; 
             say "Need $Dec more Advent Calendar articles!"; 
             last if $Dec == 0;'  ~/file_with_advent_calendar_titles.txt

Of course, the above doesn't work and all lines get read to the bitter end, regardless of the $Dec counter. This is because presently the $Dec state variable is initialized to 0 zero. To get the code to work I need to write last if $Dec == -31;. This means the the output looks odd (and also the code, if you've initialized the variable to state $Dec = 31):

~$ raku -ne 'state $Dec= 31; $Dec--; say "Need $Dec more Advent Calendar articles!"; last if $Dec == -31;' ~/file_with_advent_calendar_titles.txt
Need -1 more Advent Calendar articles!
Need -2 more Advent Calendar articles!
Need -3 more Advent Calendar articles!
Need -4 more Advent Calendar articles!
Need -5 more Advent Calendar articles!
Need -6 more Advent Calendar articles!
Need -7 more Advent Calendar articles!
Need -8 more Advent Calendar articles!
Need -9 more Advent Calendar articles!
Need -10 more Advent Calendar articles!
Need -11 more Advent Calendar articles!
Need -12 more Advent Calendar articles!
Need -13 more Advent Calendar articles!
Need -14 more Advent Calendar articles!
Need -15 more Advent Calendar articles!
Need -16 more Advent Calendar articles!
Need -17 more Advent Calendar articles!
Need -18 more Advent Calendar articles!
Need -19 more Advent Calendar articles!
Need -20 more Advent Calendar articles!
Need -21 more Advent Calendar articles!
Need -22 more Advent Calendar articles!
Need -23 more Advent Calendar articles!
Need -24 more Advent Calendar articles!
Need -25 more Advent Calendar articles!
Need -26 more Advent Calendar articles!
Need -27 more Advent Calendar articles!
Need -28 more Advent Calendar articles!
Need -29 more Advent Calendar articles!
Need -30 more Advent Calendar articles!
Need -31 more Advent Calendar articles!

I hope this Feature Request is adequately described. Thank you for your consideration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    languageChanges to the Raku Programming Language

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions