210 points · by pjmlp · 8y ago · 156 comments
The need for nested functions bugged me as well. That seems like an odd deal-breaker and hints at code smell, rather than a genuine technical need. If it's just for the sake of one-to-one transpilation, then I can see the point, but a refactoring exercise seems like a better effort to make than language migration. (Also, don't Rust and Go have closures? Surely that would satisfy this requirement.)
Now you understand how all of these "How we chose this language" writeups work.
Rust does support nested functions that don't close over their environment, and has a separate syntax for closures which do, and closures can either move or borrow their captured variables. This makes it a bit harder to write a translator that produces good, idiomatic results; you could always use a closure that borrows the closed over variables, which I think matches the Pascal semantics, but that would mean the code would look a little odd for cases in which you had a nested function just to factor out some functionality and didn't need the closure.
fn main() {
let a = 10;
fn function() {
println!("Hi!");
// println!("a is {}", a)
// fails with:
// can't capture dynamic environment in a fn item
// use the `|| { ... }` closure form instead
}
let borrow_closure = || println!("a is: {}", a);
let move_closure = move || println!("a is still: {}", a);
function();
borrow_closure();
move_closure();
}My experience as a language designer is that syntax matters, and leveraging the users' existing knowledge is worthwhile.
He mentioned an awful lot of code that would have to be refactored, and it could be that they are just too far down the rabbit hole to get themselves out.
That solution is probably the lowest cost / safest course... Assuming the Extended Pascal compiler they have supports it.
But I suspect choosing D was a fait accompli.
I have never seen a reasoned argument why C++ vs Fortran is better for technical programming.
I don’t think it’s solely dependent on having afforded formal training.
Knowing enough to know that you might not know and just might need to measure or look something up and caring enough to do so is more of the core of it.
I think the author makes it clear that direct conversion from Pascal is the primary goal. I'm guessing that once the codebase is fully transpiled to D, refactoring would be the logical next step.
Indeed they do. It's completely unclear why they don't satisfy the OP's requirement.
x = func blah() { ... }
there is a conflict between "x is the only new binding we should have as a result of executing that line" and "the function name 'blah' should do some sort of binding".)If that's enough to throw your translation process, you've got bigger problems....
(Cards on the table, D is probably a good choice and I'm not advocating for Go here.)
It's unclear where the root of that confidence lies. The translation is just as mechanical in either language as it would be otherwise: see an inner function, replace it with the one thing that can capture variables. Speaking for Rust, it would actually be even easier to do the translation using closures, because Rust closures can have their signatures inferred whereas Rust functions cannot.
"In Rust, only lambdas (unnamed functions) can be closures".
The OP's post seems to show they didn't spend much time understanding whether Go closures could serve their need. Although from my own superficial understanding, D is "closer" to Pascal than Go.
Nested functions were a very common pattern on languages like Pascal, Modula or oberon. They are equivalent to code blocks inside a function in C.
When talking about turing complete languages, everything is an ergonomic nice-to-have. All language features aspire to be so nice to have, that you come back around and call it a necessity. But you can still express a program without them, unless you strip out so much as to lose turing completeness.
(I'm generally pretty skeptical of the whole "write down your requirements with no awareness of available solutions and then choose a solution that meets them" approach to decision-making).
Maybe I've misunderstood, but aren't Rust's closures [1] exactly what the author is describing? Specifically, you can capture variables from the environment into functions.
[1] https://doc.rust-lang.org/book/first-edition/closures.html
[1] https://users.rust-lang.org/t/inner-functions-not-closed-ove...
This is achieved by adding a hidden parameter that is a pointer to the enclosing function's stack frame. This forms a threaded list so variables in nested enclosing functions can be accessed by walking that list.
This is the same way Pascal does it.
It also means that taking the address of a nested function produces a pair - a pointer to the function, and this hidden parameter to the enclosing stack frame.
* http://jdebp.info./FGA/metaware-iterator-driven-for.html
In theory, the extra frame pointer mechanisms of the ENTER and LEAVE instructions in the Intel ISA were there for supporting this kind of nested function. MetaWare High C/C++ did not use them, preferring to use a variant calling convention where (if I recall correctly) ECX contained the extra hidden parameter.
Here's a Rust version that does the same (without heap allocation!): https://play.rust-lang.org/?gist=bb5406062970b62088d58fb8a9f...
Once "non-lexical lifetimes" lands, the inner scope won't be necessary. That may have tripped the OP up.
Generally, I prefer mainstream languages myself, that’s why I mostly code C++ and C#. But “generally” is the key here. For some projects, or some parts of it, picking a non-mainstream language can be a huge win in terms of productivity.
I never used D in production, but I can remember several occasions where I picked unusual languages, both specialized and general purpose ones. Among others, I’ve used XSLT, sed, awk, VBScript, VB.NET, R, power shell, AutoLISP, MATLAB.
Theoretically all modern programming languages are equivalent because Turing-complete. Practically, for a particular problem, it can be orders of magnitude difference in development time between them. The reasons include availability of specific libraries, integration with other code be it third-party or legacy, or specific runtime features.
I'm not surprised to see criticism for D on HN. While reading HN comments on posts related to D, I've always found pro Rust/Go comments.
1)Expressiveness in the given problem domain. Some languages are really great in particular niches.
2)Availability of devs. The ability to hire and retain a really great team makes or breaks projects, and while a smart, motivated dev can learn any language, it's definitely a factor in their choice to work or not work with you. If you're in a niche language it will greatly affect the ability of people to use external resources (stackoverflow etc) if they hit blockers and some people will be reluctant to railroad their career into a tech that doesn't appear to be going to the mainstream. On the flipside, if you're lucky you may be able to get really passionate devotees of the language to work for you just to use the language where they might not have done it otherwise.
With all that said... Lisp' siren call is still out there, and experience tells me it will be the same. (As it was once before)
This was 15 years ago, but I honestly don't know what language I would use today. It's funny but sometimes binary data handling isn't a particular language's strong suit simply because they don't have the primitives to handle it, or if they do, the APIs to handle them are awkward or unnecessarily verbose. It's kinda neat that in C/C++ you can take a 1MB byte buffer and pass it to a function starting at 30% in. In other languages its like you have to pass in the whole thing and another parameter to tell it where to start. I get that's why C/C++ is in the situation it's in (security - but that's why I don't use C/C++ to handle web requests), but sometimes tasks need that.
Could make use of a saner type system, nice frameworks (Turbo Vision, OWL), while being able to painless create safe wrappers around C libraries.
> motivated dev can learn any language
The key word is "motivated". That's why I'm an expert user of C++98 and D but I can't learn Go, Python, C++11, etc.
> able to get really passionate devotees of the language
That.
I lol'd at this
But I've found that lots of places seem to be focused on if you know X rather than being a well versed, developer who enjoys growth. And it feels more and more like the reason is because they don't expect anyone to stay there for more than a few years tops.
I feel that splitting as much as possible the project into chunks/components and then replacing them one by one sounds more realistic (with the appropriate test harness). Maybe that is the plan but it is not touched in the post.
And then maybe an issue that is not that bad, but you are going to have a hard time comparing old_codebase with new_codebase once you've switched. If you have to maintain old branches, porting the fix to recent branches will be more time consuming that simply do a merge.
I think the Go compiler did something similar.
It certainly requires iteration of continuously attempting the translation until it works, and fixing bugs either in the transpiler or in the original codebase. But once everything seems to be in order, you can do the transition all at once.
It sounds like the author has never transpiled a large code base into D before.
My hobby project languages or choice are currently D and Kotlin. I prefer the semantics and flexibility of D, but Kotlin has the backings of the tried and tested Java ecosystem, which is hard to beat when you just want to get down to it. Also web servers in Java are vastly superior at the moment. Vert.x is so very fast.
https://dlang.org/blog/2018/06/11/dasbetterc-converting-make...
I'm really sad to see that the author didn't even consider the Nim programming language. They could have saved themselves a LOT of work, Nim's original compiler was written in Pascal and then translated to Nim using a `pas2nim` tool that is freely available on GitHub[1].
Out of Go, Rust and D, Nim is most similar to Pascal by a long shot. Did the author simply miss Nim completely or did they dismiss it for another reason? It seems to me like it would be absolutely perfect for their project.
I'm curious what manual translation and rewriting would be necessary when moving to Free Pascal compared to their Pascal2D transpiler and compatibility library.
I love nested functions. It's good when you need a quick local function, and don't want to expose it to the rest of the code base.
But, how do you systematically unit test nested functions?
You don't. You test the enclosing function. The nested function is just an implementation detail like any other line of code in that function.
Why, with nested unittests, of course! Just put them in a nested type first:
void main()
{
static int nested(int x)
{
return x * 2;
}
struct Test
{
unittest
{
assert(nested(2) == 4);
}
}
}
Well, OK, that's more of a hack than a feature, and you obviously can't test non-static nested functions (as there'd be no way to give values to the outer function's frame variables). But you asked :)You expose it.
If you want to test a local nested function, you test the enclosing function.
Shameless plug/pre-alpha demo: https://www.c2rust.com
Edit: missing word.
Edit: reflect resolved status of issue.
While variables which were declared prior to it in its outer function were available to its inner functions.
Unless there is an obvious example of where this breaks down in Go and Rust, I’d certainly like to know how.
Also I inagine the qt bindings are better in D.
To me, the evaluation goes solely as ecosystem vs. ecosystem
There D is ahead not so much by number of features, than by lack of showstopper flaws
But a killer feature that's available in C++, but not available in D is pure RAII idiom. Sure D does scoped destruction (via struct's destructor), but is in no way equal to the contructor+destructor combination in C++. struct's in D don't have default constructor, so RAII in D is just incomplete.
Automem[1] is a boon for D, but is useful only for heap memory resource. Not for all kinds of resources (like locks, etc).
On the other hand D has scope exit/failure, which is not available in C++.
[1] https://dlang.org/blog/2017/04/28/automem-hands-free-raii-fo...
* D has generally better metaprogramming, however, rust has real macros
* D has a garbage collector and defaults to code being memory-unsafe. Rust uses ownership semantics and defaults to safe code.
* Rust has only an llvm-based compiler (although possibly moving to a self-hosted one soon?). D has a self-hosted, gcc-based, and llvm-based compiler.
Surely Go has this?
func main() { a := 1 func() { b := a g := func() { c := b fmt.Println("c is", c) a = 7 } g() }() fmt.Println("a is now", a) }
The article title is misleading. They still have not migrated.
I mean, they have chosen to migrate. That doesn't imply they're done with the implementation.
There are many companies (e.g. Weka.IO, Sociomantic, etc.) that hire C++ developers and have them happily write D code. I did work at Weka.IO for a while and never met a single D programmer that was unhappy.
Ali Çehreli
(Edited to be less glib. bad habit)
It's not that they're not capable, but some will not want to risk several years of their career to learn it.
(that may not be entirely fair, i guess i just tend to be skeptical of people who get hung up on languages.)
I could see a C++ dev giving this a try without hurting their career prospects, but C# devs are not exactly known for trying out non-mainstream technologies. If they switch for a couple of years MS will pump out a couple of frameworks and .NET programming languages. Fire and motion as such.
Airbnb dropping react native due to a lack of qualified candidates that were articulate in both the native and javascript realm is a great example. Something can work flawlessly with the right conditions (for instance, developers that understand and can implement the path of least resistance across native and react), yet if its prohibitive to create those conditions you are back where you started.
If the dominant ideology among developers is that learning X language or technology costs more of their career than its worth, it dies out. Same goes for the company and their resources.
Erlang has been around in a niche role for >30 years and still going strong, even stronger now that Elixir has reinvigorated interest in the platform.
Yes, they also had difficulties hiring devs which know both native and react, but that's normal. Most mobile devs wouldn't touch react.
Quite the opposite: The new tool saves time and money.
Airbnb example just shows that one should not jump on the newest thing on the market.
Either the company focus solely on finding enthusiasts or they pay the premium to overcome this.
I was assuming a more rational choice but then developing serious technical software in pascal instead of fortran in the 80's was not a rational choice in the first place.