Scala package objects with attributes indicating type / value.

What is the โ€œcorrectโ€ way for an object alias in Scala?

For example, let's say I need a RoleGroup in different parts of my application (which is divided into SBT subprojects)

trait RoleGroup object RoleGroup { case object ADMIN extends RoleGroup case object MEMBER extends RoleGroup case object PUBLIC extends RoleGroup } 

Since I do not want to re-import RoleGroup, I decided to assign the RoleGroup attribute and the object in types and val analogies:

 package com.developer package controller trait ControllerBase { type RoleGroup = controller.RoleGroup val RoleGroup = controller.RoleGroup ... } 

and then the objects of the subproject package can expand the auxiliary feature in order to receive import for free:

 package com.client package object member extends com.developer.controller.ControllerBase 

I do the same for other case objects, which should be in scope. Is this a smart decision? those. Are there any flaws / problems that I should know about? All browser compiled and test pages are displayed in the same way as in a pre-refactored application, but I'm not sure if this is the best approach.

+6
source share
1 answer

This is a reasonable approach. In fact, it is used in the Scala library itself .

There are only two imaginary member levels that you need for aliases: type (i.e., traits and classes) and value (i.e. objects, packages, and values). You cover them both.

+6
source

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


All Articles