Need help understanding the syntax
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am Scala beginner.I know the syntax for function declaration
def function_name(parameter:return_type):return_type = {function_body}
Refering this link https://livebook.manning.com/#!/book/scala-in-action/chapter-2/161.
I can't get over the code written there
def breakable(op: => Unit) { ... }
Can anyone explain this?
scala
add a comment |
I am Scala beginner.I know the syntax for function declaration
def function_name(parameter:return_type):return_type = {function_body}
Refering this link https://livebook.manning.com/#!/book/scala-in-action/chapter-2/161.
I can't get over the code written there
def breakable(op: => Unit) { ... }
Can anyone explain this?
scala
Possible duplicate of Scala: What is the difference between (a: String) and (a: => String) for argument?
– Robin Green
Nov 17 '18 at 7:46
1
@RobinGreen The problem might be the lack of=
so I can't tell if this is a duplicate or not. Or rather, it is probably a duplicate of two different questions!
– Tim
Nov 17 '18 at 7:54
add a comment |
I am Scala beginner.I know the syntax for function declaration
def function_name(parameter:return_type):return_type = {function_body}
Refering this link https://livebook.manning.com/#!/book/scala-in-action/chapter-2/161.
I can't get over the code written there
def breakable(op: => Unit) { ... }
Can anyone explain this?
scala
I am Scala beginner.I know the syntax for function declaration
def function_name(parameter:return_type):return_type = {function_body}
Refering this link https://livebook.manning.com/#!/book/scala-in-action/chapter-2/161.
I can't get over the code written there
def breakable(op: => Unit) { ... }
Can anyone explain this?
scala
scala
asked Nov 17 '18 at 7:14
dragondragon
913
913
Possible duplicate of Scala: What is the difference between (a: String) and (a: => String) for argument?
– Robin Green
Nov 17 '18 at 7:46
1
@RobinGreen The problem might be the lack of=
so I can't tell if this is a duplicate or not. Or rather, it is probably a duplicate of two different questions!
– Tim
Nov 17 '18 at 7:54
add a comment |
Possible duplicate of Scala: What is the difference between (a: String) and (a: => String) for argument?
– Robin Green
Nov 17 '18 at 7:46
1
@RobinGreen The problem might be the lack of=
so I can't tell if this is a duplicate or not. Or rather, it is probably a duplicate of two different questions!
– Tim
Nov 17 '18 at 7:54
Possible duplicate of Scala: What is the difference between (a: String) and (a: => String) for argument?
– Robin Green
Nov 17 '18 at 7:46
Possible duplicate of Scala: What is the difference between (a: String) and (a: => String) for argument?
– Robin Green
Nov 17 '18 at 7:46
1
1
@RobinGreen The problem might be the lack of
=
so I can't tell if this is a duplicate or not. Or rather, it is probably a duplicate of two different questions!– Tim
Nov 17 '18 at 7:54
@RobinGreen The problem might be the lack of
=
so I can't tell if this is a duplicate or not. Or rather, it is probably a duplicate of two different questions!– Tim
Nov 17 '18 at 7:54
add a comment |
2 Answers
2
active
oldest
votes
This is a shorthand syntax for declaring a function that returns Unit
(roughly the same as void
in C
-like languages).
The definition
def breakable(op: => Unit) { ... }
is the same as
def breakable(op: => Unit): Unit = { ... }
This syntax may be removed in later versions of Scala, so best to include the =
even when declaring a function that returns Unit
.
The parameter is declared as op: => Unit
which means it is a call-by-name parameter. This means that the expression that is passed to this parameter is only evaluated when it is used, and is evaluated every time it is used. In this case it means that you can pass a block of code to breakable
in a convenient way:
breakable {
println("I have been called")
}
Each time breakable
evaluates op
, the println
statement will be executed.
add a comment |
In scala function can take values(pre-computed values or not-yet-computed values) along with function as arguments.
So, function which takes computed values as argument,
scala> def f(a: Int) = a * 100
f: (a: Int)Int
Example of function which takes a block of code returning Int
(not yet computed) , block of code here could be just anonymous or existing method(not a function; that's where things get messy)
scala> def f(a: => Int) = a
f: (a: => Int)Int
and to use it,
scala> def a: Int = 100 * 100
a: Int
scala> f(a) //a returns Int so confirms a: => Int
res3: Int = 10000
In your example, def breakable(op: => Unit) { ... }
takes a argument which WILLL be computed as Unit
.
For example, you can pass println
, which returns Unit
,
scala> def breakable(op: => Unit) = op
breakable: (op: => Unit)Unit
scala> breakable { println("i m learning fp") }
i m learning fp
The other thing you will see is function taking function as argument, as in Functor, Monad etc.
For example, map
/ flatMap
on List[Int]
would be
final override def map[B](f: A => B): List[B]
final override def flatMap[B](f: Int => scala.collection.IterableOnce[B]): List[B]
2
op
is not a function parameter, it is a call-by-name parameter, so you need to edit your explanation,
– Tim
Nov 17 '18 at 7:44
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%2f53349074%2fneed-help-understanding-the-syntax%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
This is a shorthand syntax for declaring a function that returns Unit
(roughly the same as void
in C
-like languages).
The definition
def breakable(op: => Unit) { ... }
is the same as
def breakable(op: => Unit): Unit = { ... }
This syntax may be removed in later versions of Scala, so best to include the =
even when declaring a function that returns Unit
.
The parameter is declared as op: => Unit
which means it is a call-by-name parameter. This means that the expression that is passed to this parameter is only evaluated when it is used, and is evaluated every time it is used. In this case it means that you can pass a block of code to breakable
in a convenient way:
breakable {
println("I have been called")
}
Each time breakable
evaluates op
, the println
statement will be executed.
add a comment |
This is a shorthand syntax for declaring a function that returns Unit
(roughly the same as void
in C
-like languages).
The definition
def breakable(op: => Unit) { ... }
is the same as
def breakable(op: => Unit): Unit = { ... }
This syntax may be removed in later versions of Scala, so best to include the =
even when declaring a function that returns Unit
.
The parameter is declared as op: => Unit
which means it is a call-by-name parameter. This means that the expression that is passed to this parameter is only evaluated when it is used, and is evaluated every time it is used. In this case it means that you can pass a block of code to breakable
in a convenient way:
breakable {
println("I have been called")
}
Each time breakable
evaluates op
, the println
statement will be executed.
add a comment |
This is a shorthand syntax for declaring a function that returns Unit
(roughly the same as void
in C
-like languages).
The definition
def breakable(op: => Unit) { ... }
is the same as
def breakable(op: => Unit): Unit = { ... }
This syntax may be removed in later versions of Scala, so best to include the =
even when declaring a function that returns Unit
.
The parameter is declared as op: => Unit
which means it is a call-by-name parameter. This means that the expression that is passed to this parameter is only evaluated when it is used, and is evaluated every time it is used. In this case it means that you can pass a block of code to breakable
in a convenient way:
breakable {
println("I have been called")
}
Each time breakable
evaluates op
, the println
statement will be executed.
This is a shorthand syntax for declaring a function that returns Unit
(roughly the same as void
in C
-like languages).
The definition
def breakable(op: => Unit) { ... }
is the same as
def breakable(op: => Unit): Unit = { ... }
This syntax may be removed in later versions of Scala, so best to include the =
even when declaring a function that returns Unit
.
The parameter is declared as op: => Unit
which means it is a call-by-name parameter. This means that the expression that is passed to this parameter is only evaluated when it is used, and is evaluated every time it is used. In this case it means that you can pass a block of code to breakable
in a convenient way:
breakable {
println("I have been called")
}
Each time breakable
evaluates op
, the println
statement will be executed.
edited Nov 17 '18 at 7:55
answered Nov 17 '18 at 7:34
TimTim
7,1611819
7,1611819
add a comment |
add a comment |
In scala function can take values(pre-computed values or not-yet-computed values) along with function as arguments.
So, function which takes computed values as argument,
scala> def f(a: Int) = a * 100
f: (a: Int)Int
Example of function which takes a block of code returning Int
(not yet computed) , block of code here could be just anonymous or existing method(not a function; that's where things get messy)
scala> def f(a: => Int) = a
f: (a: => Int)Int
and to use it,
scala> def a: Int = 100 * 100
a: Int
scala> f(a) //a returns Int so confirms a: => Int
res3: Int = 10000
In your example, def breakable(op: => Unit) { ... }
takes a argument which WILLL be computed as Unit
.
For example, you can pass println
, which returns Unit
,
scala> def breakable(op: => Unit) = op
breakable: (op: => Unit)Unit
scala> breakable { println("i m learning fp") }
i m learning fp
The other thing you will see is function taking function as argument, as in Functor, Monad etc.
For example, map
/ flatMap
on List[Int]
would be
final override def map[B](f: A => B): List[B]
final override def flatMap[B](f: Int => scala.collection.IterableOnce[B]): List[B]
2
op
is not a function parameter, it is a call-by-name parameter, so you need to edit your explanation,
– Tim
Nov 17 '18 at 7:44
add a comment |
In scala function can take values(pre-computed values or not-yet-computed values) along with function as arguments.
So, function which takes computed values as argument,
scala> def f(a: Int) = a * 100
f: (a: Int)Int
Example of function which takes a block of code returning Int
(not yet computed) , block of code here could be just anonymous or existing method(not a function; that's where things get messy)
scala> def f(a: => Int) = a
f: (a: => Int)Int
and to use it,
scala> def a: Int = 100 * 100
a: Int
scala> f(a) //a returns Int so confirms a: => Int
res3: Int = 10000
In your example, def breakable(op: => Unit) { ... }
takes a argument which WILLL be computed as Unit
.
For example, you can pass println
, which returns Unit
,
scala> def breakable(op: => Unit) = op
breakable: (op: => Unit)Unit
scala> breakable { println("i m learning fp") }
i m learning fp
The other thing you will see is function taking function as argument, as in Functor, Monad etc.
For example, map
/ flatMap
on List[Int]
would be
final override def map[B](f: A => B): List[B]
final override def flatMap[B](f: Int => scala.collection.IterableOnce[B]): List[B]
2
op
is not a function parameter, it is a call-by-name parameter, so you need to edit your explanation,
– Tim
Nov 17 '18 at 7:44
add a comment |
In scala function can take values(pre-computed values or not-yet-computed values) along with function as arguments.
So, function which takes computed values as argument,
scala> def f(a: Int) = a * 100
f: (a: Int)Int
Example of function which takes a block of code returning Int
(not yet computed) , block of code here could be just anonymous or existing method(not a function; that's where things get messy)
scala> def f(a: => Int) = a
f: (a: => Int)Int
and to use it,
scala> def a: Int = 100 * 100
a: Int
scala> f(a) //a returns Int so confirms a: => Int
res3: Int = 10000
In your example, def breakable(op: => Unit) { ... }
takes a argument which WILLL be computed as Unit
.
For example, you can pass println
, which returns Unit
,
scala> def breakable(op: => Unit) = op
breakable: (op: => Unit)Unit
scala> breakable { println("i m learning fp") }
i m learning fp
The other thing you will see is function taking function as argument, as in Functor, Monad etc.
For example, map
/ flatMap
on List[Int]
would be
final override def map[B](f: A => B): List[B]
final override def flatMap[B](f: Int => scala.collection.IterableOnce[B]): List[B]
In scala function can take values(pre-computed values or not-yet-computed values) along with function as arguments.
So, function which takes computed values as argument,
scala> def f(a: Int) = a * 100
f: (a: Int)Int
Example of function which takes a block of code returning Int
(not yet computed) , block of code here could be just anonymous or existing method(not a function; that's where things get messy)
scala> def f(a: => Int) = a
f: (a: => Int)Int
and to use it,
scala> def a: Int = 100 * 100
a: Int
scala> f(a) //a returns Int so confirms a: => Int
res3: Int = 10000
In your example, def breakable(op: => Unit) { ... }
takes a argument which WILLL be computed as Unit
.
For example, you can pass println
, which returns Unit
,
scala> def breakable(op: => Unit) = op
breakable: (op: => Unit)Unit
scala> breakable { println("i m learning fp") }
i m learning fp
The other thing you will see is function taking function as argument, as in Functor, Monad etc.
For example, map
/ flatMap
on List[Int]
would be
final override def map[B](f: A => B): List[B]
final override def flatMap[B](f: Int => scala.collection.IterableOnce[B]): List[B]
edited Nov 17 '18 at 8:18
answered Nov 17 '18 at 7:23
prayagupdprayagupd
20.5k893144
20.5k893144
2
op
is not a function parameter, it is a call-by-name parameter, so you need to edit your explanation,
– Tim
Nov 17 '18 at 7:44
add a comment |
2
op
is not a function parameter, it is a call-by-name parameter, so you need to edit your explanation,
– Tim
Nov 17 '18 at 7:44
2
2
op
is not a function parameter, it is a call-by-name parameter, so you need to edit your explanation,– Tim
Nov 17 '18 at 7:44
op
is not a function parameter, it is a call-by-name parameter, so you need to edit your explanation,– Tim
Nov 17 '18 at 7:44
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%2f53349074%2fneed-help-understanding-the-syntax%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
Possible duplicate of Scala: What is the difference between (a: String) and (a: => String) for argument?
– Robin Green
Nov 17 '18 at 7:46
1
@RobinGreen The problem might be the lack of
=
so I can't tell if this is a duplicate or not. Or rather, it is probably a duplicate of two different questions!– Tim
Nov 17 '18 at 7:54