Understanding implicit in Scala

I was making my way through the Scala playframework tutorial and I came across this snippet of code which had me puzzled: def newTask = Action { implicit request => taskForm.bindFromRequest.fold( errors => BadRequest(views.html.index(Task.all(), errors)), label => { Task.create(label) Redirect(routes.Application.tasks()) } ) } So I decided to investigate and came across this post. I still … Read more

When should I use the new keyword in C++?

I’ve been using C++ for a short while, and I’ve been wondering about the new keyword. Simply, should I be using it, or not? With the new keyword… MyClass* myClass = new MyClass(); myClass->MyField = “Hello world!”; Without the new keyword… MyClass myClass; myClass.MyField = “Hello world!”; From an implementation perspective, they don’t seem that … Read more

Passing a dictionary to a function as keyword parameters

I’d like to call a function in python using a dictionary. Here is some code: d = dict(param=’test’) def f(param): print(param) f(d) This prints {‘param’: ‘test’} but I’d like it to just print test. I’d like it to work similarly for more parameters: d = dict(p1=1, p2=2) def f2(p1, p2): print(p1, p2) f2(d) Is this … Read more

What is the native keyword in Java for?

While playing this puzzle (It’s a Java keyword trivia game), I came across the native keyword. What is the native keyword in Java used for? 10 s 10 Minimal runnable example Main.java public class Main { public native int square(int i); public static void main(String[] args) { System.loadLibrary(“Main”); System.out.println(new Main().square(2)); } } Main.c #include <jni.h> … Read more

Does the ‘mutable’ keyword have any purpose other than allowing the variable to be modified by a const function?

A while ago I came across some code that marked a member variable of a class with the mutable keyword. As far as I can see it simply allows you to modify a variable in a const method: class Foo { private: mutable bool done_; public: void doSomething() const { …; done_ = true; } … Read more

Difference of keywords ‘typename’ and ‘class’ in templates?

For templates I have seen both declarations: template < typename T > template < class T > What’s the difference? And what exactly do those keywords mean in the following example (taken from the German Wikipedia article about templates)? template < template < typename, typename > class Container, typename Type > class Example { Container< … Read more