Feature req: `undefined`

Hi!

In Haskell I find the undefined function from prelude really useful. I’m guessing it’s defined as something like:

undefined : _|_
undefined = error "Undefined"

This seems like a useless function, but allows you to write code like …

myverycomplexfunction : Int -> Bool -> Text -> a -> Int
myverycomplexfunction = undefined

… which then compiles.

Which is great for prototyping; it let’s you write out your program ‘top-down’, and get it to compile first, before going in to implement each individual bit.

Of course you can just write your own undefined, but it would be nice if it were part of DA.Prelude.

Is there any obvious reason why this has been omitted from the standard library? Could we add it?

3 Likes

I know it’s not your main point, but just FYI, in Haskell, undefined :: a, because it can return any type. I expect it would be the same in DAML, modulo a colon.

error has the type Text -> a, so that lines up.

And FWIW, I’m all for defining this in the Prelude.

2 Likes

The DAML Preluide/StdLib are build from scratch rather than derived from Haskell’s by removing unwanted functions. So it’s not so much that it was omitted on purpose, but that nobody has added it.

Given the simplicity of the function, why don’t you just submit a PR to add it?

2 Likes
undefined : a
undefined = error "undefined"

works in DAML. If you want to follow @bernhard’s suggestion, you could submit a PR adding that to the end of compiler/damlc/daml-stdlib-src/DA/Internal/Prelude.daml.

Note: see also @Tamas_Kalcza’s similar request: #7058. I personally prefer the name undefined but agree either way it’s a worthwhile addition to the language.

Thanks for all your feedback. Yes, I will try and submit a PR when I have a spare moment (to attempt and build the SDK :grin: - will be fun I can tell)

The PR linked above Added undefined function (#7058) · digital-asset/daml@9d0206f · GitHub has been merged and released in SDK 1.6.0.

1 Like

Reading that PR you had an interesting suggestion I thought worth sharing:

You can use _ on the RHS. By default it produces a compile-time error and displays you the expected type as well as a list of potential things that have that type. Super useful during development.

If you want to run your code, there is --ghc-option=-fdefer-typed-holes which will turn this into a runtime error (you still get a compile-time warning).