One function name, two return types?

  • Thread starter Thread starter Andrew Morton
  • Start date Start date
A

Andrew Morton

Is it possible to have two function declarations which take the same
parameters but return different types depending on how the function is used?

function f(x) as string
' return a string
end function

function f(x) as string()
' return an array of strings
end function

' somewhere in a subroutine:

Dim S As String=f(1) ' calls the string version of f(x)

Dim A As String()=f(2) ' calls the array version of f(x)

Why? Because I'm extracting IPTC metadata from a file, and some metadata has
a single value (e.g. the caption) and some may have multiple values (e.g.
the keywords). I just think it would look neater to have one function name.

Andrew
 
Hi Andrew,

Your function can return "Object" and then leave working out which type was
returned up to the caller. A more involved solution would be to construct
an abstract interface or base class and create two classes that implement
it. Your function "f()" can then behave like a factory, taking X's and
returning instances of your interface. However, in essence this is what the
"Object" return is doing, apart from you need to cast it to Array or String
depending on the TypeOf check.



Dim o As Object = f ( p )

If TypeOf (o) Is .....

ElseIf TypeOf (o) Is

Else
Throw New InvalidOperationException ( "f returned an invalid type" )
End If




function f (x) As Object

If ( ... ) Then
' return a string
ElseIf ( ... )
' return an array of strings
Else
Return Nothing
End If

End Sub




Robin
 
Robin said:
Hi Andrew,

Your function can return "Object" and then leave working out which
type was returned up to the caller.

That just sounds... wrong. I'll go with two function names then.

Thanks,

Andrew
 
Andrew said:
Is it possible to have two function declarations which take the same
parameters but return different types depending on how the function is used?

function f(x) as string
' return a string
end function

function f(x) as string()
' return an array of strings
end function

' somewhere in a subroutine:

Dim S As String=f(1) ' calls the string version of f(x)

Dim A As String()=f(2) ' calls the array version of f(x)

Why? Because I'm extracting IPTC metadata from a file, and some metadata has
a single value (e.g. the caption) and some may have multiple values (e.g.
the keywords). I just think it would look neater to have one function name.

Andrew

The return type is not part of the method signature, so you can not have
overloads of a method that take the same parameters and return different
data types.

As Robin suggested, you can return the data as Object, but then you have
to type cast the value after every call.

I would suggest that you just choose different names for the methods.
It's easier to read the code if it's clear from the method name what
kind of data the method returns, so that you don't have to examine the
data that you send into the method to find out what it returns. Even if
the method names becomes a bit longer, the code is still more readable
than if you have to put a type cast around every call to the methods.
 
Yes, that is the cleaner solution. Particularly from a maintenance
perspective. If the number of functions you need starts to multiply too
much, you can start to think about alternative patterns.


Robin
 
I would suggest that you just choose different names for the methods.
It's easier to read the code if it's clear from the method name what
kind of data the method returns, so that you don't have to examine the
data that you send into the method to find out what it returns. Even
if the method names becomes a bit longer, the code is still more
readable than if you have to put a type cast around every call to the
methods.

That's what I'm doing now, unfnortuantly longer names and my typoing
ability... :-)

Thanks,

Andrew
 
That's what I'm doing now, unfnortuantly longer names and my typoing
ability... :-)

Thanks,

Andrew

Why not create a class with three properties as the function type? One
property could serve as an indicator as to which of the other two
properties contains the return value.
 
Why not create a class with three properties as the function type? One
property could serve as an indicator as to which of the other two
properties contains the return value.

I think that's overcomplicating it - perhaps even more than returning an
object as suggested previously.

/I/ know what the type of the return value will be, and if the syntax was
there the compiler would be able to deduce which version of the function to
call, in a similar way to being able to overload a function with different
numbers of parameters. But it isn't, so two function names will have to do.

Andrew
 
Andrew said:
That's what I'm doing now, unfnortuantly longer names and my typoing
ability... :-)

With intellisense you rarely have to type the entire names, so I don't
really see long method names as a problem.

Besides, if you really think that the typing is the hardest part of
programming, you can't be writing any challenging code... ;)
 
Göran Andersson said:
With intellisense you rarely have to type the entire names, so I don't
really see long method names as a problem.

I only found ctrl+space a few days ago said:
Besides, if you really think that the typing is the hardest part of
programming, you can't be writing any challenging code... ;)

The code isn't challenging; the noisy programming environment is :-( -- I am
the whole IT department, so I'm frequently interrupted with "Andrew, can you
show me how to...", "Andrew, we just need this tweaked on the web site...",
etc. And we now have a construction site just the other side of the road
from my desk. Oh how I love jobs that don't need any concentration, like
installing printers or new computers :-)

Andrew
 
