Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #39716

Re: input/output parameters

From Anton Shepelev <anton.txt@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: input/output parameters
Date 2021-03-20 14:49 +0300
Organization A noiseless patient Spider
Message-ID <20210320144942.f32b26506f67afa7a039ec2a@gmail.com> (permalink)
References <20210313153232.40fa0c8b0adf07d7e807084d@gmail.com> <slrns4plpr.4fd.jon+usenet@raven.unequivocal.eu> <20210314185124.0bcbab76d172caf076ac3839@gmail.com> <e52cbce2-ffaa-43f3-8d0f-6f7c82950599n@googlegroups.com>

Show all headers | View raw


Michael Haufe to Anton Shepelev:

> > What is the JavaScript style of implementing TryParse()
> > and TryGetValue()?--
> > TryParse: https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse
>
> ```
> let x = parseInt('145')
> ```
>
> x is the number or NaN if it failed.

I know about parseInt(), but its interface is different: it
has no out parameter and no boolean return. Therefore, it
cannot be used inside a condition without additional
trickery, e.g.:

   var i;
   if( isNaN(i = parseInt(`d5`)))
       console.log("Not a number."); else
       console.log(`Parsed. The double is ${2*i}.`);

> If you want a default value:
>
> ```
> let x = parseInt('not a number') || 12;
> ```
> Note in the latter case '0' would be treated as a failure.

No, not in this case, but thanks for pointing out the
possibility.

> Slightly longer alternative:
>
> ```
> let parseIntOrDefault = (x, defaultValue) => {
>   let result = parseInt(x, 10)
>   return isNaN(result) ? defaultValue : result
> }
> ```

That is what I would use if I needed a default value:

   function parseIntDef(s, def)
   {   var res;

       if( typeof(s) != "string" )
       {   throw new Error("`s' is not a string.");  }

       if( typeof(def) != "number" )
       {   throw new Error("`def' is not an int.");  }

       res = parseInt(s);
       if( isNaN( res ) )
       {   res = def;  }
       return res;
   }

> > TryGetValue: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.trygetvalue
>
> In JavaScript operations belong to the object and not to
> the type.

I try not to rely on it, preferring to pass all the
necessary data in parameters.

> The default value is supplied by you and not implied by
> the type system.

I do not care about any implied default values, because I
use the out parameter only if the invocation succeeded. If
it failed and returned false, it means the dictionary does
not contain the key, and I can provide my own default value,
should I so desire.

> > while( read_next( stream, out element )
> > { // process the element
> > }
> >
> > What alternative(s) does JavaScript propose?
>
> JavaScript has iterators and generators for this style of
> coding:
>
> <https://developer.mozilla.org/en-
> US/docs/Web/JavaScript/Guide/Iterators_and_Generators>

I think it is rather complicated approach that goes way
beyond procedural programming and relies on object-oriented
and functional mechanisms (with `yield' implemented as an
entire state machine under the hood!), when all I ask for is
a simple function. OK, I can write it in JavaScript by
passing it a callback:

   function read_next( stream, set_elem )
   {  var res;

      res = true;
      if EOF( stream ) res = false;
      else             set_elem( NEXT( stream ) );

      return res;
   }

But obviously it clumsier than a plain output parameter,
about which you write:

> The problem is that it makes local reasoning more
> difficult. I can not simply look at a function call and
> know that it's simply returning a value, I'd have to
> inspect the arguments to see what side-effects are
> potentially happening in my scope.

In that sense, OOP is all about such side-effects, where a
method call may change the state of data that is not
explicitly passed to it, but is buried somewhere in `this'.
If a function changes the value of an `out' parameter, it is
not a side effect but an explicitly specified operation that
in many languages is visible in the call:

   C : hasData = ReadNext( &stream,     &element );
   C#: hasData = ReadNext(  stream, ref  element );

Both of these calls change the state of `stream' and, if a
new element has been read, put it into the `element'
variable.

Remember also that I am talking about *scalar* output
parameters. They are entirely free of side effects because a
function cannot partially modifythem, as it can modify some,
but not all, fields of an object. Therefore, modification of
object members (allowed in JavaScript) is actually less
transparent than modification of scalar values (not allowed
in JavaScript).

-- 
()  ascii ribbon campaign -- against html e-mail
/\  http://preview.tinyurl.com/qcy6mjc [archived]

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-13 15:32 +0300
  Re: input/output parameters Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-03-13 15:16 +0000
    Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-14 18:51 +0300
      Re: input/output parameters Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-03-14 16:15 +0000
        Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-03-14 18:31 +0000
          Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-14 22:27 +0300
            Re: input/output parameters Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-03-14 23:44 +0000
            Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-15 06:41 -0700
        Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-14 22:17 +0300
          Re: input/output parameters Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-03-14 23:28 +0000
          Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-15 06:39 -0700
      Re: input/output parameters Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-03-14 22:32 +0000
      Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-15 06:36 -0700
        Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-20 14:49 +0300
          Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-20 11:59 -0700
            Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-20 13:07 -0700
          Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-03-20 19:29 +0000
            Re: input/output parameters Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-03-20 20:35 +0000
  Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-15 00:27 +0300
    Re: input/output parameters Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-03-14 23:51 +0000
      Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-20 14:56 +0300
        Re: input/output parameters Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-03-20 15:08 +0000
          Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-21 15:16 +0300
            Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-21 08:46 -0700
              Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-04-10 14:54 +0300
        Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-20 12:05 -0700
          Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-21 15:58 +0300
    Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-15 06:43 -0700
    Re: input/output parameters Silvio <silvio@internet.com> - 2021-03-15 18:01 +0100
      Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-03-16 18:06 +0000
        Re: input/output parameters Silvio <silvio@internet.com> - 2021-03-16 23:58 +0100
          Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-03-17 19:14 +0000
            Re: input/output parameters Silvio <silvio@internet.com> - 2021-03-17 23:46 +0100
              Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-03-18 18:04 +0000
                Re: input/output parameters Silvio <silvio@internet.com> - 2021-03-22 00:39 +0100
                Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-03-23 10:44 +0000
                Re: input/output parameters Silvio <silvio@internet.com> - 2021-03-30 00:33 +0200
                Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-03-31 19:53 +0100
                Re: input/output parameters Silvio <silvio@internet.com> - 2021-04-01 01:17 +0200
                Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-04-01 18:02 +0100
      Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-20 15:50 +0300
        Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-03-20 18:52 +0000
          Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-21 17:35 +0300
            Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-21 08:50 -0700
              Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-21 19:31 +0300
            Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-03-21 19:27 +0000
        Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-20 13:02 -0700
  Re: input/output parameters Postiljon Petskin <yyyyyyyyyyyywwwww@zohomail.eu> - 2023-04-22 08:47 -0700

csiph-web