Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Const ValueTuple #1034

Closed
ghost opened this issue Oct 22, 2017 · 8 comments
Closed

Const ValueTuple #1034

ghost opened this issue Oct 22, 2017 · 8 comments

Comments

@ghost
Copy link

ghost commented Oct 22, 2017

Why can't ValueTuple's be declared const? I understand why value types generally can't be declared const but the reason about constructor can't be aplied to ValueTuples.

I tried to use const (int, int) tuple = (1, 2); in my code but it's not allowed.

@Joe4evr
Copy link
Contributor

Joe4evr commented Oct 22, 2017

Because there's no primitive representation of the ValueTuple types in the CLR.

Also, the ValueTuple types are mutable, so that's a whole other can of worms.

@iam3yal
Copy link
Contributor

iam3yal commented Oct 22, 2017

@drozdekl There are at least three issues with this:

  1. Constants require to be initialized at the point of declaration.

  2. You can't declare a field of a generic type as a constant even when the constrain is struct.

  3. Because of the above points you can't have a primitive representation such as ConstValueTuple as @Joe4evr pointed out.

So the following would work:

void Main()
{
	new ConstValueTuple<int, int>();
}

struct ConstValueTuple<T1, T2> where T1: struct where T2: struct
{
	public ConstValueTuple(T1 item1, T2 item2)
	{
	}
}

But the following isn't possible:

struct ConstValueTuple<T1, T2> where T1: struct where T2: struct
{
	public ConstValueTuple(T1 item1, T2 item2)
	{
		Item1 = item1;
		
		Item2 = item2;
	}
	
	public const T1 Item1; 
	
	public const T2 Item2;
}

@ghost
Copy link
Author

ghost commented Oct 22, 2017

@eyalsk Thank you for your answer. I thought that it would be great to have const tuples but I understand that it is not possible. I thought that all necessary things would be known at compile time. If one wrote const (int,int) tuple = (1,2) or even const ((int,int),int) tuple = ((1,2),3) then there are const values on the right side and ValueTuples are so simple that I would expect this would be OK. Anyway, thank you!

I almost forgot that ValueTuples are not built in and I shoudn't expect anything special about them.

@ghost ghost closed this as completed Oct 22, 2017
@iam3yal
Copy link
Contributor

iam3yal commented Oct 22, 2017

@drozdekl You welcome. :)

@ghost
Copy link
Author

ghost commented Oct 23, 2017

@eyalsk

I would like to ask another question but do not want to start a thread. Why can't ValueTuples consist only from one value? I have written buffered reader which knows key value from last reading and would like to make return value from writer in the same way.

Currently I have:

public interface IReaderStream<T> : IDisposable {
	(T Item, object LastKey) Read(object key);
}

interface IWriterStream<T> : IDisposable {
	object Write(T item, object key);
}

public interface IReader<T> {
	T ReadFrom(IReaderStream<T> source);
}

interface IWriter<T> {
	T WriteTo(IReaderStream<T> destination);
}

But I would like to write (object LastKey) Write(T item, object key);

@iam3yal
Copy link
Contributor

iam3yal commented Oct 23, 2017

@drozdekl You can check #883. :)

@ghost
Copy link
Author

ghost commented Nov 4, 2017

@eyalsk

Is there any possibility in language to have an interface which is publicly sealed? I would like to have such interface which would provide an interface to user and could be anytime completely replaced. Also I would like to make sure that I control which classes can implement that interface to influence correct behaviour.

public static class Provider {
    public IItem New() => new ItemImplementation();
}

// publicly sealed
public internal IItem {

}

internal class ItemImplementation() {

}

User of that dll could write:

IItem x = Provider.Build();

but not

class A : IItem {
}

@ghost ghost reopened this Nov 4, 2017
@ghost ghost closed this as completed Nov 4, 2017
@svick
Copy link
Contributor

svick commented Nov 4, 2017

@drozdekl There is an old proposal to do that: dotnet/roslyn#220. It's not clear to me why it was closed. There's also another similar (but broader) proposal at #485.


But I don't think you should ask questions not related to your original issue here. You should either open a new issue or move to some other place, like the gitter channel (@eyalsk seems to be present there).

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants