The Artima Developer Community
Sponsored Link

Weblogs Forum
What if Constant Values were also valid Types?

41 replies on 3 pages. Most recent reply: Jan 20, 2006 10:43 AM by Gregor Zeitlinger

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 41 replies on 3 pages [ « | 1 2 3 | » ]
Terje Slettebø

Posts: 205
Nickname: tslettebo
Registered: Jun, 2004

Re: What if Constant Values were also valid Types? Posted: Jan 19, 2006 11:03 AM
Reply to this message Reply
Advertisement
> > So be carefull. 42 is subtype of const int, but not of
> > int.
>
> 'const int' is a type?

Naturally. Consider:

const int a=123;

a=456; // Error, "a" is const (the type of "a" is "const int")

int b=123;

b=456; // Ok (the type of "b" is "int")

They definitely support different operations, which means they are different types.

>That seems pretty undesireable and unintuitive.

I don't think so. What do you find strange about it?

Terje Slettebø

Posts: 205
Nickname: tslettebo
Registered: Jun, 2004

Re: What if Constant Values were also valid Types? Posted: Jan 19, 2006 11:11 AM
Reply to this message Reply
> > Yes. It's a bit like this blog: We have the answer, but
> > not the question (what it's for). ;)
>
> I am throwing the idea out mostly as a tease and to see
> where other people go with it.

*g* I don't know if you understood my reference, but unless it's a striking coincidence that you used that number, 42 is a well-known number to "Hitchhiker"-fans. It's "the answer to Life, the Universe and Everything". The problem is coming up with the question... :)

> Personally, I find the idea useful in the context template
> metaprogramming.

Myself, I have difficulty understand what the idea is about, given that you haven't specified at all what instances of these "value types" would be like, i.e. what you may do with them.

Incidentally, "value types" is a well-known and useful concept, but it's something rather different to this... :)

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: What if Constant Values were also valid Types? Posted: Jan 19, 2006 11:35 AM
Reply to this message Reply
> > > So be carefull. 42 is subtype of const int, but not
> of
> > > int.
> >
> > 'const int' is a type?
>
> Naturally. Consider:
>
> const int a=123;
>
> a=456; // Error, "a" is const (the type of "a" is "const
> int")

You don't need 'const int' to be a type from the language definition perspective to make this work.

> int b=123;
>
> b=456; // Ok (the type of "b" is "int")
>
> They definitely support different operations, which means
> they are different types.
>
> >That seems pretty undesireable and unintuitive.
>
> I don't think so. What do you find strange about it?

Constant is an adjective i.e. a modifier of the variable. I'm not saying that implementing the feature in this way is incorrect but defining it this way at the language level seems like backwards design. From an 'ergonomic' view, good design isn't derived from ease of implementation.

A system where there is an implicit doppleganger type for every declared type seems extremely inelegant to me.

Kresimir Cosic

Posts: 34
Nickname: kreso1
Registered: Jan, 2006

Re: What if Constant Values were also valid Types? Posted: Jan 19, 2006 12:49 PM
Reply to this message Reply
@Terje Slettebø

> I'm not sure if I understand what you mean.
> Could you provide some example code showing what
> it is C++ doesn't let you express?

Of course, that's simple:
class Rectangle
{
     public:
     virtual float GetWidth()  const =0;
     virtual float GetHeight() const =0;
     virtual void  SetWidth(float w)=0;
     virtual void  SetHeight(float h)=0;
};
class Square : 
    public const Rectangle // I cant do this
{
     public:
     virtual float GetWidth()  const =0;
     virtual float GetHeight() const =0;
     virtual float GetSize()   const =0;
     virtual void  SetSize(float s)=0;
};

Instead, I have to write another class const_Rectangle. Ugly.

Kresimir Cosic

Posts: 34
Nickname: kreso1
Registered: Jan, 2006

Re: What if Constant Values were also valid Types? Posted: Jan 19, 2006 12:54 PM
Reply to this message Reply
> Personally, I find the idea useful in the context template
> metaprogramming.

Can we get an example where it would be usefull (more usefull than ordinary constants). You seem to have something in mind. I can't think of anything.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: What if Constant Values were also valid Types? Posted: Jan 19, 2006 1:01 PM
Reply to this message Reply
> > Personally, I find the idea useful in the context
> template
> > metaprogramming.
>
> Can we get an example where it would be usefull (more
> usefull than ordinary constants). You seem to have
> something in mind. I can't think of anything.

The only thing I can think of is if the runtime value 1 is considered an instance of the type 1:

boolean isPrime(0 number) {return false;}
boolean isPrime(1 number) {return false;}
boolean isPrime(2 number) {return true;}
boolean isPrime(3 number) {return true;}
boolean isPrime(int number) {
if (number % 2 == 0) return false;

...

Kresimir Cosic

Posts: 34
Nickname: kreso1
Registered: Jan, 2006

Re: What if Constant Values were also valid Types? Posted: Jan 19, 2006 1:32 PM
Reply to this message Reply
> boolean isPrime(0 number) {return false;}
> boolean isPrime(1 number) {return false;}
> boolean isPrime(2 number) {return true;}
> boolean isPrime(3 number) {return true;}
> boolean isPrime(int number) {
> if (number % 2 == 0) return false;

I think this won't work, or is useless. Just consider

int a;
scanf ("%d",&a);
bool b=isPrime(a); //which version gets called?

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: What if Constant Values were also valid Types? Posted: Jan 19, 2006 1:38 PM
Reply to this message Reply
> > boolean isPrime(0 number) {return false;}
> > boolean isPrime(1 number) {return false;}
> > boolean isPrime(2 number) {return true;}
> > boolean isPrime(3 number) {return true;}
> > boolean isPrime(int number) {
> > if (number % 2 == 0) return false;
>
> I think this won't work, or is useless. Just consider
>
> int a;
> scanf ("%d",&a);
> bool b=isPrime(a); //which version gets called?

What's the type and value of 'a'?

Kresimir Cosic

Posts: 34
Nickname: kreso1
Registered: Jan, 2006

Re: What if Constant Values were also valid Types? Posted: Jan 19, 2006 1:58 PM
Reply to this message Reply
> > int a;
> > scanf ("%d",&a);
> > bool b=isPrime(a); //which version gets called?
>
> What's the type and value of 'a'?

Type is int, exact value unknown. What will happen is that version
boolean isPrime(int number)
will get called even if 'a' is 1 or 2. This version will almost always get called.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: What if Constant Values were also valid Types? Posted: Jan 19, 2006 2:12 PM
Reply to this message Reply
> > > int a;
> > > scanf ("%d",&a);
> > > bool b=isPrime(a); //which version gets called?
> >
> > What's the type and value of 'a'?
>
> Type is int, exact value unknown. What will happen is that
> version
> boolean isPrime(int number)
> will get called even if 'a' is 1 or 2. This version will
> almost always get called.

As I said before, this would only be useful if Heron supports dynamic dispatch.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: What if Constant Values were also valid Types? Posted: Jan 19, 2006 2:13 PM
Reply to this message Reply
> As I said before, this would only be useful if Heron
> supports dynamic dispatch.

Or more specifically double-dispatch.

Howard Lovatt

Posts: 321
Nickname: hlovatt
Registered: Mar, 2003

Re: What if Constant Values were also valid Types? Posted: Jan 19, 2006 2:33 PM
Reply to this message Reply
When scanf creates the value that is assigned to a it creates an int, say 5, that is of class 5. To be useful the language needs multiple dispatch, as already pointed out in this forum. Or you allow extensible classes so that you can write:
addto int {
  boolean isPrime() { // virtual function - does Herron require virtual?
    if ( this % 2 == 0 ) return false; // note this - since type is the value there is no field with the value in it
    ...
  }
}
 
addto 0 {
  boolean isPrime() { return false; }
}
 
...

Then you can write:
int a;
scanf( "%d", &a );
bool b = a.isPrime();

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: What if Constant Values were also valid Types? Posted: Jan 19, 2006 3:41 PM
Reply to this message Reply
> > > > int a;
> > > > scanf ("%d",&a);
> > > > bool b=isPrime(a); //which version gets called?
> > >
> > > What's the type and value of 'a'?
> >
> > Type is int, exact value unknown. What will happen is
> that
> > version
> > boolean isPrime(int number)
> > will get called even if 'a' is 1 or 2. This version
> will
> > almost always get called.
>
> As I said before, this would only be useful if Heron
> supports dynamic dispatch.

Compare:


bool b1 = isPrime(someFxnReturningInt());
bool b2 = isPrime(3);


Here the specialization becomes useful.

Howard Lovatt

Posts: 321
Nickname: hlovatt
Registered: Mar, 2003

Re: What if Constant Values were also valid Types? Posted: Jan 19, 2006 3:55 PM
Reply to this message Reply
> As I said before, this would only be useful if Heron
> supports dynamic dispatch.

As a blantant plug for my own work:

http://pec.dev.java.net/nonav/frontpage.html

Is a compiler for Java that supports Multiple Dispatch amongst other extensions.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: What if Constant Values were also valid Types? Posted: Jan 19, 2006 4:49 PM
Reply to this message Reply
> Compare:
>
>

> bool b1 = isPrime(someFxnReturningInt());
> bool b2 = isPrime(3);
>

>
> Here the specialization becomes useful.

I'm not sure I get your point. Why does the specialization become useful? If double-dispatch isn't provided, then isPrime(int) still needs to check whether 3 is Prime. So having a second isPrime for the value of three is only useful if I know when I am writing my program that a condition depends on whether 3 is prime. I can just write 'true' instead of calling isPrime(3).

I think, though, I may be getting a clue as to what you meant about metaprogramming.

Flat View: This topic has 41 replies on 3 pages [ « | 1  2  3 | » ]
Topic: M Clock 2.0 Previous Topic   Next Topic Topic: Static Behavioral Subtyping and Heron Traits

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use