I tried to run in playground my original code where the values of year, month and day entered in terminal are read by readLn.
It works in playground only using fixed values of year, month and day.
As I added this to read a year value
main = do
print "Year: "
year :: Int <- readLn
It claimed this
Main.hs:25:10: error:
Illegal type signature: ‘Int’
Type signatures are only allowed in patterns with ScopedTypeVariables
|
25 | year :: Int <- readLn
| ^^^^^^^^^^^
However, since programs in the haskell playground are not run interactively, just in “one shot” execution, naturally the result is rather disappointing:
The error you get here is correct; it’s not “standard Haskell” to allow things like (year :: Int) on the left hand side of a bind. That’s not a Haskell Playground thing, you’ll find the same on any computer that doesn’t have certain extensions turned on by default.
That said, the ScopedTypeVariables extension is good and you can feel comfortable enabling it. You can do that for an individual file by adding a language pragma at the top of the file:
{-# LANGUAGE ScopedTypeVariables #-}
main = do
year :: Int <- readLn
...
It is strange that the playground doesn’t seem to use the default GHC2021 extension set (which includes ScopedTypeVariables) for compilers that support it (GHC 9.2 and later). @tomsmeding is that intended?
Yes, I would, if I could. I mean, what should I write to the issue? Is it possible to shear this discussion thread to the author or maintainer of play.haskell ?
Should we just wait until @jaror, @tomsmeding or who ever has made the fix he mentioned?
As I tried to run yesterday again the siplest possible readLn case in play, it didn’t work yet at least with selected GHC 9.25.
I have run once again tonight this code in playground.
It stopped with runtime error:
Command exited with code 1.
Errors
Main: <stdin>: hGetLine: end of file
Output
"Year: "
Having the language pragma on the top of code has no effect either in ghci or in playground, (i.e. no errors in ghci with or without it.)
Here is an example of the output results of running in ghci without errors.
$ ghci
GHCi, version 9.2.5: https://www.haskell.org/ghc/ :? for help
ghci> :l exampleReadLines.hs
[1 of 1] Compiling Main ( exampleReadLines.hs, interpreted )
Ok, one module loaded.
ghci> main
"Year: "
2023
"Month: "
2
"Day: "
24
2023-2-24, "Friday" Julian date number (JDN) = 2460000
ghci>
Does the web browser or platform affect how playground works?
I was using the latest Firefox on Linux and macOS 10.14.6
No, that is unfortunately the only thing it can do since you cannot give any input to the program in the playground. The programs you run are just given an empty stdin, as if you had run echo "" | ./my-program locally.