You can start reading the rough draft of Programming Scala, Third Edition on the O’Reilly Learning Platform. That remains true in Scala 3.0, but I’ve been told that a subsequent release of Scala 3 may finally, officially deprecate postfix expressions and remove them in a later release. They are not technically deprecated, but for a while it’s been necessary to enable the language feature for them, such as import. Postfix expressions are even more ambiguous than infix expressions. For example, suppose I have an extension method to convert Double literals to some Dollars type in a financial application, 100.0 dollars. What about postfix notation?įor methods that take no arguments and they are not defined with empty parentheses, (), it’s been a common trick for DSLs to use postfix notation. This is an alias for transient-define-infix. Macro: transient-define-argument name arglist docstring keyword value This macro defines NAME as a transient infix command. If I remove the annotation, recompile, and then run javap, then has the signature, public default T $less$plus$greater(T, T), using Scala’s default encoding for non-alphanumeric characters.Īs for many breaking changes, these restrictions on infix notation will start with Scala 3.1 or when the -source:future compilation flag is used with Scala 3.0. In that case you have to use transient-suffix-command to define the infix command and use t as the value of the :transient keyword. The default keyword comes from the fact that the Semigroup trait is implemented in byte code as a Java interface and the implementation of is a default method implementation for. When I compile these definitions, then use javap to analyze the class file, I see that has the full byte code signature public default T plus(T, T). Also, I can’t use "combine" as the argument for the annotation, as that name would collide with the actual combine method name. If I wanted to call this method from Java code, I would use plus. has no idea - until it is too late - that is defined as a postfix operator, not an infix. The annotation is a new feature for specifying the alphanumeric name the compiler should use for the generated byte code for the method. Note that it is necessary to use infix in NumericMonoid's definition, too, even though the abstract declaration in Semigroup is declared infix. The new infix keyword tells the compiler to allow infix notation for combine in user code without restriction.
0 Comments
Leave a Reply. |