Skip to content

Instantly share code, notes, and snippets.

@ericnewton76
Last active November 16, 2023 20:08
Show Gist options
  • Save ericnewton76/fd94ffe53350caf6933329ad64492266 to your computer and use it in GitHub Desktop.
Save ericnewton76/fd94ffe53350caf6933329ad64492266 to your computer and use it in GitHub Desktop.
C# language proposal: loose keyword
webserviceproxynamespace.LatLng latlng = new webserviceProxy().GetLatLng("Orlando, FL, 32803");
anotherproxynamespace.LatLng latlng2 = new anotherwebservice().GetPosition("Orlando", "FL", "32803");
//this other library wants a LatLng object
var map = new Mapping.MapManager();
map.MarkPosition(loose latlng); //parameter type is Mapping.LatLng but latlng is webserviceproxynamespace.LatLng type, should work!
map.MarkPosition(loose latlng2); //parameter type is Mapping.LatLng but latlng2 is anotherproxynamespace.LatLng type, should work!
//how does this work?
//loose keyword says maybe c# compiler automaps via dynamic object and runtime IL
//key is, for most simple types, being able to pass these object with the same shape (names of properties)
//currently this is always handled by automappers or repititive map coding
//type definitions
namespace Mapping
{
public class record LatLng(double Lat, double Lng);
public class Mapper
{
public bool MarkPosition(Mapping.LatLng latlng) => return true;
}
}
namespace webserviceproxynamespace
{
public class LatLng(decimal Lat, decimal Lng);
public class WebserviceProxy
{
public LatLng GetLatLng(string locationDefinition) => return new LatLng(0,0);
}
}
namespace anotherproxynamespace
{
public class LatLng(float Lat, float Lng);
public class anotherwebservice
{
public LatLng GetPosition(string city, string state, string zip) => return new LatLng(1,1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment