Why NGS has no “undefined”

Since I know JavaScript, some Ruby and a bit of Perl which all have the concept of undefined it was a decision I had to make whether I implement undefined in NGS. This article shows why I decided not to have the undefined value/data type.

Update (thanks /u/EldritchSundae): what you observe in Ruby example below is nil, not undefined. In bash the undefined value is empty string. Ruby does not have undefined but it has the ability to read non-existing hash keys without causing an exception like JavaScript and Perl. In Ruby’s case the result is nil, not undef (Perl) or undefined (JavaScript).

Undefined in other languages

Showing here few common cases, not all possible usages.

JavaScript

> nodejs -e 'const a; console.log(a)'
undefined

> nodejs -e 'const h={}; console.log(h["xyz"])'
undefined

> nodejs -e '(function f(a,b) { console.log(a,b) })(1)'
1 undefined

Ruby

> ruby -e 'h={}; puts h["xyz"]' # outputs empty line

Perl

> perl -e '%h=(); print $h{"xyz"}' # outputs nothing

bash

> bash -c 'echo $a' # outputs empty line

Absence of undefined in NGS

Adding yet another data type to NGS needs justification. I can’t find any justification for undefined. I do consider the usages above bugs. Accessing a variable or a place that were not assigned any value is an error.

Conveying absence of a value in NGS is done similar to other languages with the special null value. There are also somewhat experimental Box, FullBox and EmptyBox types, similar to Option, Some and None in Scala.

Undefined as a hash value for non-existing keys

Having undefined returned when looking up non-existing hash key is a trade-off. It’s more convenient and more error-prone. I have chosen Python-like approach: it’s an error.

> ngs -e 'h={}; h["xyz"]'
... Exception of type KeyNotFound ...

# and added convenience method "get"
> ngs -p 'h={}; h.get("xyz", "NONE")'
NONE

Undefined when accessing unset variable

While bash gives you an empty string by default and Perl gives you undef, I do think accessing unset variable is definitely a bug. I guess it was understood at some point by the creators of bash and Perl so bash has -u flag that makes accessing undefined variable an error and Perl has use strict mode which does the same among other things.

> bash -c 'echo $a' # no error
> bash -c 'set -u; echo $a'
bash: a: unbound variable

> bash -c 'a=(); echo ${a[0]}' # no error, just horrible syntax :)
> bash -c 'set -u; a=(); echo ${a[0]}'
bash: a[0]: unbound variable

> perl -e 'print $a' # no error
> perl -e 'use strict; print $a;'
# no error - I have no idea why, probably some "special" variable

# Perl - take number two:
> perl -e 'print $abc' # no error
> perl -e 'use strict; print $abc;'
Global symbol "$abc" requires explicit package name
(did you forget to declare "my $abc"?) at -e line 1.
Execution of -e aborted due to compilation errors.

Undefined as value for parameters without arguments

Calling an NGS function with less arguments than it expects is an error as in most languages:

> ngs -e '(F (a,b) 10)(1)'
... Exception of type ArgsMismatch ..

By the way, I do cringe every time I see JavaScript code that explicitly uses undefined:

function f(optional_a, optional_b) { }
f(undefined, 10)

The programmer took a decision not to pass a value. How in the world is this undefined? Use null for f*ck sake!


Have a nice day!

6 thoughts on “Why NGS has no “undefined”

  1. i agree, if you access an undefined variable, it is a bug. if it isnt, im happy to back to the days when the variable was automatically set to 0. but for most modern uses, its safer to consider a bug.

    in my own language, you can declare AND zero a variable by starting a line with it:

    p # there, its 0

    but if you try to reference a variable that isnt set, it behaves identically to python: it is a bug. i wanted to make it easier, which is why it gets zeroed the first time you mention it. but it is meant to transition someone to python, so i didnt make it possible to reference an un-set variable. i just made it easier and more standard to set it.

    Liked by 1 person

      • python 2 source: https://github.com/PLinux99/figos/blob/master/fig41.py

        pdf draft tutorial: https://codeinfig.wordpress.com/2016/11/28/a-note-to-anna-and-a-link-to-the-book/

        “How do you know it should be a zero and not an empty string/array/hash?”

        only because it was inspired by basic (thats ok, its heavily inspired by python too) and basic used 0. to make it an array (list): p 5 arr # set p to 0, change p to 5, convert to array containing 5 as the first element

        like python, you can multiply the array by an integer to say, create an array of 10000 zero-length strings. however, an array in fig always has at least one element (which can be a 0, an empty string, or anything else.)

        nearly contradicting what i just said, this is also possible in fig:

        p arr # set p to [0]
        python
            p = []
        fig
        p print # print empty array

        Liked by 1 person

      • its a source-to-(python)source compiler, so the variables are typed, but dynamic/duck-typed.

        the arr command takes whatever variable starts the line and converts it to an array with the same value as the first element, so p “” times 10000 # works. if you like semicolons, p=”” ; arr ; times 10000 ; print ; # works too

        Liked by 1 person

      • schools are going drag-and-drop and python on the pi, which is fine with me. these languages have foundations to promote to institutions– fig is more modest. its really designed for tutoring and it works (great) for tutoring.

        when i was developing it, i was in touch with more programs that i could have used it for in a more professional (and larger) setting. ive had to move around a bit for personal reasons, staying the same region but losing proximity to old contacts and connections.

        in short, its experimental. ive had mini teaching sessions with a number of individuals, enough to write about initial experiences. id like to do more of this sort of teaching.

        25 years of basic and getting closer to a decade of python, what im trying to teach/offer is variables/i/o/basic math/loops/conditionals/functions.

        the subject is digital literacy, and the vehicle is a simplified code that transitions rather smoothly to python but has every distraction (from variable assignment to most syntax) tucked away as optional and not what you have to start with. if i wasnt taking non-coders and introducing them to code that looks so much less like math (i know too many math-phobic people– i fully understand that its unavoidable), i would still be encouraging every non-coder to learn how to code, and every coder to try creating a toy language.

        the reason is this: i dont think we can afford a computer illiterate society anymore– and application use isnt good enough, not close.

        if we had more toy languages, most wouldnt get far, but it would incubate the next generation of (for example) educational languages. weve been reusing ideas from basic and pascal and logo forever– i do too– but more ideas, thats what i want to come out of the future of language design.

        Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s