Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add ProxyOf and TypeDef #300

Merged
merged 7 commits into from Mar 4, 2012

Conversation

Projects
None yet
8 participants
Member

9rnsr commented Oct 23, 2011

ProxyOf requires following dmd patch:
D-Programming-Language/dmd#280 Merged.

ProxyOf works like alias this, except implicit conversion to original type.
I think this is also useful for scoped!T, because scoped!T object should reject implicit conversion to T.

TypeDef is a simple use case of ProxyOf.

@yebblies yebblies and 1 other commented on an outdated diff Oct 23, 2011

win32.mak
@@ -353,7 +354,8 @@ phobos.lib : $(OBJS) $(SRCS) \
unittest : $(SRCS) phobos.lib
$(DMD) $(UDFLAGS) -L/co -c -unittest -ofunittest11.obj $(SRCS_11)
$(DMD) $(UDFLAGS) -L/co -c -unittest -ofunittest12.obj $(SRCS_12)
- $(DMD) $(UDFLAGS) -L/co -c -unittest -ofunittest2.obj $(SRCS_2)
+ $(DMD) $(UDFLAGS) -L/co -c -unittest -ofunittest2.obj $(SRCS_21)
+ $(DMD) $(UDFLAGS) -L/co -c -unittest -ofunittest2.obj $(SRCS_22)
@yebblies

yebblies Oct 23, 2011

Member

Won't this override unittest2.obj?

@9rnsr

9rnsr Oct 23, 2011

Member

Wow! Thanks.

Member

JakobOvrum commented Dec 17, 2011

TypeDef should probably have an initializer template argument, i.e.

