lunduniversity / introprog

Teaching material for "Introduction to Programming using Scala" at Lund University, LTH. http://cs.lth.se/pgk/
142 stars 176 forks source link

Update solutions for type inference exercises in the compendium #769

Open johannesnydahl opened 11 months ago

johannesnydahl commented 11 months ago

There seems to be some solutions to type inference exercises that are incorrect in the current version of the compendium, presumably due to more precise type inference in newer Scala compiler versions for union types.

I recall finding a few of these errors in the past, but this is the only one I have noted down (Exercise 2C, W08):

image

The solution says:

image

However, when trying this myself using Scala REPL the compiler infer more precise types than Any and AnyVal: image

I personally think that solutions to exercises like these have to be revised in order to not confuse students. Maybe we can include both the general and the precise type?

bjornregnell commented 11 months ago

Thanks for reporting.

Yes, with union types in Scala 3 the type inference is more precise now and all the types shown in repl sessions in answers to exercises should be inspected to see if they have changed from Scala 2 to Scala 3.

PR:s are welcome.

bjornregnell commented 3 weeks ago

Thanks for PR @axelnilsson98 I guess there are more places where there could be more precise type in in Appendix Lösningar? so I'll keep this issue open until a search for Any and Matchable has been conducted and re-run in repl to see what we get in Scala >=3.5.0