score:8
huitseeker's suggestion is actually valid with the -ydependent-method-types
option:
$ scala -ydependent-method-types
welcome to scala version 2.9.0.1 (java hotspot(tm) 64-bit server vm, java 1.6.0_24).
scala> def processandreturn[a <: anyref](obj: a): obj.type = {
| // work with obj
| obj
| }
processandreturn: [a <: anyref](obj: a)obj.type
scala> processandreturn("hi")
res1: java.lang.string = hi
from this thread, i gather that experimental dependent method types have been around for a while, and there's even a published paper using them, but the implementation may be buggy. there has been some discussion about reworking scala's type system foundations (see adriaan moor's slides on dependent object types). if it works out, i imagine that dependent method types (among other things) will be completely kosher in future versions of scala.
score:0
the question is: if you're going to return what you passed in, why not just use void? after all, you already have the object in the calling code, don't you?
score:1
huitseeker and kipton probably have the direct answer you're looking for.
with that said, the only use case i can imagine is if you want to chain calls on the same object and ensure you're really working on the same object. are there other?
for instance:
class counter {
var i = 0
override def tostring = i.tostring
}
def incr(c: counter): counter = { c.i += 1; c }
val two = incr(incr((new counter)))
// two: counter = 2
if i call incr
twice in a row, i expect to get two. now let's imagine i am working with an ill-behaved function like:
def incorrect_incr(c: counter): counter = {
c.i += 1
new counter // should return c but does not
}
val nottwo = incorrect_incr(incorrect_incr(new counter))
// nottwo: counter = 0
i think using a wrapper to ensure we keep working on the same object would solve this problem (edited after thinking on how to make this more idiomatic):
case class id[t <: anyref](t:t) {
def map[u](f: (t) => u): id[t] = {
f(t)
this
}
def get: t = t
}
val reallytwo = id(new counter).map(incorrect_incr).map(incorrect_incr).get
// reallytwo: counter = 2
map
basically ensures the function is called for its side effect, get
unwraps the value...
score:3
i guess you would want to give the return type obj.type
in your method above, but this is unfortunately prohibited as of now. as per the spec, p.47:
(...) at present singleton types of method parameters may only appear in the method body; so dependent method types are not supported.
there may be some way to achieve what you want, but not in the type specification of that method by itself.
Source: stackoverflow.com
Related Query
- How can I provide a compile-time guarantee that my method will return the same object it gets in Scala?
- How can a method accept a (T <: Seq)[A] and return T[B] -- the same type of sequence with a different type parameter?
- How can I check that method parameters have the same type as the methods this?
- How to make method return the same generic as the input?
- How to write a zipWith method that returns the same type of collection as those passed to it?
- scala: how to create a generic type which is subtype of all the number classes in scala so that it can include compare method
- How to make compiler check that 2 method arguments have the same type?
- How can I compile only the Spark Core and Spark Streaming (so that I can get unit test utilities of Streaming)?
- How to create a generic function that can be applied to two one or more types containing the same parameters?
- How to tell the Scala compiler that a while loop will return a value?
- How define Structural Type that the method return this
- How can I use implicit conversions for two types that resolve to the same base type?
- Can a parameterized trait specify that one of its abstract classes return a value of the same type as implementing classes?
- sbt-buildinfo plugin: How can I add the java version in effect at compile time to the generated BuildInfo.scala?
- how can I write a function in scala called Order that takes one arguement: a list of Ints. And returns the same List of Ints from least to greatest
- How can I guarantee the existence of a method in a companion object and reference it?
- How can a mill task update environment variables so that later running tasks will see the updated value?
- Can Scala method that returns a trait returns method with the same type as trait?
- how can I use the copy method for case classes on a list of that case class?
- How can I define a class that is almost the same as List
- How do I make a trait that I can mix into mapped fields to that I don not have to override the same properties for many fields in a database?
- How can one create a method in scala that generates the sqlcontext implicits encoder based only on type?
- Do I need to re-use the same Akka ActorSystem or can I just create one every time I need one?
- How to create a Source that can receive elements later via a method call?
- How can I define a custom equality operation that will be used by immutable Set comparison methods
- In Scaladoc, how to link to a method in the same class?
- How do I call a Java method named the same as a Scala keyword?
- How to get the name of a case class field as a string/symbol at compile time using shapeless?
- Actors (scala/akka): is it implied that the receive method will be accessed in a threadsafe manner?
- Can partial function application be lazy in the sense that it does not recompute the same partial evaluation twice?
More Query from same tag
- In scala, how I can decide between class + companion object vs just the object
- How to write a Play JSON writes converter for a case class with a single nullable member
- "datasource not a member of org.apache.phoenix" when trying to Save DataFrames to Phoenix using DataSourceV2
- Cannot get uTest to see my tests
- How can I create one copy of a piece of data per subclass?
- How to join 2 data frame based on complex conditions
- How to set class name in TextArea in Play2 Framework Scala template?
- spark scala: count occurrence key - pair values
- Swagger and Play Framework - using binders
- Accessing message object instances in Akka Scala
- Combining types in scala
- Filtering dataframe using hashmap
- Scala regex: Inconsistency between findAll and match ... case
- Is it safe to use MoreExecutors.directExecutor for Future.sequence?
- How to take row_number() based on a condition in spark with scala
- ​Keeping empty values as empty in a dataframe while doing a operation using udf
- NullPointerException whilst learning concurrency in Scala
- Is it possible to create Compound-Primary-key in a model in lift's mapper
- How can XML errors be detected when using XMLEventReader?
- Scala fast way to parallelize collection
- Supplying "Class" type as a parameter to a method in Scala
- Can I compose a function that has an implicit argument?
- scala.collection.mutable.Map.+= do not work?
- Java Play2 - Generic templates?
- what is the an implicit conversion must be more specific
- Converting Scala @suspendable Method into a Future
- How does aggregate generalise fold and fold generalise reduce?
- Removing punctuation and no-Ascii characters in scala
- use libraries in Scala
- Scala Constructor pattern vs Extractor pattern matching