- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
[Fwd: Re: [scala-website] [Contact the Scala Core Team] Implicit return parameters?]
Fri, 2009-05-15, 20:55
Thanks to Antonio for kindly providing the following sample code.
If I remove "case", the compilation fails.
MessageTester.scala:24: error: not found: value Message
implicit def b2m(body:Body):Message=Message(Header("default"),body)
Why is "case" needed here?
-------- Original Message --------
Subject: Re: [scala-website] [Contact the Scala Core Team] Implicit
return parameters?
Date: Thu, 14 May 2009 14:19:38 +0200
From: Antonio Cunei
To: nader@aeinehchi.com
References: <20090514103909.5613325E9@tresor-srv08.epfl.ch>
Nader,
For questions related to programming, your best option is always writing a
message to one of our mailing lists. You can easily subscribe by following
the instructions at: http://www.scala-lang.org/node/199
Concerning your question, I am not sure whether it is exactly what you are
looking for, but the following will work in Scala.
case class Header(s:String)
case class Body(s:String)
case class Message(h:Header,b:Body)
object Test extends Application {
implicit def b2m(b:Body):Message=Message(Header("default"),b)
implicit def m2b(m:Message):Body=m.b
def notify(m:Message):Message={
println("Here is the message: "+m.h+" / "+m.b);
m
}
val out:Body=notify(Body("testbody"))
}
[cunei@lampmac7 hbm]$ scalac HBM.scala
[cunei@lampmac7 hbm]$ scala Test
Here is the message: Header(default) / Body(testbody)
[cunei@lampmac7 hbm]$
I hope this is of help!
Toni
nader@aeinehchi.com wrote:
> Would it be possible to support implicit return parameters?
>
> Let me explain this in Java:
>
> Message outMessage = someObject.notify(Message inMessage);
> Where Message(Header, Body).
>
> As far as I understand, Scala helps write the code:
> Message outMessage = someObject.notify(Body inBody) as Header could be
> inserted as an implicit parameter. Question is whether a similar
> approach could be done for the return message as:
>
> Body outBody = someObject.notify(Body inBody)
>
> I have been working on a large project where messages are sent back and
> forth among many classes and layers. Often, the very same header
> travels through, but sometimes, the header is interfered and modified.
>
> In advance, thank you very much.
>
Mon, 2009-05-18, 10:47
#2
Re: [Fwd: Re: [scala-website] [Contact the Scala Core Team] Imp
I guess I have founded the answer to my previous question.
The reason is that a statement like
Header("default") is not valid unless it is a "case" class. If it is
not defined as a case class, it must be instantiated using
new Header("default").
The following code compiles:
class Header(val s:String)
class Body(val any:Any)
class Message(val header:Header,val body:Body)
object MessageTester extends Application {
implicit def b2m(body:Body):Message = new Message(new
Header("default"),body)
implicit def m2b(m:Message):Body=m.body
def notify(m:Message):Message={
println("Here is the message: "+m.header+" / "+m.body);
m
}
val out:Body=notify(new Body("testbody"))
}
Best Regards
--
Nader Aeinehchi
Aasenhagen 66 E
2020 Skedsmokorset
NORWAY
Direct and Mobile +47 41 44 29 57
Tel (private): +47 64 83 09 08
Fax +47 64 83 08 07
www.aeinehchi.com
Nader Aeinehchi wrote:
> Thanks to Antonio for kindly providing the following sample code.
>
> If I remove "case", the compilation fails.
>
> MessageTester.scala:24: error: not found: value Message
> implicit def b2m(body:Body):Message=Message(Header("default"),body)
>
> Why is "case" needed here?
> -------- Original Message --------
> Subject: Re: [scala-website] [Contact the Scala Core Team]
> Implicit return parameters?
> Date: Thu, 14 May 2009 14:19:38 +0200
> From: Antonio Cunei
> To: nader@aeinehchi.com
> References: <20090514103909.5613325E9@tresor-srv08.epfl.ch>
>
>
>
> Nader,
>
> For questions related to programming, your best option is always
> writing a message to one of our mailing lists. You can easily
> subscribe by following the instructions at:
> http://www.scala-lang.org/node/199
>
> Concerning your question, I am not sure whether it is exactly what you
> are looking for, but the following will work in Scala.
>
> case class Header(s:String)
> case class Body(s:String)
> case class Message(h:Header,b:Body)
>
> object Test extends Application {
> implicit def b2m(b:Body):Message=Message(Header("default"),b)
> implicit def m2b(m:Message):Body=m.b
>
> def notify(m:Message):Message={
> println("Here is the message: "+m.h+" / "+m.b);
> m
> }
>
> val out:Body=notify(Body("testbody"))
> }
>
> [cunei@lampmac7 hbm]$ scalac HBM.scala
> [cunei@lampmac7 hbm]$ scala Test
> Here is the message: Header(default) / Body(testbody)
> [cunei@lampmac7 hbm]$
>
> I hope this is of help!
> Toni
>
>
> nader@aeinehchi.com wrote:
>> Would it be possible to support implicit return parameters?
>>
>> Let me explain this in Java:
>>
>> Message outMessage = someObject.notify(Message inMessage);
>> Where Message(Header, Body).
>>
>> As far as I understand, Scala helps write the code:
>> Message outMessage = someObject.notify(Body inBody) as Header could
>> be inserted as an implicit parameter. Question is whether a similar
>> approach could be done for the return message as:
>>
>> Body outBody = someObject.notify(Body inBody)
>>
>> I have been working on a large project where messages are sent back
>> and forth among many classes and layers. Often, the very same header
>> travels through, but sometimes, the header is interfered and modified.
>>
>> In advance, thank you very much.
>>
>
>
>
>
>
On Friday May 15 2009, Nader Aeinehchi wrote:
> I guess I have founded the answer to my previous question.
> The reason is that a statement like
>
> Header("default") is not valid unless it is a "case" class. If it is
> not defined as a case class, it must be instantiated using
> new Header("default").
That's not precisely (or entirely) true. If you define an object named
Header with apply methods for the argument signatures of concern, you
get the same effect. To wit:
class Widget(ctorArg: String)
{
...
}
object Widget
{
def apply(str: String): = new Widget(str)
def apply(int: Int): = new Widget(int.toString)
}
val fooWidget = Widget("foo")
val intWidget = Widget(42)
> ...
>
> --
> Nader Aeinehchi
Randall Schulz