- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
A confusing thing ...
Wed, 2011-04-20, 09:12
Hi,
Having this:
val k : (Int) => Int = 3 *
compiles correctly
val k : (Int) => Int = 3.*
yields
scala> val k : (Int) => Int = 3.*
<console>:5: error: type mismatch;
found : (x$1: Double)Double <and> (x$1: Float)Double <and> (x$1: Long)Double
<and> (x$1: Int)Double <and> (x$1: Char)Double <and> (x$1: Short)Double <and> (x
$1: Byte)Double
required: (Int) => Int
val k : (Int) => Int = 3.*
^
val k : (Int) => Int = 3.* _
yields
<console>:5: error: type mismatch;
found : Double
required: Int
val k : (Int) => Int = 3.* _
^
What is the difference ?
Thanks,
Marius
Having this:
val k : (Int) => Int = 3 *
compiles correctly
val k : (Int) => Int = 3.*
yields
scala> val k : (Int) => Int = 3.*
<console>:5: error: type mismatch;
found : (x$1: Double)Double <and> (x$1: Float)Double <and> (x$1: Long)Double
<and> (x$1: Int)Double <and> (x$1: Char)Double <and> (x$1: Short)Double <and> (x
$1: Byte)Double
required: (Int) => Int
val k : (Int) => Int = 3.*
^
val k : (Int) => Int = 3.* _
yields
<console>:5: error: type mismatch;
found : Double
required: Int
val k : (Int) => Int = 3.* _
^
What is the difference ?
Thanks,
Marius
Wed, 2011-04-20, 09:37
#2
Re: A confusing thing ...
Hmmm ... that explains it. Thank you !
On Wed, Apr 20, 2011 at 11:24 AM, Johannes Rudolph <johannes.rudolph@googlemail.com> wrote:
On Wed, Apr 20, 2011 at 11:24 AM, Johannes Rudolph <johannes.rudolph@googlemail.com> wrote:
On Wed, Apr 20, 2011 at 10:12 AM, Marius Danciu <marius.danciu@gmail.com> wrote:
> val k : (Int) => Int = 3.*
3. is a valid double literal and is parsed as such. In the past there
was a debate to deprecate double literals with a trailing decimal dot
but AFAIR it was kept for Java/backward compatibility.
--
Johannes
-----------------------------------------------
Johannes Rudolph
http://virtual-void.net
Wed, 2011-04-20, 13:07
#3
Re: A confusing thing ...
On Wed, Apr 20, 2011 at 05:24, Johannes Rudolph
wrote:
> On Wed, Apr 20, 2011 at 10:12 AM, Marius Danciu wrote:
>> val k : (Int) => Int = 3.*
>
> 3. is a valid double literal and is parsed as such. In the past there
> was a debate to deprecate double literals with a trailing decimal dot
> but AFAIR it was kept for Java/backward compatibility.
That's one which could really be thrown out. It serves no purpose
except _syntactical_ compatibility, but it's also pretty arcane.
On the other hand, what are the chances of someone doing "3." as
anything except a learning exercise while exploring Scala?
Wed, 2011-04-20, 13:28
#4
Re: A confusing thing ...
On Wed, Apr 20, 2011 at 2:57 PM, Daniel Sobral <dcsobral@gmail.com> wrote:
On Wed, Apr 20, 2011 at 05:24, Johannes Rudolph
<johannes.rudolph@googlemail.com> wrote:
> On Wed, Apr 20, 2011 at 10:12 AM, Marius Danciu <marius.danciu@gmail.com> wrote:
>> val k : (Int) => Int = 3.*
>
> 3. is a valid double literal and is parsed as such. In the past there
> was a debate to deprecate double literals with a trailing decimal dot
> but AFAIR it was kept for Java/backward compatibility.
That's one which could really be thrown out. It serves no purpose
except _syntactical_ compatibility, but it's also pretty arcane.
On the other hand, what are the chances of someone doing "3." as
anything except a learning exercise while exploring Scala?
Good question, although it seems to me rather a fragile assumption and it can cause confusion as one of the first things
people learn about scala is that values are objects. thus 3.* is closer visually/syntactically with method * of object 3 (of type Int) than * method of object 3. of (type Double).
But maybe it's just me ...
--
Daniel C. Sobral
I travel to the future all the time.
Wed, 2011-04-20, 14:17
#5
Re: A confusing thing ...
Hi everyone,
> That's one which could really be thrown out. It serves no purpose
> except _syntactical_ compatibility, but it's also pretty arcane.
I pretty much agree, it is just too confusing.
> On the other hand, what are the chances of someone doing "3." as
> anything except a learning exercise while exploring Scala?
Actually there was an interesting article about "qunatitive language
design" on IBM developerworks:
http://www.ibm.com/developerworks/java/library/j-ldn1/
They used a corpus of existing code to actually check if the planned
changes to Java 7 (the "more precise rethrow" feature, which
introduced a new small incompatibility) have some effect on existing
code.
They found out: Not a single line of code actually used the thing they
plan to disallow in Java 7, so they went ahead.
Wouldn't that be a way to deal with "5." double literals instead of
slavishly following Java's lead?
(In fact I would like having that in a bigger process which would look
on how to work with literals in general, like supporting the
"underscores in numbers" change of Project Coin, which might be a
nice, small and backward-compatible addition to Scala's grammar.)
Bye,
Simon
Wed, 2011-04-20, 16:17
#6
Re: A confusing thing ...
On Wed, Apr 20, 2011 at 7:57 AM, Daniel Sobral <dcsobral@gmail.com> wrote:
+1 -- never would have even occurred to me that this is syntactically legal, and it's certainly confusing. Unless someone has a real-world use for it, I'd recommend deprecating and then removing it...
On Wed, Apr 20, 2011 at 05:24, Johannes Rudolph
<johannes.rudolph@googlemail.com> wrote:
> On Wed, Apr 20, 2011 at 10:12 AM, Marius Danciu <marius.danciu@gmail.com> wrote:
>> val k : (Int) => Int = 3.*
>
> 3. is a valid double literal and is parsed as such. In the past there
> was a debate to deprecate double literals with a trailing decimal dot
> but AFAIR it was kept for Java/backward compatibility.
That's one which could really be thrown out. It serves no purpose
except _syntactical_ compatibility, but it's also pretty arcane.
+1 -- never would have even occurred to me that this is syntactically legal, and it's certainly confusing. Unless someone has a real-world use for it, I'd recommend deprecating and then removing it...
Wed, 2011-04-20, 16:37
#7
Re: A confusing thing ...
you can also do 3d which is as short as 3.
val plus3: Double => Double = 3d+
On 20 Apr 2011, at 16:13, Justin du coeur wrote:
> On Wed, Apr 20, 2011 at 7:57 AM, Daniel Sobral wrote:
> On Wed, Apr 20, 2011 at 05:24, Johannes Rudolph
> wrote:
> > On Wed, Apr 20, 2011 at 10:12 AM, Marius Danciu wrote:
> >> val k : (Int) => Int = 3.*
> >
> > 3. is a valid double literal and is parsed as such. In the past there
> > was a debate to deprecate double literals with a trailing decimal dot
> > but AFAIR it was kept for Java/backward compatibility.
>
> That's one which could really be thrown out. It serves no purpose
> except _syntactical_ compatibility, but it's also pretty arcane.
>
> +1 -- never would have even occurred to me that this is syntactically legal, and it's certainly confusing. Unless someone has a real-world use for it, I'd recommend deprecating and then removing it...
Wed, 2011-04-20, 17:07
#8
Re: A confusing thing ...
On 20/04/2011 13:57, Daniel Sobral wrote:
> On Wed, Apr 20, 2011 at 05:24, Johannes Rudolph
> wrote:
>>
>> 3. is a valid double literal and is parsed as such. In the past there
>> was a debate to deprecate double literals with a trailing decimal dot
>> but AFAIR it was kept for Java/backward compatibility.
>
> That's one which could really be thrown out. It serves no purpose
> except _syntactical_ compatibility, but it's also pretty arcane.
Just curious: I also dislike the notation .3 for 0.3, but I saw some people to use it.
I dislike it because I find the dot not standing out, so one can easily miss it.
Are there any people (in this mailing list) using such notation in code?
If one is thrown out, the other can be thrown as well... That even simplify the literal
number parser...
Wed, 2011-04-20, 19:47
#9
Re: A confusing thing ...
>>>>> "Justin" == Justin du coeur writes:
>> > 3. is a valid double literal and is parsed as such. [...]
Justin> never would have even occurred to me that this is
Justin> syntactically legal, and it's certainly confusing. Unless
Justin> someone has a real-world use for it, I'd recommend deprecating
Justin> and then removing it...
+1. (and that's a sentence-ending period, not a decimal point)
Being compatible with Java's good when there are substantial benefits to
doing so, but here it seems to me that the benefit is slight and the
cost isn't. People often turn up on IRC all confused about this. It's
easy to hit this issue literally the first day you start using Scala.
Thu, 2011-04-21, 18:17
#10
Re: A confusing thing ...
On Wed, Apr 20, 2011 at 7:46 PM, Seth Tisue wrote:
> +1. (and that's a sentence-ending period, not a decimal point)
Yes, please.
Ismael
Fri, 2011-04-22, 15:57
#11
Re: A confusing thing ...
On Wed, Apr 20, 2011 at 1:57 PM, Daniel Sobral wrote:
> On Wed, Apr 20, 2011 at 05:24, Johannes Rudolph
> wrote:
>> On Wed, Apr 20, 2011 at 10:12 AM, Marius Danciu wrote:
>>> val k : (Int) => Int = 3.*
>>
>> 3. is a valid double literal and is parsed as such. In the past there
>> was a debate to deprecate double literals with a trailing decimal dot
>> but AFAIR it was kept for Java/backward compatibility.
>
> That's one which could really be thrown out. It serves no purpose
> except _syntactical_ compatibility, but it's also pretty arcane.
>
> On the other hand, what are the chances of someone doing "3." as
> anything except a learning exercise while exploring Scala?
I've added in inspection in IntelliJ to help people avoid this trap.
See the screenshots http://youtrack.jetbrains.net/issue/SCL-3113
-jason
On Wed, Apr 20, 2011 at 10:12 AM, Marius Danciu wrote:
> val k : (Int) => Int = 3.*
3. is a valid double literal and is parsed as such. In the past there
was a debate to deprecate double literals with a trailing decimal dot
but AFAIR it was kept for Java/backward compatibility.