On Thu, Nov 6, 2014 at 1:07 PM, Nils Bruin <nbr...@sfu.ca> wrote:
> On Thursday, November 6, 2014 10:57:23 AM UTC-8, Robert Bradshaw wrote:
>>
>>     cdef float -> float F
>>     cdef (float -> float, float, float) -> float G
>>     cdef (char*) -> (float -> float, float, float) -> float H
>
> Is there any precedent for an infix operator in type declarations? I find it
> really surprising.

The "input -> output" syntax is used for anonymous functions and
callable types in several languages, from Haskell to OCaml to Java.
Python 3 introduces -> to annotate function return types in PEP 3107.

> It's a little better with parentheses I think:
>
> cdef (float -> float) F
>
> Without them, the "float F" seems to visually bind much more closely than
> the "float -> float", contrary to your proposed precedence.

Yeah, the parentheses make this more clear, though cdef float ->
(float F) doesn't really have a reasonable interpretation. The syntax
"[type] [space] [name]" is a bit unfortunate--we already have ugly,
special logic to handle things like "cdef unsigned short int foo" or
even worse "def foo(self, int): ..."

> Also, C's
> convention that "a->b == *a.b" makes the "->" use here unexpected,
> especially because it's used in an environment where pointer (type)s are
> involved.

Yeah, Stefan pointed this out too, though we don't use -> in Cython
(except for function return annotation). I think the fact that these
are function *pointers* wouldn't need to be called out, given that
functions can be assigned to function pointers and function pointers
are callable just like functions. They'd behave more like first class
function objects.

>>     cdef lambda float: float F
>>     cdef lambda (lambda float: float), float, float: float G
>>     cdef lambda (char*): lambda: (lambda float: float), float, float:
>> float H
>
>
> At least this overcomes the infix issue a bit: right from the start, the
> "lambda" hints that this is a function type. However, visually again this
> reads to me (because : in normal writing a : would separate sentences of
> words separated by spaces, hence indicating that a space has higher
> precedence than a colon) as:
>
> cdef lambda float: (float F)
>
> which can be circumvented by parentheses:
>
> cdef (lambda float: float) F
>
> The "lambda" solution doesn't look very appealing here, but I think (with
> parentheses) it is the one that is most likely to be unambiguously clear.

Yeah, it looks funny, but is harder to miss-interpret.

- Robert

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to