struct TypeDef(T, alias Init)
{
    private T payload = Init;
    / * ... * /

This way, it can fully cover the old typedef, which let you do this:

typedef int Foo = 123;

Foo foo; // foo is default-initialized to 123
Foo[2] arr; // and so are arr[0] and arr[1]

I'm not sure what the prettiest way is to make the initializer template argument optional (it should obviously default to T.init).

Member

9rnsr commented Dec 18, 2011

Updated for custom initializer.

@tgehr tgehr and 2 others commented on an outdated diff Dec 27, 2011

std/typecons.d
+ shared auto ref opSlice(B, E)(auto ref B b, auto ref E e) { return a[b..e]; }
+ const shared auto ref opSlice(B, E)(auto ref B b, auto ref E e) { return a[b..e]; }
+
+ auto ref opAssign(V)(auto ref V v) { return a = v; }
+ const auto ref opAssign(V)(auto ref V v) { return a = v; }
+ immutable auto ref opAssign(V)(auto ref V v) { return a = v; }
+ shared auto ref opAssign(V)(auto ref V v) { return a = v; }
+ const shared auto ref opAssign(V)(auto ref V v) { return a = v; }
+
+ auto ref opIndexAssign(V, I...)(auto ref V v, auto ref I i) { return a[i] = v; }
+ const auto ref opIndexAssign(V, I...)(auto ref V v, auto ref I i) { return a[i] = v; }
+ immutable auto ref opIndexAssign(V, I...)(auto ref V v, auto ref I i) { return a[i] = v; }
+ shared auto ref opIndexAssign(V, I...)(auto ref V v, auto ref I i) { return a[i] = v; }
+ const shared auto ref opIndexAssign(V, I...)(auto ref V v, auto ref I i) { return a[i] = v; }
+
+ auto ref opSiliceAssign(V, R...)(auto ref V v) { return a[] = v; }
@tgehr

tgehr Dec 27, 2011

Contributor

Did you intend to write "opSliceAssign" here?

@9rnsr

9rnsr Dec 28, 2011

Member

Ah... that's a typo. Thanks for your pointing out.

@andralex

andralex Mar 1, 2012

Owner

There should be a corresponding unittest that would fail with the typo.

Trass3r commented Dec 30, 2011

That typedef doesn't allow multiple typedefs with the same basetype if I'm not mistaken.
typedef void* typeA;
typedef void* typeB;

Also it only provides a super type. typedef needs to be much more sophisticated:
http://d.puremagic.com/issues/show_bug.cgi?id=5467#c0

Member

9rnsr commented Jan 2, 2012

That typedef doesn't allow multiple typedefs with the same basetype if I'm not mistaken.
typedef void* typeA;
typedef void* typeB;

It is a limitation of template based user type. We need some compiler support.

Also it only provides a super type. typedef needs to be much more sophisticated:
http://d.puremagic.com/issues/show_bug.cgi?id=5467#c0

TypeDef is an emulation of deprecated typedef (and an use case of ProxyOf), not implementation of issue 5467.

Trass3r commented Jan 2, 2012

It is a limitation of template based user type. We need some compiler support.

What about the FILE / LINE trick used in 5467?

Member

9rnsr commented Jan 2, 2012

What about the FILE / LINE trick used in 5467?

Not enough. See following test.

struct TypeDef(T, string fn = __FILE__, size_t ln = __LINE__)
{
    pragma(msg, fn);
    pragma(msg, ln);
}
template Scope(T)
{
    // deprecated built-in typedef
    typedef int X;

    // emulated typedef
//  alias TypeDef!int Y;                        // bad
    alias TypeDef!(int, __FILE__, __LINE__) Y;  // workaround
        // template instantiation doesn't expand
        // __FILE__ and __LINE__ in instantiated place.
}
void main()
{
    alias Scope!int.X IntX;
    alias Scope!string.X StrX;
    static assert(!is(IntX == StrX));   // OK

    alias Scope!int.Y IntY;
    alias Scope!string.Y StrY;
    static assert(!is(IntY == StrY));   // fails!
}

Template-base typedef is independent from its instantiated scope.
We need a special token to bind the instantiated scope into symbol, like following concept code.

struct TypeDef(T, string fn = __FILE__, string ln = __LINE__, alias scope = __HERE__) { ... }
template Scope(T)
{ alias TypeDef!int Y; // Scope!int and Scope!string are different scope,
                       // and __HERE__ distinguish them.
Member

klickverbot commented Feb 11, 2012

Would generating the different overloads with a simple string mixin hurt readability much? All that duplication makes my eyes hurt. ;)

Member

9rnsr commented Feb 11, 2012

Maybe the cause of autotester failing in linux system is the compiler memory corruption.
Reducing unit test code suppresses the unintended segfault in Ubuntu 10.04 LTS.

Owner

andralex commented Feb 21, 2012

This is great work. I see you rebased, let's finish my comments and then we merge.

9rnsr added some commits Oct 22, 2011

@9rnsr 9rnsr Add TypeDef 73fb568
@9rnsr 9rnsr Add ProxyOf 1ffd105
@9rnsr 9rnsr Apply review result to TypeDef
- Add third template parameter 'cookie'.
- Rename internal payload variable.
2784a73
Member

9rnsr commented Feb 22, 2012

Applied review result, and improved ProxyOf definition.
While coding, I found a dmd bug corrupting memory in compilation that breaks ProxyOf (Patch is here).
Maybe it is also the reason of some posix breaking.

Member

9rnsr commented Feb 23, 2012

OK, after fixing dmd bug, auto-tests in almost platforms are succeeding.
I think it's ready for merging this.

Member

JakobOvrum commented Feb 23, 2012

Would perhaps adding enum TypeDef_cookie = cookie; (public) make sense for TypeDef? I can't think of anything in specific right now, but I have a feeling it would aid some generic code.

Just a thought.

Member

9rnsr commented Feb 23, 2012

The purpose of cookie is for just separating instantiation of TypeDef template.
So there is no need to set human readable string to cookie, and making it public is almost useless.

Owner

andralex commented Mar 1, 2012

All of my comments seem to be gone, so I forgot a lot of my previous points :o). Nevertheless I think this is ready for committing. The names are still nagging me. ProxyOf and TypeDef are just hard on the eyes. I think Proxy and Typedef would ring much better. What do you think?

Owner

CyberShadow commented Mar 1, 2012

All of my comments seem to be gone

One of the reasons to merge instead of rebasing.

Trass3r commented Mar 1, 2012

@andralex Totally agree.

Member

9rnsr commented Mar 4, 2012

OK. Renamed.

@andralex andralex added a commit that referenced this pull request Mar 4, 2012

@andralex andralex Merge pull request #300 from 9rnsr/ProxyOf
This is a historical moment!
608218d

@andralex andralex merged commit 608218d into dlang:master Mar 4, 2012

@schveiguy schveiguy referenced this pull request in schveiguy/phobos Apr 15, 2012

Merged

std.process ready for review? #1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment