Call-by-value vs Call-by-name

13 views
Skip to first unread message

Gavin Baumanis

unread,
Jan 11, 2018, 5:04:07 AM1/11/18
to Melbourne Scala User Group
Hi there everyone,

I understand the difference between call-by-value and call-by-name.

And logging (or the side of effect of time, via logging) seems to be used in all the examples to show their difference, too.

I have also read that as a preference, you should choose call-by-value orver call-by-name.

My question is;
If I want the behaviour in my logging - that each instance of time I use - I want "the" time AT the time of use...
Do I construct my code to use call-by-value, xxx times as required?

or am I being overly rigid and I just use call-by-name as that seems to fit the bill - perfectly. (for this use-case)

Thanks!

Ben Hutchison

unread,
Jan 14, 2018, 7:31:24 AM1/14/18
to scala...@googlegroups.com
Hi Gavin,

re "a preference, you should choose call-by-value over call-by-name", I rarely find such unconditional rules of thumb to be useful myself. Context matters. 

All I can advise is "you should understand the characteristics of call-by-value and call-by-name so you can use them both appropriately". Scala is strict, ie defaults to call-by value, so if you're working in Scala you'll have to deliberately choose to use call-by-name. 


With your second question, its hard to give a clear cut answer when the structure of the problem is vaguely described. But guessing what your code might look like, then yes I think you want call-by-name.

-Ben

--
You received this message because you are subscribed to the Google Groups "Melbourne Scala User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-melb+unsubscribe@googlegroups.com.
To post to this group, send email to scala...@googlegroups.com.
Visit this group at https://groups.google.com/group/scala-melb.
For more options, visit https://groups.google.com/d/optout.

Luke Stephenson

unread,
Jan 15, 2018, 10:19:17 PM1/15/18
to Melbourne Scala User Group
Hi Gavin,

My understanding is that log frameworks often use call by name to avoid the overhead of building the log message unless the logging is enabled.  Eg without call by name you might see something like:

if (log.isDebugEnabled()) 
   log.debug("User logged in " + user)

The isDebugEnabled() is used to avoid the overhead of building the String to be logged if not required.  However if the log message is passed by name, then there is no need for this and you can just do:

log.debug("User logged in " + user)

That's pretty general and not related to your question. I don't understand your problem space well enough to comment.

Luke
Reply all
Reply to author
Forward
0 new messages