在Scala中使用隐式的良好实践 [英] Good Practice of Using Implicits in Scala

查看:59
本文介绍了在Scala中使用隐式的良好实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有客观的理由来/不在代码中的任何地方都使用隐式参数,只是为了缩短应传递的参数数量

Are there any objective reasons to / not to use implicit parameters everywhere in code just to shorten the number of arguments that should be passed

我有一个REST API.端点的每个调用都有一个唯一的ID,该ID应该按原样传递给该调用触发的用于调试目的的每个函数.消除显式传递参数的第一个想法是使它成为每个函数中的隐式参数.但是后来,我的直觉告诉我这是对隐式函数的滥用(尽管我没有坚定的理由不这样做).

I have a REST API. Each call of an end-point has a unique id that should be passed literally in EVERY function that is being triggered by the call for debugging purposes. A first thought that comes up to eliminate explicit passing of the parameter is to make it an implicit parameter in each function. But then, my intuition tells me that it is a misusage of implicits (though I don't have firm arguments not doing so).

是否可以通过方便性推理来证明这种方法的合理性,在这种情况下,增益仅去除单个参数,并被认为是像我这样的情况的良好实践?

Could be such approach justified by convenience reasoning where the gain is only removing a single parameter and assumed to be a good practice for the situations like mine?

推荐答案

我认为这个问题不适用于Stack Overflow.它主要是基于意见的,还不清楚您要问什么.

I think that this question is not a good fit for Stack Overflow. It is primarily opinion based and it is not completely clear what you are asking.

我认为传递上下文"类型的参数(例如请求ID)是Scala中隐式参数的相当常规的用法,因此,我认为您的提案是一种很好的做法.

I think that passing around "context" type parameters like a request id is a fairly conventional use for implicit arguments in Scala, so I would consider your proposal good practice.

在代码中到处都使用隐式参数只是为了缩短应传递的参数数量,我感到不舒服.

I don't feel comfortable using implicit parameters everywhere in code just to shorten the number of arguments that should be passed.

为什么不呢?

这篇关于在Scala中使用隐式的良好实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