- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Would you please help me to understand this code syntax?
Wed, 2010-03-03, 14:19
Hey,
I'm reading about "Variance under inheritance" :
http://programming-scala.labs.oreilly.com/ch12.html#VarianceUnderInherit...
Would you please check the example:
code-examples/TypeSystem/variances/func-script.scala
var f: C => C = (c: C) => new C // #1
f = (c: CSuper) => new CSub // #2
f = (c: CSuper) => new C // #3
f = (c: C) => new CSub // #4
f = (c: CSub) => new CSuper // #5: ERROR!
I didn't understand anything about this syntax
If f is a function, what is C => C ?
f body should starts after = , then what is (c: C) => new C ?
It looks like encryption to me?
Thanks for your help and time.
Wed, 2010-03-03, 17:37
#2
Re: Would you please help me to understand this code syntax?
The shorthand for the type of a function is
(ArgumentType1,ArgumentType2,...,ArgumentTypeN) => ResultType
where the first parens are unnecessary if there's a single argument type, and the shorthand for actually defining a function is
(a1: ArgType1, a2: ArgType2, ..., aN: ArgTypeN) => { result_expression }
so
var f: C => C = (c: C) => new C
can be parsed apart as
var f: (C) => C = {
(c: C) => new C
}
which can be written in long form as
var f: Function1[C,C] = {
new Function1[C,C] {
def apply(c: C) = {
new C
}
}
}
Now you can see why the shorthand is so useful
(The example is actually illustrating how covariance and contravariance interact in the case of functions. If you say "I have a function that takes a C and gives back a C" then it is perfectly okay if your function actually will accept superclasses of C also--you've promised to take a C, but you haven't promised to break if someone feeds you a not-C. However, as you've promised to return a C, you must return an actual C or a subclass of C (which can be treated as a C). You can't return a superclass of C, because that's not what you said your function was going to do.)
--Rex
On Wed, Mar 3, 2010 at 8:19 AM, HHB <hubaghdadi@yahoo.ca> wrote:
(ArgumentType1,ArgumentType2,...,ArgumentTypeN) => ResultType
where the first parens are unnecessary if there's a single argument type, and the shorthand for actually defining a function is
(a1: ArgType1, a2: ArgType2, ..., aN: ArgTypeN) => { result_expression }
so
var f: C => C = (c: C) => new C
can be parsed apart as
var f: (C) => C = {
(c: C) => new C
}
which can be written in long form as
var f: Function1[C,C] = {
new Function1[C,C] {
def apply(c: C) = {
new C
}
}
}
Now you can see why the shorthand is so useful
(The example is actually illustrating how covariance and contravariance interact in the case of functions. If you say "I have a function that takes a C and gives back a C" then it is perfectly okay if your function actually will accept superclasses of C also--you've promised to take a C, but you haven't promised to break if someone feeds you a not-C. However, as you've promised to return a C, you must return an actual C or a subclass of C (which can be treated as a C). You can't return a superclass of C, because that's not what you said your function was going to do.)
--Rex
On Wed, Mar 3, 2010 at 8:19 AM, HHB <hubaghdadi@yahoo.ca> wrote:
Hey,
I'm reading about "Variance under inheritance" :
http://programming-scala.labs.oreilly.com/ch12.html#VarianceUnderInheritance
Would you please check the example:
code-examples/TypeSystem/variances/func-script.scala
var f: C => C = (c: C) => new C // #1
f = (c: CSuper) => new CSub // #2
f = (c: CSuper) => new C // #3
f = (c: C) => new CSub // #4
f = (c: CSub) => new CSuper // #5: ERROR!
I didn't understand anything about this syntax
If f is a function, what is C => C ?
f body should starts after = , then what is (c: C) => new C ?
It looks like encryption to me?
Thanks for your help and time.
--
View this message in context: http://old.nabble.com/Would-you-please-help-me-to-understand-this-code-syntax--tp27768082p27768082.html
Sent from the Scala - User mailing list archive at Nabble.com.
Thu, 2010-03-04, 11:17
#3
Re: Would you please help me to understand this code syntax?
Isn't it the other way around. Functions are covariant in their parameter type but contravariant in there return types?
-Stefan
2010/3/3 Rex Kerr <ichoran@gmail.com>
-Stefan
2010/3/3 Rex Kerr <ichoran@gmail.com>
The shorthand for the type of a function is
(ArgumentType1,ArgumentType2,...,ArgumentTypeN) => ResultType
where the first parens are unnecessary if there's a single argument type, and the shorthand for actually defining a function is
(a1: ArgType1, a2: ArgType2, ..., aN: ArgTypeN) => { result_expression }
so
var f: C => C = (c: C) => new C
can be parsed apart as
var f: (C) => C = {
(c: C) => new C
}
which can be written in long form as
var f: Function1[C,C] = {
new Function1[C,C] {
def apply(c: C) = {
new C
}
}
}
Now you can see why the shorthand is so useful
(The example is actually illustrating how covariance and contravariance interact in the case of functions. If you say "I have a function that takes a C and gives back a C" then it is perfectly okay if your function actually will accept superclasses of C also--you've promised to take a C, but you haven't promised to break if someone feeds you a not-C. However, as you've promised to return a C, you must return an actual C or a subclass of C (which can be treated as a C). You can't return a superclass of C, because that's not what you said your function was going to do.)
--Rex
On Wed, Mar 3, 2010 at 8:19 AM, HHB <hubaghdadi@yahoo.ca> wrote:
Hey,
I'm reading about "Variance under inheritance" :
http://programming-scala.labs.oreilly.com/ch12.html#VarianceUnderInheritance
Would you please check the example:
code-examples/TypeSystem/variances/func-script.scala
var f: C => C = (c: C) => new C // #1
f = (c: CSuper) => new CSub // #2
f = (c: CSuper) => new C // #3
f = (c: C) => new CSub // #4
f = (c: CSub) => new CSuper // #5: ERROR!
I didn't understand anything about this syntax
If f is a function, what is C => C ?
f body should starts after = , then what is (c: C) => new C ?
It looks like encryption to me?
Thanks for your help and time.
--
View this message in context: http://old.nabble.com/Would-you-please-help-me-to-understand-this-code-syntax--tp27768082p27768082.html
Sent from the Scala - User mailing list archive at Nabble.com.
Thu, 2010-03-04, 13:17
#4
Re: Would you please help me to understand this code syntax?
Nope, it's contravariant in the parameter type and co-variant in the return type. That is, you can expect less (contravariant argument type) and give more (covariant argument type), but not the other way around.
Donna
On Thu, Mar 4, 2010 at 11:08 AM, Stefan Langer <mailtolanger@googlemail.com> wrote:
Donna
On Thu, Mar 4, 2010 at 11:08 AM, Stefan Langer <mailtolanger@googlemail.com> wrote:
Isn't it the other way around. Functions are covariant in their parameter type but contravariant in there return types?
-Stefan
2010/3/3 Rex Kerr <ichoran@gmail.com>
The shorthand for the type of a function is
(ArgumentType1,ArgumentType2,...,ArgumentTypeN) => ResultType
where the first parens are unnecessary if there's a single argument type, and the shorthand for actually defining a function is
(a1: ArgType1, a2: ArgType2, ..., aN: ArgTypeN) => { result_expression }
so
var f: C => C = (c: C) => new C
can be parsed apart as
var f: (C) => C = {
(c: C) => new C
}
which can be written in long form as
var f: Function1[C,C] = {
new Function1[C,C] {
def apply(c: C) = {
new C
}
}
}
Now you can see why the shorthand is so useful
(The example is actually illustrating how covariance and contravariance interact in the case of functions. If you say "I have a function that takes a C and gives back a C" then it is perfectly okay if your function actually will accept superclasses of C also--you've promised to take a C, but you haven't promised to break if someone feeds you a not-C. However, as you've promised to return a C, you must return an actual C or a subclass of C (which can be treated as a C). You can't return a superclass of C, because that's not what you said your function was going to do.)
--Rex
On Wed, Mar 3, 2010 at 8:19 AM, HHB <hubaghdadi@yahoo.ca> wrote:
Hey,
I'm reading about "Variance under inheritance" :
http://programming-scala.labs.oreilly.com/ch12.html#VarianceUnderInheritance
Would you please check the example:
code-examples/TypeSystem/variances/func-script.scala
var f: C => C = (c: C) => new C // #1
f = (c: CSuper) => new CSub // #2
f = (c: CSuper) => new C // #3
f = (c: C) => new CSub // #4
f = (c: CSub) => new CSuper // #5: ERROR!
I didn't understand anything about this syntax
If f is a function, what is C => C ?
f body should starts after = , then what is (c: C) => new C ?
It looks like encryption to me?
Thanks for your help and time.
--
View this message in context: http://old.nabble.com/Would-you-please-help-me-to-understand-this-code-syntax--tp27768082p27768082.html
Sent from the Scala - User mailing list archive at Nabble.com.
Thu, 2010-03-04, 18:17
#5
Re: Would you please help me to understand this code syntax?
Less specific, more specific. Just to clarify. :-)
On Thu, Mar 4, 2010 at 9:08 AM, Donna Malayeri <lindydonna@gmail.com> wrote:
--
Daniel C. Sobral
I travel to the future all the time.
On Thu, Mar 4, 2010 at 9:08 AM, Donna Malayeri <lindydonna@gmail.com> wrote:
Nope, it's contravariant in the parameter type and co-variant in the return type. That is, you can expect less (contravariant argument type) and give more (covariant argument type), but not the other way around.
Donna
On Thu, Mar 4, 2010 at 11:08 AM, Stefan Langer <mailtolanger@googlemail.com> wrote:
Isn't it the other way around. Functions are covariant in their parameter type but contravariant in there return types?
-Stefan
2010/3/3 Rex Kerr <ichoran@gmail.com>
The shorthand for the type of a function is
(ArgumentType1,ArgumentType2,...,ArgumentTypeN) => ResultType
where the first parens are unnecessary if there's a single argument type, and the shorthand for actually defining a function is
(a1: ArgType1, a2: ArgType2, ..., aN: ArgTypeN) => { result_expression }
so
var f: C => C = (c: C) => new C
can be parsed apart as
var f: (C) => C = {
(c: C) => new C
}
which can be written in long form as
var f: Function1[C,C] = {
new Function1[C,C] {
def apply(c: C) = {
new C
}
}
}
Now you can see why the shorthand is so useful
(The example is actually illustrating how covariance and contravariance interact in the case of functions. If you say "I have a function that takes a C and gives back a C" then it is perfectly okay if your function actually will accept superclasses of C also--you've promised to take a C, but you haven't promised to break if someone feeds you a not-C. However, as you've promised to return a C, you must return an actual C or a subclass of C (which can be treated as a C). You can't return a superclass of C, because that's not what you said your function was going to do.)
--Rex
On Wed, Mar 3, 2010 at 8:19 AM, HHB <hubaghdadi@yahoo.ca> wrote:
Hey,
I'm reading about "Variance under inheritance" :
http://programming-scala.labs.oreilly.com/ch12.html#VarianceUnderInheritance
Would you please check the example:
code-examples/TypeSystem/variances/func-script.scala
var f: C => C = (c: C) => new C // #1
f = (c: CSuper) => new CSub // #2
f = (c: CSuper) => new C // #3
f = (c: C) => new CSub // #4
f = (c: CSub) => new CSuper // #5: ERROR!
I didn't understand anything about this syntax
If f is a function, what is C => C ?
f body should starts after = , then what is (c: C) => new C ?
It looks like encryption to me?
Thanks for your help and time.
--
View this message in context: http://old.nabble.com/Would-you-please-help-me-to-understand-this-code-syntax--tp27768082p27768082.html
Sent from the Scala - User mailing list archive at Nabble.com.
--
Daniel C. Sobral
I travel to the future all the time.
C => C is a type. It's syntax sugar for scala.Function1[C,C].
(c: C) => new C
is the syntax of an anonymous function value, more specifically of a
function of type C => C.
On Wed, Mar 3, 2010 at 2:19 PM, HHB wrote:
>
> Hey,
> I'm reading about "Variance under inheritance" :
> http://programming-scala.labs.oreilly.com/ch12.html#VarianceUnderInherit...
> Would you please check the example:
> code-examples/TypeSystem/variances/func-script.scala
>
> var f: C => C = (c: C) => new C // #1
> f = (c: CSuper) => new CSub // #2
> f = (c: CSuper) => new C // #3
> f = (c: C) => new CSub // #4
> f = (c: CSub) => new CSuper // #5: ERROR!
>
> I didn't understand anything about this syntax
> If f is a function, what is C => C ?
> f body should starts after = , then what is (c: C) => new C ?
> It looks like encryption to me?
> Thanks for your help and time.
> --
> View this message in context: http://old.nabble.com/Would-you-please-help-me-to-understand-this-code-s...
> Sent from the Scala - User mailing list archive at Nabble.com.
>
>