Search code examples
programming-languages

dot operators on functions


I don't know if this is possible, but are there any languages where you can use a dot operator on a function per se. I'll give an example.

function blah returns type2

type 2 looks like this

{ data

number }

when I call blah are there any languages that support blah.number, so that when it makes the function call and gets the type2, it then grabs number and returns that. I'm sorry if this is an obvious answer, but I couldn't even think of a good way to word it to google it.

I just ran into a situation that would be convienient to have that, rather then make an intermediate variable you just make sure you return the type.

I know that I could add a "get" function that would get the specific number variable from that type, but that's an additional function someone would have to add so I am excluding that as a option (as I can just return the type and access using a variable there isn't really a dire need for a new function).

EDIT: I feel like an idiot.....

EDIT # 2: For some reason I had it in my head that you couldn't do dot operations on functions, (I don't care about the parentheses I was just trying to give an example)

Edit # 3: Is there a name for this or is it still just a dot operation?


Solution

  • Well this works in C if the function returns a struct like this:

    struct retval {
        char * data;
        int number;
    };
    
    retval foo() {
        // do something and then return an instance of retval
    }
    
    // call
    int a = foo().number;
    

    I would like to know if there is any language that does not support something like this.

    About Edit #3 The name would generally be member access, since all you do is to access a member of the return value. This could differ across languages though.