DEV Community

Discussion on: 100 Languages Speedrun: Episode 49: Crystal

Collapse
 
marcellourbani profile image
Marcello Urbani

Hey Tomasz
Quite impressive type inference!

I think the issue with Haskell is that + is not a valid string operator.
Should you define a Num instance for [a] it works. I don't have to tell you it's a bad idea though

A more idiomatic analogue would be to define a Semigroup instance for Int, Double,...
Not sure whether you can define a single one for all numbers

PS: IMHO having "a" + "b" to return "ab" is a bad idea, expecially with dynamic languages

Collapse
 
taw profile image
Tomasz Wegrzanowski

Most languages, dynamically or statically typed, support "a" + "b" without it causing any issues, and that's very natural code. "a" + 7 is a potential problem, and it's usually disallowed.

Haskell's problem here is that it doesn't want String to be a primitive type, it's just [Char]. This is generally a poor design, and most language rightfully treat String as a basic type, not a list or array of anything.

Anyway, I tried to get Haskell to support this by defining some new type classes, with String being an instance, but Haskell doesn't want composite type like [Char] to be an instance, even with various language extensions enabled.

I'm not sure if there's any way to force this. I managed to do this for Scala type classes (Episode 57), but not for Haskell. Maybe someone else has better luck with this.

Collapse
 
marcellourbani profile image
Marcello Urbani

Yep, and mopst people use Text instead
My point on + to concatenate strings was not that it's unpopular, just that restricting it to numbers is a reasonable design choice