!_ equivalent to passing false?

10 views
Skip to first unread message

abra...@brightcove.com

unread,
Feb 24, 2015, 5:49:21 PM2/24/15
to stream...@googlegroups.com
Hi all,

I was reading about Streamline's support for Futures.  The documentation says that, in order to receive a Future from a streamlined function, you must pass !_ in place of the callback.  Looking at the generated source code, it looks like the !_ exists verbatim in the streamlined code, such that you are effectively passing the value false.

Am I understanding the intended behavior correctly?  Could I pass "false" in place of !_ without breaking anything?

// test._js
// Will this work as expected?
function asyncFn(_) {  return 'foobar';  }
var future = asyncFn(false);
var result = future(_);

Am I understanding correctly that, with --promises, passing false will create a future, while passing null or undefined will create a promise?

Thanks in advance,
Andrew

Bruno Jouhier

unread,
Feb 25, 2015, 2:37:52 AM2/25/15
to stream...@googlegroups.com
Today they are equivalent except in fast mode where !_ is transformed in a special way. See https://github.com/Sage/streamlinejs/blob/master/lib/fibers-fast/transform.js#L271-L292

There is also issue #181 to take into consideration. If I implement it, I will have to change the way !_ is handled. The test on futures will need to be done on the caller side rather than the callee as today. So code generation will change and may break this assumption.

Bruno

Andrew Bradley

unread,
Feb 25, 2015, 6:44:38 PM2/25/15
to stream...@googlegroups.com
Ok, that makes sense, thanks for the quick reply.

Would there be a way for me to change the "!_" to some other construct or identifier, for example "FUTURE", even if it was a compiler API option that wasn't available from the command line?

I ask because I'm considering using Streamline with TypeScript, and trying to figure out how to make it play nice with the type system.  (this would never be possible if streamlined code wasn't valid Javascript syntax, so thank you for that!)

When the TypeScript compiler sees "!_" it will assume the value is "false" and the type is "boolean."  However, if I'm able to use an identifier like FUTURE instead of !_, then I can make my own type for FUTURE and use that type in streamlined function signatures.

// Make the type system happy
interface Callback {
    (err: any, value: any): void;
}
declare var FUTURE: ReturnAFuture;

// Declare a streamlined function with several function signatures
function asyncAdd2(n: number, _: Callback): number;
function asyncAdd2(n: number, _: ReturnAFuture): Future<number>;
function asyncAdd2(n: number): Promise<number>;
function asyncAdd2(n: number, _: any) {
    return n + 2;
}

// Use the streamlined function
var future = asyncAdd2(3, FUTURE);
var result = future(_);

I'm still thinking about if this is a sensible idea, so any wisdom anyone else can share would be great.

--
You received this message because you are subscribed to the Google Groups "streamline.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to streamlinejs...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bruno Jouhier

unread,
Feb 27, 2015, 3:20:54 AM2/27/15
to stream...@googlegroups.com
It wouldn't be too difficult to add. I chose !_ because I wanted to reserve only one identifier.

In the mean time you could get around with `boolean` in the signature. 

One subtle issue with !_ is that it can be used in contexts where _ is not defined. For example the following is valid:

function foo() { return bar(!_); }

TypeScript may bark at it because _ is not defined. It won't bark at return bar(false);

Bruno
 
To unsubscribe from this group and stop receiving emails from it, send an email to streamlinejs+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages