Calling readln() after readf

Steven Schveighoffer schveiguy at gmail.com
Wed Jul 6 01:47:39 UTC 2022


On 7/5/22 8:14 PM, Gary Chike wrote:
> On Monday, 20 June 2022 at 16:08:33 UTC, Ali Çehreli wrote:
>> On 6/20/22 07:00, Gary Chike wrote:
>>
>> > Would it be appropriate to forego `readf`
>> > and read input as a string using `readln` ,benefiting from
>> the `strip`
>> > function, then convert to their appropriate datatype
>>
>> Makes sense. The following are related as well:
>>
>>   https://dlang.org/library/std/conv/parse.html
>>
>>   https://dlang.org/library/std/format/read/formatted_read.html
>>
> It's interesting from my own experimentation and observation, comparing 
> `parse!int()` vs `to!int()` For instance, I can formulate a statement 
> such as this using `to!int()`:
> `auto age = to!int(readln.strip());`
> but the same syntax  using `parse!int()` would generate an error:
> `auto age = parse!int(readln.strip());`
> So, in order to use `parse!int()`, I would need to separate it into two 
> statements with a variable acting as an intermediary:
> ```
> auto input = readln();
> auto age = parse!int(input);
> 

Yes, you have 2 orthogonal desires:

1. I want to update the original range or not
2. I want to treat the entire range as representing my conversion type 
or just the first part.

Phobos only handles 2 of the 4 possible scenarios. Well, really one 
scenario (reading the entire range, and wanting to update the original) 
is not important.

-Steve


More information about the Digitalmars-d-learn mailing list