What does “abstract over” mean?

后端 未结 6 1961
醉话见心
醉话见心 2021-01-29 17:58

Often in the Scala literature, I encounter the phrase \"abstract over\", but I don\'t understand the intent. For example, Martin Odersky writes

You can p

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-29 18:13

    The other answers give already a good idea of what kinds of abstractions exist. Lets go over the quotes one by one, and provide an example:

    You can pass methods (or "functions") as parameters, or you can abstract over them. You can specify types as parameters, or you can abstract over them.

    Pass function as a parameter: List(1,-2,3).map(math.abs(x)) Clearly abs is passed as parameter here. map itself abstracts over a function that does a certain specialiced thing with each list element. val list = List[String]() specifies a type paramter (String). You could write a collection type which uses abstract type members instead: val buffer = Buffer{ type Elem=String }. One difference is that you have to write def f(lis:List[String])... but def f(buffer:Buffer)..., so the element type is kind of "hidden" in the second method.

    A consequence from our event streams being first-class values is that we can abstract over them.

    In Swing an event just "happens" out of the blue, and you have to deal with it here and now. Event streams allow you to do all the plumbing an wiring in a more declarative way. E.g. when you want to change the responsible listener in Swing, you have to unregister the old and to register the new one, and to know all the gory details (e.g. threading issues). With event streams, the source of the events becomes a thing you can simply pass around, making it not very different from a byte or char stream, hence a more "abstract" concept.

    Abstract type members provide flexible way to abstract over concrete types of components.

    The Buffer class above is already an example for this.

提交回复
热议问题