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 20 8975
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 InvalidOperatio nException ( "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.inv alidwrote in message
news:%2******** ********@TK2MSF TNGP05.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
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
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
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.inv alidwrote in message
news:Or******** ******@TK2MSFTN GP06.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
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
On May 10, 6:23 am, "Andrew Morton" <a...@in-press.co.uk.inv alid>
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. za***@construct ion-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 overcomplicatin g 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 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ron_Adam |
last post by:
I'm trying to figure out how to test function arguments by adding a
decorator.
@decorate
def func( x):
# do something
return x
This allows me to wrap and replace the arguments with my own, but not
get the arguments that the original function received.
|
by: Bill Davy |
last post by:
I am working with MSVC6 on Windows XP.
I have created an MSVC project called SHIP
I have a file SHIP.i with "%module SHIP" as the first line (file is below).
I run SHIP.i through SWIG 1.3.24 to obtain SHIP_wrap.cpp and SHIP.py; the
latter contains the line "import _SHIP".
I compile SHIP_wrap.cpp and a bunch of files into a DLL which I have the
|
by: Adam Warner |
last post by:
Hello all,
I'm very new to C but I have a number of years of Common Lisp programming
experience. I'm trying to figure out ways of translating higher order
concepts such as closures into C. The code will not be idiomatic C.
GCC has an extension to ISO C that permits nested functions:
<http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html>
For implementing closures they have a serious limitation:
|
by: Edward Diener |
last post by:
In C++ an overridden virtual function in a derived class must have the exact
same signature of the function which is overridden in the base class, except
for the return type which may return a pointer or reference to a derived
type of the base class's return type.
In .NET the overridden virtual function is similar, but an actual parameter
of the function can be a derived reference from the base class's reference
also.
This dichotomy...
|
by: Ian Bicking |
last post by:
I got a puzzler for y'all. I want to allow the editing of functions
in-place. I won't go into the reason (it's for HTConsole --
http://blog.ianbicking.org/introducing-htconsole.html), except that I
really want to edit it all in-process and in-memory. So I want the
identity of the function to remain the same, even as I edit the body
and hopefully the signature too.
Well, the reason is that I want to edit any function object, without...
| |
by: I.M. !Knuth |
last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until
I started goofing around with this:
================================================================================
/* A function that returns a pointer-of-arrays to the calling function. */
#include <stdio.h>
int *pfunc(void);
|
by: Beta What |
last post by:
Hello,
I have a question about casting a function pointer. Say I want to make
a generic module (say some ADT implementation) that requires a function
pointer from the 'actual/other modules' that takes arguments of type
(void *) because the ADT must be able to deal with any type of data.
In my actual code, I will code the function to take arguments of their
real types, then when I pass this pointer through an interface
function, I...
|
by: simon |
last post by:
hi,
I would like to separate my javascript completely from my xhtml. in the
end there should be only
<script type="text/javascript" src="javalib.js"></script>
in the head-tag to my javascript.
Because I want to use some ajax-requests and other javascript-functions
on my xhtml, I need to dynamically add event handlers to any possible
dom-elements. All solutions I found so fare are for specific, pre-known
|
by: Tony Lownds |
last post by:
(Note: PEPs in the 3xxx number range are intended for Python 3000)
PEP: 3107
Title: Function Annotations
Version: $Revision: 53169 $
Last-Modified: $Date: 2006-12-27 20:59:16 -0800 (Wed, 27 Dec 2006) $
Author: Collin Winter <collinw@gmail.com>,
Tony Lownds <tony@lownds.com>
Status: Draft
Type: Standards Track
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |