funq: syntax that doesn't scare off C++ developers

Aaron J. Seigo aseigo at kde.org
Fri Jul 25 08:53:05 UTC 2014


Hi ...

So .. first things first: the git url I included only works for people who have 
a KDE commit account. Sorry about that :) Here is the public url that should 
work for everyone:

	git clone git://anongit.kde.org/scratch/aseigo/funq

OK.. so the first thing that I'd really appreciate feedback on is the general 
feel of the funq syntax. While it intends to be a pragmatically functional 
language[1], I have tried to make it *look* more familiar. One developer I 
showed it to without telling them what it was asked if it was Java :)

The intention there is to make it easier for developers mostly familiar with 
the Algol family of languages (C, C++, Java, Python ...) to be able to 
understand and write the code without too much rewiring of their brain.

The other primary goal with the syntax is to keep it simple through clarity, 
consistency and trying to always provide "just one way to do things".

So here is the basic anatomy of a funq function:

tags functionName(parameter list) : preconditions
{
	code block
}

Only the function name and code block are required and they do what one would 
expect. 

We can leave function tags for later discussion, but they offer hints to the 
compiler as to how the function is intended to be used (allowing additional 
compiler checks and some useful introspection tricks).

The parameter list supports pattern matching. There is an example of a (not 
very efficient) fibonacci number function in the file:

    fibonacci(1) { 0 }
    fibonacci(2) { 1 }
    fibonacci(x) : std::isType(x, std::Int) && x > 2 
    {
        fibonacci(x - 1) + fibonacci(x - 2)
    }

These are actually the same function, and which version of them is called 
relies on the value of the parameter passed. fibonacci(1) calls the first 
version; fibonacci(2) calls the second; calling it with >2 calls the third 
version. any non-int or integer less than 1 will result in an error due to no 
matching function.

Preconditions optionally follow the the parameter list and allow various 
boolean checks to be made on the parameters. The third fibonacci function 
version checks that x is an integer and greater than 2.

A function version is only called if both the parameter list and preconditions 
match.

There are no return types; functions are free to return different types.

Variables are single-assignment (you can't assign a different value to a 
variable once it is set) and typing is inferred (but not dynamic, due in part 
to single-assignment). Due to single-assignment, there is no need for a 
comparison operator (==). The assignment operator does both assignment and 
checking. So:

	x = 1; // x is now 1.. obvious :)
	x = 2; // false!
	1 = x; // true!
	x = "An awesome string"; // false!
	y = x; // true! y now also equals 1
	z = 3; 
	y = z; // false!
	y = x; // true!

In expressions expecting booleans such as the preconditions clause, true/false 
is the result of '='. In a function body '=' generates no error or an error 
corresponding to true / false. On error a function stops processing and 
returns an error at that point which, if not caught, will result in the 
process it is running in to crash. Sort of like an assert.

That should hopefully give you enough information to be able to get a general 
feel for the code in examples/syntax_samples.fqt 

I would love to hear your thoughts on:

* what you found not clear enough / easy enough to read

* any inconsistencies you spot

* any ideas for improving readability and ease of writing new code

Nothing is yet set in stone, so we can revise anything we wish. Use a critical 
eye, and at this point there are no stupid questions, no bad observations, no 
wrong ideas. I'll take all feedback offered under consideration.

Once syntax is basically "OK" we can move on to more interesting things, such 
as processes. This will likely be an iterative process where we revisit basic 
syntax as the language features are layered up on it. I've already done a 
significant number of iterations to just get it this far, and I expect several 
more will be in order. :)



[1] it won't be a "pure" functional language as I feel that, as good as such 
things are, it creates complexity that may discourage usage by people used to 
Qt and C++

-- 
Aaron J. Seigo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
URL: <http://kde.org/pipermail/funq-devel/attachments/20140725/c52fe9fb/attachment.sig>


More information about the Funq-devel mailing list