0

Is it possible to write postfix function that I can use like that:

//... val myStr: String = "some string" val myNewStr = myStr myFunction otherMyFunction // Instead of: otherMyFunction(myFunction(myStr)) def myFunction(String: str): String { //do something } def otherMyFunction(String: str): String { //do something } //... 

I want to call my function like if it's a method of type String. It there such possibility? If there is, what syntax I have to use?

1
  • 2
    You seem to be looking for "extension classes" (aka "implicit classes" in Scala 2) in combination with postfix Commented May 19, 2022 at 10:25

2 Answers 2

2

It sounds like what you're asking is whether you can add a method to the String type. Yes, you can do that with an implicit class. See, for example, https://www.baeldung.com/scala/implicit-classes.

Sign up to request clarification or add additional context in comments.

Comments

1

In Scala 2 and without Scalaz or other non-core functional lib:

(myFunction compose otherMyFunction) (myStr) 

Scala 2.13 you can use the pipe chaining operator

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.