469,646 Members | 1,145 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

One function name, two return types?

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
May 10 '07 #1
20 8349

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

"Andrew Morton" <ak*@in-press.co.uk.invalidwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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


May 10 '07 #2
Robin Tucker wrote:
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
May 10 '07 #3
Andrew Morton wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
May 10 '07 #4

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

"Andrew Morton" <ak*@in-press.co.uk.invalidwrote in message
news:Or**************@TK2MSFTNGP06.phx.gbl...
Robin Tucker wrote:
>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

May 10 '07 #5
Göran Andersson wrote:
Andrew Morton wrote:
>Is it possible to have two function declarations which take the same
parameters but return different types depending on how the function
is used?
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
May 10 '07 #6
On May 10, 6:23 am, "Andrew Morton" <a...@in-press.co.uk.invalid>
wrote:
Göran Andersson wrote:
Andrew Morton wrote:
Is it possible to have two function declarations which take the same
parameters but return different types depending on how the function
is used?
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
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.

May 10 '07 #7
za***@construction-imaging.com wrote:
On May 10, 6:23 am, Andrew wrote:
>>Andrew Morton wrote:
Is it possible to have two function declarations which take the
same parameters but return different types depending on how the
function is used?

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
May 10 '07 #8
Andrew Morton wrote:
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
_____
http://www.guffa.com
May 10 '07 #9
Göran Andersson wrote:
Andrew Morton wrote:
>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.
I only found ctrl+space a few days ago <embarrased emoticon>.
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
May 10 '07 #10
On May 10, 9:22 am, "Andrew Morton" <a...@in-press.co.uk.invalid>
wrote:
Göran Andersson wrote:
Andrew Morton wrote:
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.

I only found ctrl+space a few days ago <embarrased emoticon>.
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 :-( -- 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

May 10 '07 #11
Or still another option would be to always return an array...

"Göran Andersson" <gu***@guffa.coma écrit dans le message de news:
%2****************@TK2MSFTNGP06.phx.gbl...
Andrew Morton wrote:
>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.

--
Göran Andersson
_____
http://www.guffa.com

May 10 '07 #12
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

"Andrew Morton" wrote:
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
May 10 '07 #13
rowe_newsgroups wrote:
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. :)

--
Göran Andersson
_____
http://www.guffa.com
May 10 '07 #14
On May 10, 9:34 am, guy <g...@discussions.microsoft.comwrote:
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

"Andrew Morton" wrote:
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
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

May 10 '07 #15
On May 10, 11:18 am, Göran Andersson <g...@guffa.comwrote:
rowe_newsgroups wrote:
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. :)

--
Göran Andersson
_____http://www.guffa.com
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

May 10 '07 #16
On May 10, 10:26 am, Chris Dunaway <dunaw...@gmail.comwrote:
On May 10, 9:34 am, guy <g...@discussions.microsoft.comwrote:
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
"Andrew Morton" wrote:
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

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

May 10 '07 #17
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...

"guy" <gu*@discussions.microsoft.coma écrit dans le message de news:
54**********************************@microsoft.com...
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

"Andrew Morton" wrote:
>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

May 10 '07 #18
Patrice wrote:
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
May 11 '07 #19
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
"Chris Dunaway" wrote:
On May 10, 9:34 am, guy <g...@discussions.microsoft.comwrote:
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

"Andrew Morton" wrote:
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

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

May 11 '07 #20
Looks a quite good syntax. I didn't gave this a close look at this is not
something I miss but I think it could also cause some problems regarding
inheritance. What if you used the relaxed syntax and you add later another
overloads etc...

I made a quick search and it looks like it is possible to simulate though
this using C++ :

http://cpptips.hyperformix.com/cpptips/ret_func_sig.txt
(though the poster mention that he suggest to stay away from it).

---
Patrice

"Andrew Morton" <ak*@in-press.co.uk.invalida écrit dans le message de
news: ur**************@TK2MSFTNGP06.phx.gbl...
Patrice wrote:
>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

May 11 '07 #21

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Ron_Adam | last post: by
26 posts views Thread by Adam Warner | last post: by
5 posts views Thread by Ian Bicking | last post: by
3 posts views Thread by Beta What | last post: by
4 posts views Thread by Tony Lownds | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.