A string is treated as a sequence

In Daniel's answers from this post: What are the Scala context and viewing boundaries?

The method of processing a string as a Scala collection is presented.

handling String and Array, which are Java classes such as Scala> collections. For instance:

def f [CC <% Traversable [_]] (a: CC, b: CC): CC = if (a.size <b.size) a else b

I would like to know where I can find this functionality in the standard Scala libraries.

Another simple question related to the posts above:

I continue to see the shorthand of "ev", especially when it comes to examples of border restrictions or views:

def g [A] (a: A) (implicit ev: B [A]) = h (a)

What does it mean?

Thanks in advance. Greetings

+5
source share
1 answer

I would like to know if I can find this functionality in the Scala standard libraries.

Scala provides a wrapper around java.lang.String called WrappedString :

 final class WrappedString(val self: String) extends AbstractSeq[Char] with IndexedSeq[Char] with StringLike[WrappedString] 

At startup:

 f("he", "hello") 

The compiler implicitly converts the string literal to an instance of WrappedString via Predef.wrapString :

 f[String]("he", "hello")({ ((s: String) => scala.this.Predef.wrapString(s)) }); 

In turn, WrappedString extends IndexedSeq[Char] , and therefore it obeys the bounds view request to be convertible bypass.

I continue to see the shorthand of "ev", especially when it is related to the boundary of the context or the boundary of the view, what does this mean?

This is short for "evidence." If you think about it, when you ask for some implicit parameter that should be in scope, the compiler requires you to confirm that the operation can happen.

+4
source

Source: https://habr.com/ru/post/1262578/


All Articles