The Artima Developer Community
Sponsored Link

Programming in Scala Forum
I'm confused by what s.length.toString.length means

2 replies on 1 page. Most recent reply: Nov 7, 2016 10:24 PM by Napoleon Ahiable

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Napoleon Ahiable

Posts: 2
Nickname: 102000
Registered: Oct, 2016

I'm confused by what s.length.toString.length means Posted: Nov 2, 2016 1:21 AM
Reply to this message Reply
Advertisement
On page 96 of the 3rd Edition PDF, there is this expresssion

def widthOfLength(s: String) = s.length.toString.length

My understanding is that it returns the length of passed in string as a String instead of an Int.

Therefore from the REPL, if I do e.g. "Test".length.toString.length, I expect the value 4 of type String instead of Int. However it returns 1 as Int.

Am I missing something?


Suresh Uthirapathy

Posts: 1
Nickname: sureshuind
Registered: Nov, 2016

Re: I'm confused by what s.length.toString.length means Posted: Nov 2, 2016 11:45 PM
Reply to this message Reply
> On page 96 of the 3rd Edition PDF, there is this
> expresssion
>
> def widthOfLength(s: String) = s.length.toString.length
>

> My understanding is that it returns the length of passed
> in string as a String instead of an Int.
>
> Therefore from the REPL, if I do e.g.
> "Test".length.toString.length, I expect the
> value 4 of type String instead of Int.
> However it returns 1 as Int.
>
> Am I missing something?

s.length.toString.length
s ="test"
s.lenght = 4
s.length.toString = "4" --> this is just a string now
s.length.toString.length = 1 --> lenght of string "4" is 1.
if s.length.toString = "23" then
then
s.length.toString.length = 2 --> lenght of string "23" is 2.

Napoleon Ahiable

Posts: 2
Nickname: 102000
Registered: Oct, 2016

Re: I'm confused by what s.length.toString.length means Posted: Nov 7, 2016 10:24 PM
Reply to this message Reply
I get it now thanks.

Flat View: This topic has 2 replies on 1 page
Topic: Why Func literals require type casting when overloading is used Previous Topic   Next Topic Topic: Where is my book

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use