I don't get this syntax - do you?

90 views
Skip to first unread message

der Raab

unread,
Jun 8, 2017, 6:42:12 AM6/8/17
to Haxe
I understand that I'm writing very old fashioned code so I think I'm misinterpreting something very basic. Let's please talk about lines 62 to 68 in https://github.com/kevinresol/tink_http_middleware/blob/master/src/tink/http/middleware/Static.hx which I'll paste here:

var result:Promise<OutgoingResponse> = FileSystem.exists(staticPath) >>
 
function(exists:Bool) return if(!exists) Future.sync(Failure(notFound)) else FileSystem.isDirectory(staticPath) >>
 
function(isDir:Bool) return if(isDir) Future.sync(Failure(notFound)) else FileSystem.stat(staticPath) >>
 
function(stat:FileStat) {
 
var mime = mime.Mime.lookup(staticPath);
 
return partial(req.header, stat, File.readStream(staticPath).idealize(function() {}), mime, staticPath.withoutDirectory());
 
}

I don't get what >> does in this case. 

What I try to do is replacing Future.sync(Failure(notFound)) with a call to my new method _create404Response(): So that the server returns an 404 status if a file doesn't exist.

private function _create404Response() : OutgoingResponse {

return new OutgoingResponse( new ResponseHeader( 404, 'Not Found', [ new HeaderField('Server', "Mine") ] ), "" );
}

I tried all kind of combinations but I either get compile time or runtime errors. 

Hm?

Juraj Kirchheim

unread,
Jun 8, 2017, 7:00:27 AM6/8/17
to haxe...@googlegroups.com
The `>>` is an operator that basically takes a future and a function and tries to match up the types to produce a new future: https://haxetink.github.io/tink_core/#/types/future?id=operators

I think the code could be simplified a bit in two ways:

1. just call stat and use mode to determine if the thing is a directory (i.e. stat.mode & 0x4000 != 0)
2. use promises rather than surprises and rely on .next() rather than funky operators.

Best,
Juraj

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

der Raab

unread,
Jun 8, 2017, 9:20:14 AM6/8/17
to Haxe
Thanks (again!) Juraj, I read the documentation on Future operators and I try to dig thru the concepts, but right now I simply don't find where the Classes or Enums for Success() and Failure() are declared?

der Raab

unread,
Jun 8, 2017, 9:49:36 AM6/8/17
to Haxe
Anyway, I figured out a way that seems to do what I want:

var result:Promise<OutgoingResponse> =

 
FileSystem.exists( staticPath ) >>


 
function( exists : Bool ) return FileSystem.isDirectory( staticPath ) >>

   
function( isDir : Bool ) return FileSystem.stat( staticPath ) >>

   
function( stat : FileStat ) {
 
     
if ( exists && ! isDir ) {


     
var mime = mime.Mime.lookup(staticPath);
     
return partial(req.header, stat, File.readStream(staticPath).idealize(function() {}), mime, staticPath.withoutDirectory());
     
}

     
else {

     
return _create404Response();
     
}
   
}

But it only worked because I made sure partial() or _create404Response() are called in the same level of "nested" Futures. Otherwise I get compile time errors. So I wonder why that is and if there would be a better way listening to Failure() and then return an 404.

This Nested Future thing looks quite abstract to me... :/

Ben Merckx

unread,
Jun 8, 2017, 9:54:48 AM6/8/17
to Haxe
What you're trying to achieve can be done a bit easier: https://github.com/benmerckx/monsoon/issues/7#issuecomment-307067911

Op donderdag 8 juni 2017 15:49:36 UTC+2 schreef der Raab:

der Raab

unread,
Jun 8, 2017, 10:44:02 AM6/8/17
to Haxe
THANKS! 
Reply all
Reply to author
Forward
0 new messages