Supplying implicit argument EXPLICITLY
I have a function like this:
case class SomeCaseClass(i: Int)
def func[T: Manifest](someArg: Int)(implicit i: String) = {
SomeCaseClass(2)
}
I need to call func
and supply i
explicitly
but when I call func[SomeCaseClass](2)("hello")
, I get:
error: not enough arguments for method func: (implicit evidence$1:
Manifest[ScalaFiddle.this.SomeCaseClass], implicit i:
String)ScalaFiddle.this.SomeCaseClass. Unspecified value parameter i.
funcSomeCaseClass("hello")
Any way to do it without changing the function's signature?
scala
add a comment |
I have a function like this:
case class SomeCaseClass(i: Int)
def func[T: Manifest](someArg: Int)(implicit i: String) = {
SomeCaseClass(2)
}
I need to call func
and supply i
explicitly
but when I call func[SomeCaseClass](2)("hello")
, I get:
error: not enough arguments for method func: (implicit evidence$1:
Manifest[ScalaFiddle.this.SomeCaseClass], implicit i:
String)ScalaFiddle.this.SomeCaseClass. Unspecified value parameter i.
funcSomeCaseClass("hello")
Any way to do it without changing the function's signature?
scala
Do you have the real signature (assuming this is a simplified example)? What is the manifest being used for?
– Thilo
Nov 22 '18 at 12:06
In the actual case, the function callsjValue.extract[T]
oforg.json4s.ExtractableJsonAstNode
. This function's signature isdef extract[A](implicit formats: Formats, mf: scala.reflect.Manifest[A]): A
. In any case, this is part of out infrastructure code, and I am very reluctant to change it.
– Barak BN
Nov 22 '18 at 12:12
1
As a side note, having an implicitstring
is not a great idea... although I realize you may have simplified the type for the question.
– Lasf
Nov 22 '18 at 13:28
add a comment |
I have a function like this:
case class SomeCaseClass(i: Int)
def func[T: Manifest](someArg: Int)(implicit i: String) = {
SomeCaseClass(2)
}
I need to call func
and supply i
explicitly
but when I call func[SomeCaseClass](2)("hello")
, I get:
error: not enough arguments for method func: (implicit evidence$1:
Manifest[ScalaFiddle.this.SomeCaseClass], implicit i:
String)ScalaFiddle.this.SomeCaseClass. Unspecified value parameter i.
funcSomeCaseClass("hello")
Any way to do it without changing the function's signature?
scala
I have a function like this:
case class SomeCaseClass(i: Int)
def func[T: Manifest](someArg: Int)(implicit i: String) = {
SomeCaseClass(2)
}
I need to call func
and supply i
explicitly
but when I call func[SomeCaseClass](2)("hello")
, I get:
error: not enough arguments for method func: (implicit evidence$1:
Manifest[ScalaFiddle.this.SomeCaseClass], implicit i:
String)ScalaFiddle.this.SomeCaseClass. Unspecified value parameter i.
funcSomeCaseClass("hello")
Any way to do it without changing the function's signature?
scala
scala
asked Nov 22 '18 at 11:59
Barak BNBarak BN
1418
1418
Do you have the real signature (assuming this is a simplified example)? What is the manifest being used for?
– Thilo
Nov 22 '18 at 12:06
In the actual case, the function callsjValue.extract[T]
oforg.json4s.ExtractableJsonAstNode
. This function's signature isdef extract[A](implicit formats: Formats, mf: scala.reflect.Manifest[A]): A
. In any case, this is part of out infrastructure code, and I am very reluctant to change it.
– Barak BN
Nov 22 '18 at 12:12
1
As a side note, having an implicitstring
is not a great idea... although I realize you may have simplified the type for the question.
– Lasf
Nov 22 '18 at 13:28
add a comment |
Do you have the real signature (assuming this is a simplified example)? What is the manifest being used for?
– Thilo
Nov 22 '18 at 12:06
In the actual case, the function callsjValue.extract[T]
oforg.json4s.ExtractableJsonAstNode
. This function's signature isdef extract[A](implicit formats: Formats, mf: scala.reflect.Manifest[A]): A
. In any case, this is part of out infrastructure code, and I am very reluctant to change it.
– Barak BN
Nov 22 '18 at 12:12
1
As a side note, having an implicitstring
is not a great idea... although I realize you may have simplified the type for the question.
– Lasf
Nov 22 '18 at 13:28
Do you have the real signature (assuming this is a simplified example)? What is the manifest being used for?
– Thilo
Nov 22 '18 at 12:06
Do you have the real signature (assuming this is a simplified example)? What is the manifest being used for?
– Thilo
Nov 22 '18 at 12:06
In the actual case, the function calls
jValue.extract[T]
of org.json4s.ExtractableJsonAstNode
. This function's signature is def extract[A](implicit formats: Formats, mf: scala.reflect.Manifest[A]): A
. In any case, this is part of out infrastructure code, and I am very reluctant to change it.– Barak BN
Nov 22 '18 at 12:12
In the actual case, the function calls
jValue.extract[T]
of org.json4s.ExtractableJsonAstNode
. This function's signature is def extract[A](implicit formats: Formats, mf: scala.reflect.Manifest[A]): A
. In any case, this is part of out infrastructure code, and I am very reluctant to change it.– Barak BN
Nov 22 '18 at 12:12
1
1
As a side note, having an implicit
string
is not a great idea... although I realize you may have simplified the type for the question.– Lasf
Nov 22 '18 at 13:28
As a side note, having an implicit
string
is not a great idea... although I realize you may have simplified the type for the question.– Lasf
Nov 22 '18 at 13:28
add a comment |
2 Answers
2
active
oldest
votes
You need to give all implicit parameters explicitly if you give any, and T: Manifest
means there is an additional implicit parameter.
Happily, implicitly
method will summon the implicit which the compiler would have supplied:
func[SomeCaseClass](2)(implicitly, "hello") // inferred to implicitly[Manifest[SomeCaseClass]]
should i need to import anything here ? i'm getting compiler error and my scala version is 2.11
– Balaji Reddy
Nov 22 '18 at 12:29
No, you shouldn't, it's inPredef
. Do you get an error withimplicitly[Manifest[SomeCaseClass]]
? If yes, what is the error?
– Alexey Romanov
Nov 22 '18 at 12:34
Type mismatch, expected: Manifest[SomeCaseClass], actual: CanBuildFrom[String, Char, String]
– Balaji Reddy
Nov 22 '18 at 12:36
1
There's noCanBuildFrom
involved in your example code. Obviously if you have different types there you'll need to adjust. You can see it working at scalafiddle.io/sf/EYro2yQ/0.
– Alexey Romanov
Nov 22 '18 at 12:38
i'm not really sure why i'm getting this error. but this is not stopping me from running the code and getting the result...
– Balaji Reddy
Nov 22 '18 at 12:49
add a comment |
When I define the method func
in my scala REPL, I am finding the output as:
func: [T](someArg: Int)(implicit evidence$1: Manifest[T], implicit i: String)SomeCaseClass
In other words, the same code can also be written as:
def func1[T](someArg: Int)(implicit manifest: Manifest[T], i: String) = {
SomeCaseClass(2)
}
It is described here as well.
So in the above code, we can see that the implicit
section now have two parameters, not the only String
. And you need to provide all the params of the implicit
section in case you want to fill them explicitly. If you're providing just one it will throw a compilation error.
Hence your method func
can be called through the below code:
func(2)(Manifest.classType(classOf[SomeCaseClass]), "hello")
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430546%2fsupplying-implicit-argument-explicitly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to give all implicit parameters explicitly if you give any, and T: Manifest
means there is an additional implicit parameter.
Happily, implicitly
method will summon the implicit which the compiler would have supplied:
func[SomeCaseClass](2)(implicitly, "hello") // inferred to implicitly[Manifest[SomeCaseClass]]
should i need to import anything here ? i'm getting compiler error and my scala version is 2.11
– Balaji Reddy
Nov 22 '18 at 12:29
No, you shouldn't, it's inPredef
. Do you get an error withimplicitly[Manifest[SomeCaseClass]]
? If yes, what is the error?
– Alexey Romanov
Nov 22 '18 at 12:34
Type mismatch, expected: Manifest[SomeCaseClass], actual: CanBuildFrom[String, Char, String]
– Balaji Reddy
Nov 22 '18 at 12:36
1
There's noCanBuildFrom
involved in your example code. Obviously if you have different types there you'll need to adjust. You can see it working at scalafiddle.io/sf/EYro2yQ/0.
– Alexey Romanov
Nov 22 '18 at 12:38
i'm not really sure why i'm getting this error. but this is not stopping me from running the code and getting the result...
– Balaji Reddy
Nov 22 '18 at 12:49
add a comment |
You need to give all implicit parameters explicitly if you give any, and T: Manifest
means there is an additional implicit parameter.
Happily, implicitly
method will summon the implicit which the compiler would have supplied:
func[SomeCaseClass](2)(implicitly, "hello") // inferred to implicitly[Manifest[SomeCaseClass]]
should i need to import anything here ? i'm getting compiler error and my scala version is 2.11
– Balaji Reddy
Nov 22 '18 at 12:29
No, you shouldn't, it's inPredef
. Do you get an error withimplicitly[Manifest[SomeCaseClass]]
? If yes, what is the error?
– Alexey Romanov
Nov 22 '18 at 12:34
Type mismatch, expected: Manifest[SomeCaseClass], actual: CanBuildFrom[String, Char, String]
– Balaji Reddy
Nov 22 '18 at 12:36
1
There's noCanBuildFrom
involved in your example code. Obviously if you have different types there you'll need to adjust. You can see it working at scalafiddle.io/sf/EYro2yQ/0.
– Alexey Romanov
Nov 22 '18 at 12:38
i'm not really sure why i'm getting this error. but this is not stopping me from running the code and getting the result...
– Balaji Reddy
Nov 22 '18 at 12:49
add a comment |
You need to give all implicit parameters explicitly if you give any, and T: Manifest
means there is an additional implicit parameter.
Happily, implicitly
method will summon the implicit which the compiler would have supplied:
func[SomeCaseClass](2)(implicitly, "hello") // inferred to implicitly[Manifest[SomeCaseClass]]
You need to give all implicit parameters explicitly if you give any, and T: Manifest
means there is an additional implicit parameter.
Happily, implicitly
method will summon the implicit which the compiler would have supplied:
func[SomeCaseClass](2)(implicitly, "hello") // inferred to implicitly[Manifest[SomeCaseClass]]
answered Nov 22 '18 at 12:22
Alexey RomanovAlexey Romanov
106k25210351
106k25210351
should i need to import anything here ? i'm getting compiler error and my scala version is 2.11
– Balaji Reddy
Nov 22 '18 at 12:29
No, you shouldn't, it's inPredef
. Do you get an error withimplicitly[Manifest[SomeCaseClass]]
? If yes, what is the error?
– Alexey Romanov
Nov 22 '18 at 12:34
Type mismatch, expected: Manifest[SomeCaseClass], actual: CanBuildFrom[String, Char, String]
– Balaji Reddy
Nov 22 '18 at 12:36
1
There's noCanBuildFrom
involved in your example code. Obviously if you have different types there you'll need to adjust. You can see it working at scalafiddle.io/sf/EYro2yQ/0.
– Alexey Romanov
Nov 22 '18 at 12:38
i'm not really sure why i'm getting this error. but this is not stopping me from running the code and getting the result...
– Balaji Reddy
Nov 22 '18 at 12:49
add a comment |
should i need to import anything here ? i'm getting compiler error and my scala version is 2.11
– Balaji Reddy
Nov 22 '18 at 12:29
No, you shouldn't, it's inPredef
. Do you get an error withimplicitly[Manifest[SomeCaseClass]]
? If yes, what is the error?
– Alexey Romanov
Nov 22 '18 at 12:34
Type mismatch, expected: Manifest[SomeCaseClass], actual: CanBuildFrom[String, Char, String]
– Balaji Reddy
Nov 22 '18 at 12:36
1
There's noCanBuildFrom
involved in your example code. Obviously if you have different types there you'll need to adjust. You can see it working at scalafiddle.io/sf/EYro2yQ/0.
– Alexey Romanov
Nov 22 '18 at 12:38
i'm not really sure why i'm getting this error. but this is not stopping me from running the code and getting the result...
– Balaji Reddy
Nov 22 '18 at 12:49
should i need to import anything here ? i'm getting compiler error and my scala version is 2.11
– Balaji Reddy
Nov 22 '18 at 12:29
should i need to import anything here ? i'm getting compiler error and my scala version is 2.11
– Balaji Reddy
Nov 22 '18 at 12:29
No, you shouldn't, it's in
Predef
. Do you get an error with implicitly[Manifest[SomeCaseClass]]
? If yes, what is the error?– Alexey Romanov
Nov 22 '18 at 12:34
No, you shouldn't, it's in
Predef
. Do you get an error with implicitly[Manifest[SomeCaseClass]]
? If yes, what is the error?– Alexey Romanov
Nov 22 '18 at 12:34
Type mismatch, expected: Manifest[SomeCaseClass], actual: CanBuildFrom[String, Char, String]
– Balaji Reddy
Nov 22 '18 at 12:36
Type mismatch, expected: Manifest[SomeCaseClass], actual: CanBuildFrom[String, Char, String]
– Balaji Reddy
Nov 22 '18 at 12:36
1
1
There's no
CanBuildFrom
involved in your example code. Obviously if you have different types there you'll need to adjust. You can see it working at scalafiddle.io/sf/EYro2yQ/0.– Alexey Romanov
Nov 22 '18 at 12:38
There's no
CanBuildFrom
involved in your example code. Obviously if you have different types there you'll need to adjust. You can see it working at scalafiddle.io/sf/EYro2yQ/0.– Alexey Romanov
Nov 22 '18 at 12:38
i'm not really sure why i'm getting this error. but this is not stopping me from running the code and getting the result...
– Balaji Reddy
Nov 22 '18 at 12:49
i'm not really sure why i'm getting this error. but this is not stopping me from running the code and getting the result...
– Balaji Reddy
Nov 22 '18 at 12:49
add a comment |
When I define the method func
in my scala REPL, I am finding the output as:
func: [T](someArg: Int)(implicit evidence$1: Manifest[T], implicit i: String)SomeCaseClass
In other words, the same code can also be written as:
def func1[T](someArg: Int)(implicit manifest: Manifest[T], i: String) = {
SomeCaseClass(2)
}
It is described here as well.
So in the above code, we can see that the implicit
section now have two parameters, not the only String
. And you need to provide all the params of the implicit
section in case you want to fill them explicitly. If you're providing just one it will throw a compilation error.
Hence your method func
can be called through the below code:
func(2)(Manifest.classType(classOf[SomeCaseClass]), "hello")
add a comment |
When I define the method func
in my scala REPL, I am finding the output as:
func: [T](someArg: Int)(implicit evidence$1: Manifest[T], implicit i: String)SomeCaseClass
In other words, the same code can also be written as:
def func1[T](someArg: Int)(implicit manifest: Manifest[T], i: String) = {
SomeCaseClass(2)
}
It is described here as well.
So in the above code, we can see that the implicit
section now have two parameters, not the only String
. And you need to provide all the params of the implicit
section in case you want to fill them explicitly. If you're providing just one it will throw a compilation error.
Hence your method func
can be called through the below code:
func(2)(Manifest.classType(classOf[SomeCaseClass]), "hello")
add a comment |
When I define the method func
in my scala REPL, I am finding the output as:
func: [T](someArg: Int)(implicit evidence$1: Manifest[T], implicit i: String)SomeCaseClass
In other words, the same code can also be written as:
def func1[T](someArg: Int)(implicit manifest: Manifest[T], i: String) = {
SomeCaseClass(2)
}
It is described here as well.
So in the above code, we can see that the implicit
section now have two parameters, not the only String
. And you need to provide all the params of the implicit
section in case you want to fill them explicitly. If you're providing just one it will throw a compilation error.
Hence your method func
can be called through the below code:
func(2)(Manifest.classType(classOf[SomeCaseClass]), "hello")
When I define the method func
in my scala REPL, I am finding the output as:
func: [T](someArg: Int)(implicit evidence$1: Manifest[T], implicit i: String)SomeCaseClass
In other words, the same code can also be written as:
def func1[T](someArg: Int)(implicit manifest: Manifest[T], i: String) = {
SomeCaseClass(2)
}
It is described here as well.
So in the above code, we can see that the implicit
section now have two parameters, not the only String
. And you need to provide all the params of the implicit
section in case you want to fill them explicitly. If you're providing just one it will throw a compilation error.
Hence your method func
can be called through the below code:
func(2)(Manifest.classType(classOf[SomeCaseClass]), "hello")
answered Nov 22 '18 at 12:27
anuj saxenaanuj saxena
24917
24917
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430546%2fsupplying-implicit-argument-explicitly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you have the real signature (assuming this is a simplified example)? What is the manifest being used for?
– Thilo
Nov 22 '18 at 12:06
In the actual case, the function calls
jValue.extract[T]
oforg.json4s.ExtractableJsonAstNode
. This function's signature isdef extract[A](implicit formats: Formats, mf: scala.reflect.Manifest[A]): A
. In any case, this is part of out infrastructure code, and I am very reluctant to change it.– Barak BN
Nov 22 '18 at 12:12
1
As a side note, having an implicit
string
is not a great idea... although I realize you may have simplified the type for the question.– Lasf
Nov 22 '18 at 13:28