-1

In Scala, it is possible to call a function without using parentheses. For example, "1,2,3" split "," is a valid statement, which behaves the exact same way as "1,2,3".split(",").

How can I define a function that can be called this way? My experience with this (creating a class with a member function, and then try to call it without . and ( )) didn't work out, it just raised a syntax error.

uh_big_mike_boi
  • 3,350
  • 4
  • 33
  • 64
V. Courtois
  • 315
  • 7
  • 20
  • 5
    Show us the code that didn't work. What you've described _should_ work as long as the method takes a single parameter. – jwvh Aug 14 '19 at 06:50
  • 2
    please go through this tutorial https://www.scala-exercises.org/std_lib/infix_prefix_and_postfix_operators – user51 Aug 14 '19 at 06:52
  • I'm editing, it looks like I didn't say exactly what happened ; sorry. Thanks for the link, too @RajkumarNatarajan – V. Courtois Aug 14 '19 at 07:21
  • 1
    Okay I found my mistake: I tried to do this with the apply operator, something like `"1,2,3" split "," 2`, which would have worked with `"1,2,3" split "," apply 2`. Should I answer myself, close the question, or rewrite it to ask if it is possible to do `"1" 0` instead of `"1"(0)` ? – V. Courtois Aug 14 '19 at 07:29
  • Please look into about Scala DSL - https://stackoverflow.com/questions/49216312/what-is-dsl-in-scala – Ra Ka Aug 14 '19 at 07:44
  • @RaKa Would it really be necessary to completely define an "easier Scala" DSL for this to be possible? I mean, it is possible to override some operators, why not `()`? – V. Courtois Aug 14 '19 at 07:56

1 Answers1

0

Putting "." is optional while calling a function , () also can be removed at the time of call if you have only one parameter or no parameter. For more explanation see this post

Rules for Parenthesis and dots

Aditya Agarwal
  • 693
  • 1
  • 10
  • 17