Hacker News new | ask | show | jobs
by thomasz 5011 days ago
This looks promising. Nothing that has never been done before, but I really like the support for baked in doctests and contracts. This kind of sugar can make a real difference in terms of quality and readability

    #from the website, because it seems to be down for many right now.

    class SmallSample
	var _random = Random() 
	def randomString(length as int, alphabet as String) as String
		require
			length > 0
			alphabet <> ''
		ensure
			result.length == length
		test
			utils = SmallSample()
			assert utils.randomString(5, 'ab').length == 5
			s = utils.randomString(1000, 'a')
			for c in s, assert c == 'a'
		body
			sb = StringBuilder()
			for i in length
			c = alphabet[_random.next(alphabet.length)]
			sb.append(c)
			return sb.toString
2 comments

On that example, the syntax looks like Python with a bit of BASIC (the type declarations). That "StringBuilder" object reminds me of Java, I would prefer a concatenation operator.

Seems that doesn't use explicit variable declaration. Supporting that would be a great improvement over Python.

It has explicit variable declaration. You can always do: varname as Type = expression

You often won't see this for the sake of succinctness. I might have misunderstood what you were saying but thought I'd mention that.

I've always wanted to work with a language that supports contracts (originally Eiffel I guess). Would love to see this hacked into Go. Or maybe I shouldn't turn this into a Go thread and start using Cobra.
> I've always wanted to work with a language that supports contracts (originally Eiffel I guess). Would love to see this hacked into Go.

There have been a few attempts to hack (design by) contracts into Perl. Here are some which can be found on CPAN:

* Class::Agreement - https://metacpan.org/module/Class::Agreement

* Class::Contract - https://metacpan.org/module/Class::Contract

* Sub::Contract - https://metacpan.org/module/Sub::Contract

Sub::Contract is my favourite because it's lightweight, pragmatic & with a more Perlish implementation.

If you work with .net, the code contracts project[1] is halfway there: It's quite ugly and they made some design decisions I don't agree with (for the wrong reasons, i guess...), but it has had a tremendous impact on the quality of my code.

http://research.microsoft.com/en-us/projects/contracts/