The code isn't challenging; the noisy programming environment is :-( -- Iam
the whole IT department, so I'm frequently interrupted with "Andrew, can you
show me how to...", "Andrew, we just need this tweaked on the web site...",
etc. And we now have a construction site just the other side of the road
from my desk. Oh how I love jobs that don't need any concentration, like
installing printers or new computers :-)

Andrew
I only found ctrl+space a few days ago <embarrased emoticon>.

Ctrl+j will also bring up intellisense. One of my favorite features of
the C# editor is the fact the intellisense pops up as soon as you
begin typing, and not just whenever you type a period or an open
parentheses. It's surely possible to turn this feature on for the VB
editor - but I haven't found out how.

Thanks,

Seth Rowe
 
Andrew,

I asked the same question in VB2002 days and was told the concept was silly
and no one would understand it.

Personally I think it would be very useful, after all it is no different
from declaring a method where the first parameter is a return value - and
that can be overloaded!

Nice to see that I am not alone:-)

cheers

guy
 
rowe_newsgroups said:
Ctrl+j will also bring up intellisense.

I like Ctrl+Space better, if what you typed can be unambigously
identified, it will just complete the word without bringing up a list
(at least in C#). :)
One of my favorite features of
the C# editor is the fact the intellisense pops up as soon as you
begin typing, and not just whenever you type a period or an open
parentheses. It's surely possible to turn this feature on for the VB
editor - but I haven't found out how.

Actually I turned that off. I hate when things start to pop up
everywhere whern you haven't asked for it. :)
 
Andrew,

I asked the same question in VB2002 days and was told the concept was silly
and no one would understand it.

Personally I think it would be very useful, after all it is no different
from declaring a method where the first parameter is a return value - and
that can be overloaded!

Nice to see that I am not alone:-)

cheers

guy

I can't think of any languages, off the top of my head, that allow
overloading of methods based on return type alone. I think one of the
main problems with it is that it would be difficult in some cases for
the compiler to figure out which method to call. Consider this code:

'Method returns a string
Public Function SomeFunction() As String
End Function

'Method returns a string array
Public Function SomeFunction() As String()
End Function

Dim o As Object = SomeFunction()


Which of the two methods above should the compiler choose? I know
that's a contrived example, but it illustrates one difficulty of
overloading a method by return type only.

I'd be interested in hearing some other arguments, either for or
against the case for overloading by return type alone.

Chris
 
I like Ctrl+Space better, if what you typed can be unambigously
identified, it will just complete the word without bringing up a list
(at least in C#). :)


Actually I turned that off. I hate when things start to pop up
everywhere whern you haven't asked for it. :)
Actually I turned that off. I hate when things start to pop up
everywhere whern you haven't asked for it. :)

I love it - perhaps it's due to my poor ability to type case-sensitive
words. Do you know how to turn on the feature for VB? There is no
"IntelliSense" choice in the Basic node in the Tools-->Options menu
like there is for C#.

Thanks,

Seth Rowe
 
I can't think of any languages, off the top of my head, that allow
overloading of methods based on return type alone. I think one of the
main problems with it is that it would be difficult in some cases for
the compiler to figure out which method to call. Consider this code:

'Method returns a string
Public Function SomeFunction() As String
End Function

'Method returns a string array
Public Function SomeFunction() As String()
End Function

Dim o As Object = SomeFunction()

Which of the two methods above should the compiler choose? I know
that's a contrived example, but it illustrates one difficulty of
overloading a method by return type only.

I'd be interested in hearing some other arguments, either for or
against the case for overloading by return type alone.

Chris

Here's, perhaps, a better example:

Public Interface IFoo
End Interface

Public Class ClassA : Implements IFoo
End Class

Public Class ClassB : Implements IFoo
End Class


'somewhere else
Public Function SomeFunction() As ClassA
End Function

Public Function SomeFunction() As ClassB
End Function


'How could this code be resolved?
Dim f As IFoo = SomeFunction()

Although, I suppose, the compiler could throw an error in that case.

Chris
 
IMO the problem is that an argument is always well defined in type. On the
other hand there is no association between the return value and the
receiving variable (if any).

For example :

Debug.WriteLine(MyFunction())

Which version should be called if MyFunction can return Object or String ?
There is no way to tell...
 
Patrice said:
IMO the problem is that an argument is always well defined in type.
On the other hand there is no association between the return value
and the receiving variable (if any).

For example :

Debug.WriteLine(MyFunction())

Which version should be called if MyFunction can return Object or
String ? There is no way to tell...

Well, if a way of having different versions of a function depending on the
required return type was implemented, then to remove ambiguity where
necessary:

Debug.WriteLine(MyFunction() As String)

Andrew
 
Hi Chris,
suppose I rewrote your function-
'Method returns a string
Public Function SomeFunction() As String
End Function

'Method returns a string array
Public Function SomeFunction() As String()
End Function

as
Public Sub Somefunction(MyReturnValue as String)
and
Public Sub SomeFunction(MyReturnValue as String())

and called it passingan object?

guy
 
Back
Top