Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the construction method of Scala

Shulou Source: shulou.com Published: 2022-06-02 07:22:34 11月15日 Update

This article introduces the relevant knowledge of "what is the construction method of Scala". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The construction method of Scala is very flexible (I won't say more about it here), but it comes with the hassle of overloading the construction method.

The construction method of Scala: the trouble of overloading

For example, define a new exception type. Generally speaking, self-defined exceptions belong to checked exceptions, most of which are inherited from Exception, so most of them need to define multiple construction methods. If you define it in Java, there's nothing to say, just overload it, but it's a bit troublesome to use Scala. Scala states that all overloaded constructors must call or indirectly call the default constructor, so you must use the following methods.

MyException (message: String, cause: Throwable) extends Exception (message, cause) {def this (message: String): = this (message, null) def this (cause: Throwable): = this (null, cause) def this: = this (null, null)}

Of course, this works, but if you take a closer look at the implementation of Throwable, you will find that the exception stack will be lost if the cause passed in is null. And the most disgusting thing is that Throwable does not provide the corresponding setter/getter, all we can do is call the constructor.

So I came up with the following strange trick.

Object SpcException {def apply (message: String, cause: Throwable): Exception = (new SpcException1 (message, cause)) .asInstanceOf [Exception] def apply (message: String): Exception = (new SpcException2 (message)) .asInstanceOf [Exception] def apply (cause: Throwable): Exception = (new SpcException3 (cause). AsInstanceOf [Exception] def apply (): Exception = (new SpcException4) .asInstanceof [Exception]} trait SpcException class SpcException1 (message String: Cause: Throwable) extends Exception (message, cause) with SpcException class SpcException2 (message: String) extends Exception (message) with SpcException class SpcException3 (cause: Throwable) extends Exception (cause) with SpcException class SpcException4 extends Exception with SpcException "what is the construction method of Scala"? Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Methods trouble content more knowledge easy to say practical disgusting successful learning and then nothing generally speaking dilemmas multiple practical that is strange tricks situations articles cases Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno NVidia Huawei Shulou Tech Info Docker