SimpleIO in JEP draft 8323335

Stephen Colebourne scolebourne at joda.org
Wed Feb 21 22:04:48 UTC 2024


I'd like to express basic agreement that reading and writing a string
(and not more) is a good first API. But I also agree that it is
potentially an on-ramp to nowhere.

I'm also not at all happy with `input("How old are you? ")` which is
doing two unrelated things (printing and reading a line)


I'd like to throw out a left-field idea to the educators here -
transformation via method reference.

  printLine("Enter your age");
  int age = readLine(Integer::parseInt);

Obviously a method reference lambda is not a simple thing to explain
fully, but it is short and direct. Perhaps in the initial learning
stages you can get away without explaining exactly how
Integer::parseInt operates?

Suggested API:
 public final class SystemIO {
   public static void printLine(Object obj) ...
   public static void print(Object obj) ...
   public static String readLine() throws UncheckedIOException ...
   public static <T> T readLine(Function<String, T> transformer)
throws UncheckedIOException ...
}

Stephen


On Tue, 20 Feb 2024 at 19:32, Cay Horstmann <cay at horstmann.com> wrote:
>
> Hi Rémi,
>
> I am not bothered by the concepts that Scanner needs, because for me they are reasonable investments. As you point out, they are:
>
> * instance methods
> * new
> * import
>
> I would switch to a different approach if it (a) didn't introduce yet another fiddly special case (magic static import) and (b) wasn't unreasonably verbose. I would find this too painful:
>
> import java.util.SimpleIO;
>
> int age = Integer.parseInt(SimpleIO.input("How old are you? "));
>
> I could embrace the magic static import if it was sufficiently general and useful to pay for the complexity. Hence my suggestion
>
> int age = parseInt(input("How old are you? "));
>
> Or
>
> int age = readInt("How old are you? ");
>
> I need to make these decisions for the books and activities that I produce. As it is in the JEP today, I'd stick with Scanner. I would put in a note, just to signal that I know what is happening,  that as of Java 25 (?), you can also use input, but that it relies on another fiddly rule, and it doesn't transfer to file I/O, so why bother...
>
> My sentiment is just a data point. I understand that the cost/benefit model is different for everyone.
>
> Cheers,
>
> Cay
>
> PS. I'd like to register a forceful pushback on "parsing numbers is so crazily complex that we can't possibly deal with it". What does print/println produce for numbers? Is it the crazily complex localized number format? No--only printf applies localization. The print/println methods format numbers as Java literals. That's a settled issue. It's been like that since Java 1.0. Not crazily complex. A readInt/readDouble (or whatever it is called) in SimpleIO can surely be the canonical inverse of print/println.


More information about the amber-dev mailing list