Type declaration question

I know in Scala one can declare type alias with syntax like type MyString = String. But recently I learned a new way to declare type that I was not aware of, and it’s interesting.

The example is on the blog - https://blog.chmist.com/composing-monadic-functions-with-kleisli-4ef2f9a2d6c5

Where it declares a type

type ToErrorOr[A, B] = Kleisli[ErrorOr, A, B]

and in the statement of val stringToInt ... it declares with returned type String ToErrorOr Int instead of ToErrorOr[String, Int]. Why can the program declare with such expression? Any reasons behind that? And any similar usages?


This works similar to infix notation for methods. For types, infix notation can be used with any type constructor taking two arguments (in other words: any type with two generic parameters), just like the type alias in your example. As you noted, String ToErrorOr Int is equivalent to ToErrorOr[String, Int]. For any type X with parameters A and B, A X B is the same as X[A, B].

For a collection of more examples of usages for this feature, see this stackoverflow question

Those are plentiful examples, thank you!