我有这3个monad变形金刚
{
"name": "www-admin",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@agm/core": "^1.0.0-beta.0",
"@angular/animations": "^4.1.3",
"@angular/cdk": "^2.0.0-beta.11",
"@angular/common": "^4.0.0",
"@angular/compiler": "^4.0.0",
"@angular/core": "^4.0.0",
"@angular/forms": "^4.0.0",
"@angular/http": "^4.0.0",
"@angular/material": "^2.0.0-beta.6",
"@angular/platform-browser": "^4.0.0",
"@angular/platform-browser-dynamic": "^4.0.0",
"@angular/router": "^4.0.0",
"@ngui/auto-complete": "^0.14.4",
"@ngx-translate/core": "^7.0.0",
"@ngx-translate/http-loader": "^0.1.0",
"@types/lodash": "^4.14.107",
"@types/moment": "^2.13.0",
"angular-2-dropdown-multiselect": "^1.6.0",
"angular-2-local-storage": "^1.0.1",
"angular2-hotkeys": "^2.0.3",
"angular2-modal": "^2.0.3",
"angular2-moment": "^1.8.0",
"angular2-toaster": "^4.0.1",
"angular4-carousel": "^3.1.8",
"bootstrap": "^3.3.7",
"core-js": "^2.4.1",
"jquery": "^3.3.1",
"lodash": "^4.17.5",
"moment": "^2.18.1",
"mydatepicker": "^2.0.31",
"ng-drag-drop": "^4.0.1",
"ng-http-loader": "^0.3.3",
"ng-pick-datetime": "^5.0.0-beta.15",
"ng-sidebar": "^5.1.0",
"ng2-auto-complete": "^0.12.0",
"ng2-bootstrap": "^1.6.3",
"ng2-bootstrap-modal": "https://github.com/Sky4CE/ng2-bootstrap-modal/tarball/v.1.0.2",
"ng2-dragula": "^1.5.0",
"ng2-tooltip": "0.0.7",
"ngx-bootstrap": "^1.6.6",
"ngx-chips": "^1.5.2",
"ngx-tooltip": "0.0.9",
"rxjs": "^5.1.0",
"ts-md5": "^1.2.2",
"zone.js": "^0.8.4"
},
"devDependencies": {
"@angular/cli": "^1.6.2",
"@angular/compiler-cli": "^4.0.0",
"@types/jasmine": "2.5.38",
"@types/jquery": "^3.3.1",
"@types/node": "~6.0.60",
"codelyzer": "~2.0.0",
"jasmine-core": "~2.5.2",
"jasmine-spec-reporter": "~3.2.0",
"karma": "~1.4.1",
"karma-chrome-launcher": "~2.1.1",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^0.2.0",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.0",
"ts-node": "~2.0.0",
"tslint": "~4.5.0",
"typescript": "2.3.0"
}
}
我想将相应的完整类型(即确切的相应类型)提升为这些类型。因此,对于T,我想将Future [Option [Int]]放入其中。对于E,我想将Future(Either [String,Int])提升,对于P,我想将(Future [Either [String,Option [Int]]])提升到其中。
我编写了这段代码,并进行了编译。除了我需要一种更简洁的方法来实现相同的目的。
type T[A] = OptionT[Future, A]
type E[A] = EitherT[Future, String, A]
type P[A] = OptionT[E, A]
我正在使用Cats 1.1.0和Scala 2.12.3。
asInstanceOf很烦人。但是如果我将最后一行更改为
val x : T[Int] = OptionT(Future(Option(10)))
val y : E[Int] = EitherT(Future(Right(10).asInstanceOf[Either[String, Int]]))
val z : P[Int] = OptionT(EitherT(Future(Right(Option(10)).asInstanceOf[Either[String, Option[Int]]])))
我收到此编译器错误
val z : P[Int] = OptionT(EitherT(Future(Right(Option(10)))))
答案 0 :(得分:3)
您可以使用import cats.implicits._
的隐式方法
然后你可以写类似
val z: P[Int] = OptionT(EitherT(Future(10.some.asRight[String])))
当然,您也可以编写自己的隐式
implicit class EitherFuture[A, B](val e: Future[A Either B]) extends AnyVal {
def asEitherT: EitherT[Future, A, B] = EitherT(e)
}
implicit class OptionEitherT[A](val e: EitherT[Future, String, Option[A]]) extends AnyVal {
def asOptionT = OptionT(e)
}
val zz: P[Int] = Future(10.some.asRight[String]).asEitherT.asOptionT
答案 1 :(得分:1)
尝试为Right提供类型参数:
val z : P[Int] = OptionT(EitherT(Future(Right[String,Option[Int]](Option(10)))))
没有类型参数,当您执行Right(1)
时,scala会推断Either[Nothing,Int]
。