You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm coding a nullable type that improves upon Option to be both unboxed and provide an intuitive Applicative syntax for function and methods. In other words, I can apply instances of the nullable type to any function or method that doesn't understand the nullable type, which eliminates boilerplate. And the syntax should look similar to the normal use of a method or function (see the {code}test{code} functions).
First attempt using a left-associate operator * that is a method of the left-hand side operand has the problem that I need to write the function and method invocations in right-to-left order as well use parenthesis to enforce right-associativity (see the object Test2).
Next attempt using a right-associate operator *: that is a method of the right-hand side operand has the correct function and method invocations in left-to-right order but the problem that I need to use parenthesis to enforce left-associativity (see the {code}object Test3{code}).
I found a work-around which has much cleaner {code}test{code} functions result, but it requires boilerplate and can’t handle a function or method of arbitrary number of input parameters. This work-around does have the advantage that inputting the literal {code}null:B{code} (versus {code}NullableB{code}) does not short-circuit the function, i.e. not prevent the function from being called, but this could be worked around by flagging with a special object to indicate {code}null:B{code}.
Note I am unable to implement the following because the first instance of →must have a higher precedence than | and the latter instance must of the same or lower precedence.
deftest2= instance→method2<3→true>
It would be much more visually intuitive if I could overload the dot(.), comma(,), and parenthesis as operators as follows.
deftest2= instance.method2(3,true)
EDIT: I see there is the capability to pass (assume optionally with call-by-name) a parenthesized list of arguments to an overloaded operator[2]. I didn't yet try to implement this and it might increase the boilerplate of the library implementation. So in theory I could achieve as follows.
deftest2= instance*method2<(3,true)>
Or at least the ability to overload some visually similar Unicode symbols[1] with the same precedence such as follows.
deftest2= instance·method2⸨3⸥true⸩
I don't know Scala macros well enough yet to determine if they can help me (i.e. do they have enough type information), yet in any case much better to improve this so the implementation is more comprehensible to a wider audience.
@soc said:
I think there is pretty much no chance for any changes in this syntax department, especially when they make things more complicated.
Also, creating a ticket here is not the right approach:
Create a topic in scala-debate or scala-internals, and if there is consensus that this should be done (very unlikely, but that's just imho), come back to this ticket, re-open it and assign it to the person who volunteered implementing it.
Shelby Moore III (shelby) said (edited on Nov 13, 2014 7:21:38 PM UTC):
I understand the point about discussing it at scala-debate or scala-internals to try to gain consensus. One advantage of writing it down here first versus posting to a Google group, is the prettier code blocks and ability to edit my post until I get it perfected.
However, I don't understand your assertion that my suggested improvement makes anything more complicated. Note the amount of spaghetti code needed to implement case #3 without the suggested improvement. Also note that it appears it is impossible to implement an Applicative without the improvement, at least not without violating Scala subtyping as Scalaz does.
This is serious deficiency. Almost enough so that I was tempted to classify it as a bug, not just an improvement. However, one may be able to argue successfully that subtyping isn't congruent with Applicative, which I suspect might be Tony Morris's position.
Debating it at scala-* is not within my available free time at the moment.
I'm coding a nullable type that improves upon Option to be both unboxed and provide an intuitive Applicative syntax for function and methods. In other words, I can apply instances of the nullable type to any function or method that doesn't understand the nullable type, which eliminates boilerplate. And the syntax should look similar to the normal use of a method or function (see the {code}test{code} functions).
First attempt using a left-associate operator * that is a method of the left-hand side operand has the problem that I need to write the function and method invocations in right-to-left order as well use parenthesis to enforce right-associativity (see the
object Test2
).Next attempt using a right-associate operator *: that is a method of the right-hand side operand has the correct function and method invocations in left-to-right order but the problem that I need to use parenthesis to enforce left-associativity (see the {code}object Test3{code}).
I found a work-around which has much cleaner {code}test{code} functions result, but it requires boilerplate and can’t handle a function or method of arbitrary number of input parameters. This work-around does have the advantage that inputting the literal {code}null:B{code} (versus {code}NullableB{code}) does not short-circuit the function, i.e. not prevent the function from being called, but this could be worked around by flagging with a special object to indicate {code}null:B{code}.
Compare.
Note I am unable to implement the following because the first instance of →must have a higher precedence than | and the latter instance must of the same or lower precedence.
It would be much more visually intuitive if I could overload the dot(.), comma(,), and parenthesis as operators as follows.
EDIT: I see there is the capability to pass (assume optionally with call-by-name) a parenthesized list of arguments to an overloaded operator[2]. I didn't yet try to implement this and it might increase the boilerplate of the library implementation. So in theory I could achieve as follows.
Or at least the ability to overload some visually similar Unicode symbols[1] with the same precedence such as follows.
I don't know Scala macros well enough yet to determine if they can help me (i.e. do they have enough type information), yet in any case much better to improve this so the implementation is more comprehensible to a wider audience.
[1] http://www.scala-lang.org/old/node/4723
[2] http://www.scala-lang.org/docu/files/ScalaReference.pdf#subsection.6.12.3
The text was updated successfully, but these errors were encountered: